开发者

canvas fill color, stays black

I have this code to draw a triangular canvas. But I can't manage to get the fill color anything other then black. It's stated to work with the ctx.fillStyle but it doesn't. I must be missing something in my code can you guys have a look?

  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 and strokeStyle have to be set before you draw the object, not afterwards!

Think of it as loading paint onto a paintbrush. You must do this before you stroke with the brush!

See: http://jsfiddle.net/F8smR/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜