简体   繁体   中英

lineTo not woking

Ok I have this piece of code which is supposed to be drawing a simple house but when i run it I get nothing and no warnings that something is wrong with it. anyone know why?

function onLoad() 
{ 
    var canvas; 
    var context;
function initialise () 
{
    canvas = document.getElementById('canvas'); 
    if (!canvas) 
    { 
        alert('Error: I cannot find the canvas element!'); 
        return; 
    }
    if (!canvas.getContext) 
    { 
        alert('Error: no canvas.getContext!'); 
        return; 
    }
    context = canvas.getContext('2d'); 
    if (!context) 
    { 
        alert('Error: failed to getContext!'); 
        return; 
    } 
}

function draw()
{
    context.beginPath();
    context.moveTo(150,100);
    context.lineTo(250,200);
    context.lineTo(250,300);
    context.lineTo(50,300);
    context.lineTo(50,200);
    context.lineTo(150,100);
    context.closePath();
    context.stroke();
}

initialise();
draw(); 
}

the code should work fine, but I think you forget to call your onLoad() function, so nothing can happen

change onLoad() with

window.onload = function () {
... //your code
}

(and it will not draw a house, just the roof ;-) )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM