Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1052597
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T17:07:08+00:00 2026-05-16T17:07:08+00:00

i wrote a nice plugin which transforms IMG to canvas jQuery.fn.img2canvas = function() {

  • 0

i wrote a nice plugin which transforms IMG to canvas

jQuery.fn.img2canvas = function() { 

      return this.each(function(){

        if($(this).get(0).tagName=='IMG'&&$(this).parent().get(0).tagName!='CANVAS')
        {
            //alert($(this).get(0).tagName);
            $(this).load(function()
            {
                var c = $("<canvas class=' img2canvas'>"+$(this).outerHTML()+"</canvas>");
                //var c = $("<canvas></canvas>");
                $(c).attr('width', this.width);
                $(c).attr('height', this.height);
                var ctx = $(c)[0].getContext( "2d" );
                var img = new Image();
                img.src = $(this).attr('src');
                ctx.drawImage(img, 0, 0);
                $(c).data('imgsrc', this.src);
                $(c).attr('id', $(this).attr('id')+'_canvas');
                $(this).replaceWith($(c));
            });
        }
     });


    };

so far so good. but there is no way i can continue working with these canvas.

$('img').img2canvas(); //creating the canvas
$('.img2canvas').css('border', '6px solid red'); //but there is no canvas yet
$('canvas').each(function(){alert($(this).data('imgsrc'));}); // still no canvas
$('.img2canvas').each(function(){alert($(this).data('imgsrc'));}); //still no canvas

does not help.

what do i need to do to keep the plugin architecture and being able to continue working on the canvas elements? you can see a live demo here http://www.andcontext.com/inimad/sto_demo.php

thx for your input.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T17:07:09+00:00Added an answer on May 16, 2026 at 5:07 pm

    Try assign a callback feature to run when the Image has loaded

    jQuery.fn.img2canvas = function(callback)
    {
    
          return this.each(function(){
    
            if($(this).get(0).tagName=='IMG'&&$(this).parent().get(0).tagName!='CANVAS')
            {
                //alert($(this).get(0).tagName);
                $(this).load(function()
                {
                    var c = $("<canvas class=' img2canvas'>"+$(this).outerHTML()+"</canvas>");
                    //var c = $("<canvas></canvas>");
                    $(c).attr('width', this.width);
                    $(c).attr('height', this.height);
                    var ctx = $(c)[0].getContext( "2d" );
                    var img = new Image();
                    img.src = $(this).attr('src');
                    ctx.drawImage(img, 0, 0);
                    $(c).data('imgsrc', this.src);
                    $(c).attr('id', $(this).attr('id')+'_canvas');
                    $(this).replaceWith($(c));
                    callback(this);
                });
            }
         });
    };
    
    $('img').img2canvas(function(canvas){
        //...
    });
    

    This is so basically the anonymous function passed in will be called each time the image has loaded and the canvas has been created

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have wrote this little script which generates a nice random password. The problem
I wrote a nice little game which works well in its .fla form. Because
I wrote a nice little program. At some point, it is required to load
Wrote a quick Java proggy to spawn 10 threads with each priority and calculate
When i write a jQuery plugin i like to specify options for spacings the
I wrote a nice ARM assembler routine a few years back and it has
I created a form and wrote all the JQuery for it -- form validation
I wrote a program that simulates soft bodies using springs. It looks nice but
I've started to write few jQuery plugins and figured it'd be nice to setup
I don't need to write this in jQuery but I'm not versed enough in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.