我尝试在一个彩虹笔绘画画布上作画,但每当我画线时,线条都呈现出点状。
只有当我移动得非常缓慢时,它才能正确显示。
“mousemove”事件监听器似乎无法检测到快速变化,或者我的代码存在其他问题吗? 此外,这是Codepen链接,如果有人想要一个可用的程序。 这是Codepen链接!
“mousemove”事件监听器似乎无法检测到快速变化,或者我的代码存在其他问题吗? 此外,这是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] = [e.x, e.y];
// 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(e.x, e.y);
ctx.stroke();
[x, y] = [e.x, e.y];
hue++;
}
canvas.addEventListener('mouseup', endDraw);
function endDraw() {
paint = false;
}
<canvas id="draw"></canvas>