0

簡単な家を描くことになっているこのコードがありますが、実行しても何も表示されず、何かが間違っているという警告も表示されません。誰もが理由を知っていますか?

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(); 
}
4

1 に答える 1

0

コードは正常に動作するはずですが、onLoad() 関数を呼び出すのを忘れていると思いますので、何も起こりません

onLoad() を変更します

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

(そして、家は描かず、屋根だけを描きます ;-) )

于 2012-12-07T09:26:06.053 に答える