html5使用canvas畫(huà)三角形

字號(hào):


    <canvas id=canvas width=500 height=500 style=background-color: yellow;></canvas>
    代碼如下:
    var canvas=document.getelementbyid(canvas);
    var cxt=canvas.getcontext(2d);
    cxt.beginpath();
    cxt.moveto(250,50);
    cxt.lineto(200,200);
    cxt.lineto(300,300);
    cxt.closepath();//填充或閉合 需要先閉合路徑才能畫(huà)
    //空心三角形
    cxt.strokestyle=red;
    cxt.stroke();
    //實(shí)心三角形
    cxt.beginpath();
    cxt.moveto(350,50);
    cxt.lineto(300,200);
    cxt.lineto(400,300);
    cxt.closepath();
    cxt.fill();