I am developing a paint program using an HTML5 canvas. I created a drawing tool in which the user drags and moves the mouse.
I have a listener on a mousemove event that draws short lines:
Painter.mainCanvas.beginPath(); Painter.mainCanvas.moveTo(Painter.lastX, Painter.lastY); Painter.lastX = e.offsetX; Painter.lastY = e.offsetY; Painter.mainCanvas.lineTo(Painter.lastX, Painter.lastY); Painter.mainCanvas.stroke();
Everything worked fine until I set the global alpha to <1. When using this method to draw, the endpoint also starts with a dot. Thus, the point is drawn twice. And since we have a transparent color, the dot now has a different color with different dots in the row.
I tried another method when when running mousemove it uses lineTo() and stroke() when mouseup fires.
This solves the double-drawing problem, but also introduces a new problem: when the user intends to draw the same point twice, i.e. cross line without mouse, the point will not be displayed twice. Since the lineTo() function will not draw a point twice without an interval between them.
html5 vector-graphics canvas
liuyanghejerry
source share