Canvas 画布填充颜色,保持黑色

Canvas 画布填充颜色,保持黑色,canvas,colors,geometry,fill,Canvas,Colors,Geometry,Fill,我有这个代码来画一个三角形的画布。但是除了黑色以外,我无法获得填充颜色。据说它可以与ctx.fillStyle一起工作,但它不能。我的代码里一定漏了什么你们能看一下吗 function drawShape(){ // get the canvas element using the DOM var canvas = document.getElementById('balkboven'); // Make sure we don't execute when c

我有这个代码来画一个三角形的画布。但是除了黑色以外,我无法获得填充颜色。据说它可以与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"

     }
}

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

可以将其视为将油漆加载到画笔上。你必须在用刷子划水之前这样做


请参见:

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

可以将其视为将油漆加载到画笔上。你必须在用刷子划水之前这样做

见: