Line drawing process
- Call beginPath, set the line style, and start the drawing two。 Call lineTo to specify path coordinates
- Call stroke to finish drawing
API
- BeginPath Start a drawing
- MoveTo Set the brush start point
set the starting point and draw the line segment; if you do not set the starting point, draw the point.
- LineTo Set brush end point
- Stroke Perform drawing
Draw lines on the canvas with the mouse
const ctx = canvas.getContext("2d")
let lock = false
const canvasDown = () => {
lock = true
ctx.beginPath()
ctx.strokeStyle = options.color
ctx.shadowBlur = options.scale
ctx.shadowColor = options.color
}
const canvasMove = ({ offsetX, offsetY }) => {
if (!lock) return
ctx.lineTo(offsetX, offsetY)
ctx.stroke()
}
const canvasUp = () => {
lock = false
}
canvas.addEventListener("mousedown", canvasDown)
canvas.addEventListener("mousemove", canvasMove)
canvas.addEventListener("mouseup", canvasUp)