0

I'm trying to add an image to my canvas but it doesn't seem to work I've taken a look and can't see anything wrong with my code but maybe someone else can.

This is my JS file.

if (window.addEventListener) 
{
    window.addEventListener( 'load', onLoad, false);
}

function onLoad()
{
var canvas;
var context;

function initialise() 
{
    canvas = document.getElementById('canvas'); 

    if (!canvas.getContext)
    { 
        alert('Error: no canvas.getContext!'); 
        return; 
    }

    context = canvas.getContext('2d'); 
    if (!context)
    { 
        alert('Error: failed to getContext!'); 
        return; 
    }

}
}

function draw()
{
    var sun = new Image();
    sun.src = 'images.sun.png';
    context.drawImage(sun, 100, 100);
}

onLoad();
draw(); 

Here's my HTML mabye this will help find the problem

<!doctype html>
    <html>
        <head>
            <title>Test</title> <script type="text/javascript" src="javascript/canvas.js"></script>
        </head>
        <body>
            <canvas id="canvas" width="800" height="600">
                Your browser does not support the canvas tag
            </canvas>

        </body>
    </html>

2 Answers 2

1

the problem is, that you declare canvas & context in onLoad() but try to access it in draw().

so, by making them global, the problem is solved:

var canvas;
var context;

if (window.addEventListener) {
    window.addEventListener('load', function(){
        onLoad();
    }, false);
}

function onLoad() {
    initialise();
    draw();
}

function initialise() {
    canvas = document.getElementById('canvas');

    if (!canvas.getContext) {
        alert('Error: no canvas.getContext!');
        return;
    }

    context = canvas.getContext('2d');
    if (!context) {
        alert('Error: failed to getContext!');
        return;
    }

}

function draw() {
    var sun = new Image();
    sun.addEventListener('load', function(){
        context.drawImage(sun, 100, 100);
    }, false);
    sun.src = 'http://puu.sh/1yKaY';
}
Sign up to request clarification or add additional context in comments.

6 Comments

I get the same error Uncaught TypeError: Cannot call method 'drawImage' of undefined
and you have a <canvas id="canvas" width=200" height="200"></canvas> or something like that in your body?
yea I'll add my HTML to the question but I'm pretty sure its all right
works for me? did you copy my complete JS into your canvas.js?
@partofweb Hi, consider adding a description of how the answer solves the problem as well as the code, to help future users of SO :)
|
0

Try to draw image on it's onload event

 sun.onload = function() {
    context.drawImage(sun, 100, 100);
 };

1 Comment

when i tried this chrome debugger game me this error Uncaught TypeError: Cannot call method 'drawImage' of undefined

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.