Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Nov 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Image object problem,

    Hi, someone can help plese!!!!

    I dont know what happened with the source because after the script executes, there is nothing in the canvas

    function draw() {
    var ctx = document.getElementById('canvas').getContext('2d');
    var img = new Image();

    img.onload = function(){
    ctx.drawImage(img,0,0);
    }
    }

    img.src = 'images/backdrop.png';

    Can someone help please!!!

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,398
    Thanks
    12
    Thanked 570 Times in 563 Posts
    EDIT: You closed the function too early. The img.src line has to go into the function, so just move the last closing bracket } after the img.src line

    Code:
    function draw() {
       var ctx = document.getElementById('canvas').getContext('2d');
       var img = new Image();
    
       img.onload = function(){
          ctx.drawImage(img,0,0);
       }
       img.src = 'images/backdrop.png';
    }


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •