怎么用JS准确记录并绘制鼠标移动轨迹?
我在做一个画板功能,想实时记录鼠标移动的路径然后画出来,但发现轨迹特别卡顿,而且点太稀疏连不成线。我试过在 mousemove 里直接 push 坐标到数组,但效果很差。
有没有办法平滑一点地采集轨迹?或者是不是应该用 requestAnimationFrame 配合?下面是我目前的代码:
const points = [];
document.addEventListener('mousemove', (e) => {
points.push({ x: e.clientX, y: e.clientY });
// 然后用 canvas 把这些点连起来
});
暂无解答