javascript - HTML 画布线条被画成 gittery
问题描述
我的 HTML 画布有问题。当我快速移动这条线时,它会以一种乱七八糟的方式绘制,我可以看到多条线。我不知道这是否是帧速率和我的眼睛的问题,或者我做错了什么......这是我的代码:
const can = document.getElementById('can');
const ctx = can.getContext('2d');
can.width = innerWidth;
can.height = innerHeight;
function triangle(x1, y1, x2, y2){
ctx.strokeStyle = "blue";
ctx.lineWidth = "2";
ctx.beginPath();
ctx.moveTo(x1, y1);
ctx.lineTo(x2, y2);
ctx.stroke();
ctx.beginPath();
ctx.moveTo(x2, y2);
ctx.lineTo(x2, y2 + (y1 - y2));
ctx.strokeStyle = "#33cc33";
ctx.stroke();
ctx.beginPath();
ctx.moveTo(x2, y2 + (y1 - y2));
ctx.lineTo(x1, y1);
ctx.strokeStyle = "red";
ctx.stroke();
}
addEventListener('mousemove', (e)=>{
ctx.clearRect(0, 0, can.width, can.height);
triangle(can.width / 2, can.height / 2, e.clientX, e.clientY);
});
<canvas id="can"></canvas>
提前致谢!
PS:如果问题已经在其他地方回答了,我真的很抱歉,我搜索了一下,找不到它......
解决方案
推荐阅读
- ruby-on-rails - 如何解决将rails数据导出到csv的日文字符的编码问题?
- c++ - 为 C++ 映射做编号列表时的 int 或 index
- python - 如何在数据帧上使用类似 Count_values 的东西?
- javascript - 创建一个待办事项列表应用程序并停留在帖子上
- javascript - 打字稿反应 createContext 和 useState
- sql - 如何在 PostgreSQL 中设计特定于上下文的权限/角色系统?
- networking - 如何在 Windows10 中使用 Cuckoo 使用 Windump
- python - 特斯拉 api 股价
- php - 如何通过 FireFox Developer 设置和使用 FirePHP
- jquery - 如何在 Jquery Mobile 1.4 中调整项目列表的大小和位置