简体   繁体   English

html5 canvas 描边颜色总是显示为灰色?

[英]html5 canvas stroke color always displaying as grey?

I've got an array like this:我有一个这样的数组:

var hitColors = ["#ff0000","#00ff00","#0000ff","#ffff00","#00ffff","#ff00ff"];

I've got a canvas that I'm "redrawing" every few seconds like this:我有一个 canvas 我每隔几秒钟就“重绘”一次,如下所示:

// main canvas rectangle
context.beginPath();
context.rect(0, 0, canvasWidth, canvasHeight);
context.fillStyle = '#FFFFFF';
context.fillRect(0, 0, canvasWidth, canvasHeight);

context.rect(thisXPos-1, thisYPos-1, words[activeWord][2].width+2, words[activeWord][2].height+2);
context.strokeStyle = hitColors[hitSpot];

alert('"' + hitColors[hitSpot] + '"');
alert(context.strokeStyle);

context.lineWidth = 1;
context.stroke();
context.closePath();

I can confirm that context.closePath();我可以确认 context.closePath(); is returning the proper color from the array but when I alert context.StrokeStyle it's always set to #000000 and the rect border is grey.正在从数组中返回正确的颜色,但是当我提醒 context.StrokeStyle 它总是设置为 #000000 并且矩形边框是灰色的。 How can I fix this?我怎样才能解决这个问题?

Add or subtract 0.5 pixels from your values.从您的值中添加或减去 0.5 个像素。

Basically, if you try to draw a 1px line centered around an integer pixel value what you end up with is a 2 pixel line centered around that point which and the line will be semi-transparent.基本上,如果您尝试绘制以 integer 像素值为中心的 1px 线,您最终会得到一条以该点为中心的 2 像素线,该线将是半透明的。 Semi-transparent black looks like grey.半透明的黑色看起来像灰色。 So, if you want a straight line of any colour that is exactly 1 pixel wide, you need to draw that line with at pixel intervals of 0.5.因此,如果您想要一条正好为 1 像素宽的任何颜色的直线,则需要以 0.5 的像素间隔绘制该直线。

I switched the array to this:我将数组切换为:

var hitColors = ["#f00","#0f0","#00f","#ff0","#0ff","#f0f"];

and it started working properly.它开始正常工作。

You never set you strokeStyle.你永远不会设置你的strokeStyle。 its defaulting to #000000.它默认为#000000。

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

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