简体   繁体   English

画布上的画线点缀着

[英]Drawing lines on canvas are dotted

I was trying to work on a rainbow-pen drawing canvas, but whenever I draw, the lines appear dotted. 我试图在彩虹笔画画布上工作,但每当我画画时,线条都会出现点缀。 Only when I move really slowly, does it appear properly. 只有当我移动得非常慢时才能正常显示。

The 'mousemove' event listener, isn't really being able to detect fast changes or is there some other issue in my code? 'mousemove'事件监听器,实际上无法检测到快速更改,或者我的代码中是否存在其他问题? Also, here is the codepen link, if anyone wants a working program. 此外,这里是codepen链接,如果有人想要一个工作程序。

Here is the codepen! 这是codepen!

 const canvas = document.querySelector('#draw'); canvas.width = window.innerWidth; canvas.height = window.innerHeight; var ctx = canvas.getContext("2d"); ctx.lineWidth = 50; ctx.lineCap = "round"; ctx.lineJoin = "line"; ctx.strokeStyle = 0; let hue = 0; var [x, y] = [0, 0]; let paint = false; canvas.addEventListener('mousedown', beginDraw); function beginDraw(e) { paint = true; [x, y] = [ex, ey]; // ctx.moveTo(x, y); } canvas.addEventListener('mousemove', draw); function draw(e) { if (paint == false) return; ctx.strokeStyle = `hsla(${hue}, 100%, 50%, 0.5)`; ctx.beginPath(); ctx.moveTo(x, y); ctx.lineTo(ex, ey); ctx.stroke(); [x, y] = [ex, ey]; hue++; } canvas.addEventListener('mouseup', endDraw); function endDraw() { paint = false; } 
 <canvas id="draw"></canvas> 

I think the problem is opacity in your hsla function for color. 我认为问题是你的hsla函数的颜色不透明。 Because it is set to 0.5 you have some transparency and because you are drawing a line for each mousemove you have for each mouse move event start and end point for your drawing. 因为它设置为0.5,所以您有一些透明度,因为您为每个鼠标移动绘制一条线,用于绘制每个鼠标移动事件的起点和终点。 Sometimes these points overlaps each other. 有时这些点相互重叠。

You can remove transparency and keep it to 1. In such case you are no longer see places where 2 dots are drawn one above another making color more visible to others. 您可以删除透明度并将其保持为1.在这种情况下,您不再会看到一个上方绘制2个点的位置,使其他人更容易看到颜色。

ctx.strokeStyle = `hsla(${hue}, 100%, 50%, 1)`;

In case you dont like to see dots when color changes you will need to make more color values also playing with other parameters not only hue parameter, because color step may be to large and you may see edges when color is changed. 如果你不想在颜色变化时看到点,你需要制作更多颜色值,同时使用其他参数而不仅仅是色调参数,因为颜色步长可能很大,你可能会在颜色变化时看到边缘。

The mousemove event handler fires only so many times per second. mousemove事件处理程序每​​秒只触发很多次。 On each execution, your code draws a line from the previous mouse location to the current mouse location. 在每次执行时,您的代码都会从上一个鼠标位置绘制一条线到当前鼠标位置。 Each subsequent line partially overlaps the previous line. 每个后续行部分重叠前一行。

For slow strokes, the overlap is almost 100% so you won't see the effect, but for faster strokes, the overlap is shown as a circle. 对于慢速笔划,重叠几乎为100%,因此您将看不到效果,但对于更快的笔划,重叠显示为圆形。 Since you're using partial transparency, the spots where the lines overlap become darker and this causes the "dotted" effect you are seeing. 由于您使用的是局部透明度,因此线条重叠的点变得更暗,这会导致您看到的“虚线”效果。

If you set your opacity to 1, the effect will go away. 如果将不透明度设置为1,效果将消失。

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

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