【发布时间】:2018-03-28 21:01:42
【问题描述】:
我正在尝试创建一个简单的纯 JS 程序,用于在画布上绘图。我现在有一个可以接受的解决方案,但是如果我画得很快,我的绘图笔就会不连续,我会出现零散的圆圈,可能是因为电脑跟不上节奏。
var draw = false;
function yesDraw() {
draw = true;
}
function mouseCoordinates(e) {
if (draw) {
var x = e.offsetX;
var y = e.offsetY;
drawing(x, y);
}
}
function noDraw() {
draw = false;
}
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
function drawing(x, y) {
ctx.beginPath();
ctx.arc(x, y, 10, 0, 2 * Math.PI);
ctx.fillStyle = "black";
ctx.fill();
}
<canvas id="myCanvas" height="400" ; width="1000" onmousedown="yesDraw()" onmousemove="mouseCoordinates(event)" onmouseup="noDraw()" onmouseout="noDraw()" style="border: solid 1px black;">Your browser does not support canvas.</canvas>
有没有什么办法可以让画图持续流,并且仍然保持 100% JS?
【问题讨论】:
标签: javascript html canvas drawing