admin管理员组文章数量:1429515
Earlier on I noticed that strokeRect
(and any other method that involved stroke such as lineTo
) created a grey 2 px wide line instead of a 1px wide black line. After some Google searching I found that context.translate(0.5, 0.5)
fixed this. but now fillRect
(and like before any other method that involves fill
) creates a black box with a grey border around it.
Does anyone know a good way to make it so that both fillRect and strokeRect have crisp edges with no grey borders? I also don't know whether or not I should use context.translate(0.5, 0.5)
for images, as it seems like SVGs have crisp edges regardless of whether or not I translate.
Here is a jsfiddle demonstrating this: /
Note that the bottom strokeRect is crisp while the top one is blurry, and the top fillRect is crisp while the bottom one is blurry.
Earlier on I noticed that strokeRect
(and any other method that involved stroke such as lineTo
) created a grey 2 px wide line instead of a 1px wide black line. After some Google searching I found that context.translate(0.5, 0.5)
fixed this. but now fillRect
(and like before any other method that involves fill
) creates a black box with a grey border around it.
Does anyone know a good way to make it so that both fillRect and strokeRect have crisp edges with no grey borders? I also don't know whether or not I should use context.translate(0.5, 0.5)
for images, as it seems like SVGs have crisp edges regardless of whether or not I translate.
Here is a jsfiddle demonstrating this: http://jsfiddle/Tysonzero/ydm21pkt/1/
Note that the bottom strokeRect is crisp while the top one is blurry, and the top fillRect is crisp while the bottom one is blurry.
Share Improve this question edited Jan 21, 2015 at 5:09 semicolon asked Jan 21, 2015 at 0:58 semicolonsemicolon 2,58031 silver badges37 bronze badges 3-
They definitely are integers. I generally use
(100, 100, 100, 100)
or similar. – semicolon Commented Jan 21, 2015 at 1:44 - 1 Is your actual canvas size scaled in css? – Damon Smith Commented Jan 21, 2015 at 3:14
- What do you mean? I don't directly set the canvas size in CSS, I set the width and the height in JavaScript and it auto re-sizes. I can assure you none of that stuff is the issue. Here is the kind of thing I am talking about. – semicolon Commented Jan 21, 2015 at 4:52
1 Answer
Reset to default 3Strokes draw half-inside & half-outside the x,y coordinates. That's why you are seeing the blur with integer x,y and why it clears up when the x,y are offset by a half pixel. Here's more on why the blur occurs: http://www.mobtowers./html5-canvas-crisp-lines-every-time/
An easy way to make rects crisper is to add methods to your context instance that offset strokeRect & fillRect for best appearance:
var canvas=document.getElementById("canvas");
var context=canvas.getContext("2d");
var cw=canvas.width;
var ch=canvas.height;
// add pixel aligned versions of strokeRect & fillRect to this context instance
context.sRect=function(x,y,w,h){
x=parseInt(x)+0.50;
y=parseInt(y)+0.50;
this.strokeRect(x,y,w,h);
}
context.fRect=function(x,y,w,h){
x=parseInt(x);
y=parseInt(y);
context.fillRect(x,y,w,h);
}
context.strokeStyle = "#000000";
context.fillStyle = "#000000";
context.strokeRect(100, 50, 100, 100);
context.fillRect(300.5, 50.5, 100, 100);
context.sRect(100,200,100,100);
context.fRect(300.5,200,100,100);
context.fillText('Unadjusted',20,100);
context.fillText('Adjusted',20,250);
body{ background-color: ivory; padding:10px; }
#canvas{border:1px solid red;}
<canvas id="canvas" width=500 height=500></canvas>
本文标签: htmlJavaScript either strokeRect or fillRect blurry depending on translationStack Overflow
版权声明:本文标题:html - JavaScript either strokeRect or fillRect blurry depending on translation - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745454751a2659049.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论