简体   繁体   English

HTML5 canvas-控制在同一位置绘制时会发生什么

[英]HTML5 canvas - controlling what happens when drawing on same place

How can i control what happens when i draw a line in the same coordinates as previous line? 当我在与上一行相同的坐标中绘制一条线时,如何控制会发生什么? Currently the color becomes brighter, but i want it to stay the same (or more accurately - i want the second line to on top of the first line). 目前颜色变亮,但我希望它保持不变(或更准确地说-我希望第二行位于第一行的顶部)。

I tried to do: 我试着做:

_context.setGlobalCompositeOperation(Composite.SOURCE_OVER);

But it seems to work only when drawing canvas on canvas, not when drawing on the same canvas. 但这似乎仅在在画布上绘制画布时有效,而在同一画布上绘制时则无效。

Supposing your color has a full alpha, your problem is probably related to the fact you draw lines of width 1 at integer coordinates. 假设您的颜色具有完整的alpha,则您的问题可能与您在整数坐标处绘制宽度为1的线有关。

Look at the following fiddle : http://jsfiddle.net/RAgak/ 请看以下小提琴: http : //jsfiddle.net/RAgak/

Drawing at integer coordinates makes the line wider and fuzzy. 以整数坐标绘图会使线变宽且模糊。 And drawing a second time makes it brighter. 再次绘制会使其更亮。 But this doesn't happen when I draw the line at half-integer coordinates. 但是,当我在半整数坐标处绘制直线时,不会发生这种情况。

var y = 10;
c.beginPath();
c.moveTo(0, y);
c.lineTo(30, y);
c.stroke(); // fuzzy
c.beginPath();
c.moveTo(50, y+0.5);
c.lineTo(80, y+0.5);
c.stroke(); // ok

This is due to the fact lines are drawn over all pixels they're over (on canvas positionning is in float). 这是因为事实是,线条绘制在它们结束的所有像素上(在画布上定位是浮动的)。 When you want to draw precise vertical or horizontal lines in javascript on a canvas, you'd better have them in half ints. 当您想在画布上的javascript中绘制精确的垂直或水平线时,最好将其设置为半整数。

See illustration : The first horizontal line was drawn with ay position of 1. This line is fuzzy and wide. 参见插图:以y位置1绘制了第一条水平线。这条线是模糊且宽的。 The second horizontal line was drawn with ay position of 4.5. 用4.5的y位置绘制第二条水平线。 It is thin and precise. 它既薄又精确。

在此处输入图片说明

The solution, at least when drawing horizontal or vertical lines (or rects), is to take the width of the line into account and draw at integer or half-integer coordinates. 至少在绘制水平或垂直线(或矩形)时,解决方案是考虑线的宽度并以整数或半整数坐标进行绘制。

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

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