简体   繁体   English

无法为html5画布上的矩形着色背景

[英]unable to make background colored for a rectange on html5 canvas

I added this lines in order to make color of border and background GREEN for a rectangle, but without success: 我添加以下行是为了使边框的颜色和背景的绿色变为矩形,但没有成功:

context.fillStyle = 'green';
context.fill();
context.strokeStyle = 'green';
context.stroke();

Here my code in JSFIDDLE: https://jsfiddle.net/f5z8qtcp/1/ 这是我在JSFIDDLE中的代码: https ://jsfiddle.net/f5z8qtcp/1/

How to make the green rectangle colored too in the background ... grey rectangle while creating it must be like it is, i want just to color the result (Border already colored to green). 如何在背景中使绿色矩形也着色...在创建灰色矩形时一定是这样,我只想对结果着色(边框已经着色为绿色)。

Thank's. 谢谢。

Use fillRect function: 使用fillRect函数:

function drawAll(){
  ctx.clearRect(0, 0, canvas.width, canvas.height);
  ctx.lineWidth=1;
  ctx.strokeStyle='green';
  ctx.fillStyle = 'green';
  for(var i=0;i<rects.length;i++){
    var r=rects[i];
    ctx.strokeRect(r.left,r.top,r.right-r.left,r.bottom-r.top);
    ctx.fillRect(r.left,r.top,r.right-r.left,r.bottom-r.top);
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM