帆布填充颜色,保持黑色

0 geometry canvas colors fill

我有这个代码来绘制三角形画布.但是,除了黑色,我无法获得填充颜色.它声称可以使用ctx.fillStyle,但它没有.我必须在代码中遗漏一些东西你们可以看看吗?

  function drawShape(){
    // get the canvas element using the DOM
      var canvas = document.getElementById('balkboven');

    // Make sure we don't execute when canvas isn't supported
       if (canvas.getContext){

    // use getContext to use the canvas for drawing
       var ctx = canvas.getContext('2d');
       var ctxwidth = window.innerWidth;              
    // Filled triangle
       ctx.canvas.width  = window.innerWidth;
       ctx.beginPath();
       ctx.moveTo(0,0);
       ctx.lineTo(ctxwidth,0);
       ctx.lineTo(0,105);
       ctx.fill();
       ctx.fillStyle="red"

     }
}
Run Code Online (Sandbox Code Playgroud)

Sim*_*ris 5

fillStyle并且strokeStyle必须绘制对象之前设置,而不是之后!

可以把它想象成将油漆装到画笔上.你必须在用刷子抚摸之前这样做!

请参阅:http://jsfiddle.net/F8smR/