HTML5 Canvas: How to border a fillRect?

后端 未结 3 810
太阳男子
太阳男子 2020-12-18 21:07

In HTML5, I want to make a fillRect() (with a white fill color) and a border (black). I don\'t want to use strokeRect() unless I can f

3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-18 21:54

    I tried on my own and it looks like this:

    var x = 100;
    var y = 100;
    var width = 50;
    var height = 50;
    
    var borderWidth = 5;   
    var offset = borderWidth * 2;
    
    c.beginPath();
    c.fillStyle = 'black';
    c.fillRect( x - borderWidth, y -borderWidth, width + offset, height + offset);
    c.fillStyle = 'green';
    c.fillRect( x, y, width, height);
    

提交回复
热议问题