Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

HTML Canvas problem.

  • 20-10-2013 11:06am
    #1
    Registered Users, Registered Users 2 Posts: 48


    I am new to website design and I cannot get my canvas to run images. I would appreciate some advice as any fixes I have tried are not working. This is the javascript and html code,

    function drawOnCanvas(){
    var canvas = document.getElementById('mycanvas');
    canvas = canvas.getContext('2d');

    var image = new image();
    image.src= "images/img1.jpg";
    image.addEventListener("load", function(){canvas.drawOnCanvas(image,0,0);}, false);
    }


    window.addEventListener("load", drawOnCanvas, false);

    <section id="sect">
    <div>
    <canvas id ="mycanvas" width= "595" height= "595">
    Your browser does not support the HTML5 Canvas.
    </canvas>
    </div>
    </section>


Comments

  • Registered Users, Registered Users 2 Posts: 1,311 ✭✭✭Procasinator


    You have a few small mistakes.

    The object you need when creating the image variable is "Image" rather than "image" (JavaScript is case-sensitive).

    Secondly, the method you are looking for is drawImage rather than drawOnCanvas inside your event listener.

    That should leave you with:
    function drawOnCanvas(){
    	var canvas = document.getElementById('mycanvas');
    	canvas = canvas.getContext('2d');
    
    	var image = new Image();
    	image.src= "images/img1.jpg";
    	image.addEventListener("load", function() {
    		canvas.drawImage(image,0,0);
    	}, false);
    }
    
    window.addEventListener("load", drawOnCanvas, false);
    


  • Registered Users, Registered Users 2 Posts: 48 donedl1


    Thanks Procasinator, I have been stuck on this for quite a few days and looking at on-line references for a fix. Frustrated would be an understatement, thanks again.


Advertisement