如何使用键码和 clearRect() 在 canvas 中修复此圆的绘制
How to fix the drawing of this circle in canvas using keycodes and clearRect()
我正在尝试使用键盘光标键在 Canvas 上制作圆圈动画。问题是 clearRect() 函数没有被正确使用,当你使用光标键时,有一条用圆圈绘制的奇怪的线。
我尝试重新排序包括 cleaRect() 函数的代码,但这没有帮助。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<script src="keycode.js" defer></script>
<title>Example Javascript</title>
</head>
<body onload="setup()">
<canvas width="1000" height="1000" style="border: 1px solid black">
Your browser does not support canvas.
</canvas><br>
<input type="button" value="Move">
</body>
</html>
这是javascript代码
console.log("Script is connected");
let canvas = window.document.querySelector("Canvas");
let ctx = canvas.getContext("2d");
let increment = 0;
function setup() {
ctx.arc(canvas.width / 2, canvas.height / 2, 100, 0, 2 * Math.PI);
ctx.fillStyle = "lightblue";
ctx.fill();
ctx.stroke();
window.addEventListener("keydown", function (event) {
if (event.keyCode === 37) {
ctx.clearRect(0, 0, 1000, 1000);
increment += 200;
ctx.arc(canvas.width / 2 - increment, canvas.height / 2, 100, 0, 2 * Math.PI);
ctx.fillStyle = "lightblue";
ctx.fill();
ctx.stroke();
console.log(increment);
}
if (event.keyCode === 38) {
ctx.clearRect(0, 0, 10000, 1000);
increment += 200;
ctx.arc(canvas.width / 2, canvas.height / 2 - increment, 100, 0, 2 * Math.PI);
ctx.fillStyle = "lightblue";
ctx.fill();
ctx.stroke();
console.log(increment);
}
if (event.keyCode === 39) {
ctx.clearRect(0, 0, 10000, 1000);
increment += 200;
ctx.arc(canvas.width / 2 + increment, canvas.height / 2, 100, 0, 2 * Math.PI);
ctx.fillStyle = "lightblue";
ctx.fill();
ctx.stroke();
console.log(increment);
}
if (event.keyCode === 40) {
ctx.clearRect(0, 0, 1000, 1000);
increment += 200;
ctx.arc(canvas.width / 2, canvas.height / 2 + increment, 100, 0, 2 * Math.PI);
ctx.fillStyle = "lightblue";
ctx.fill();
ctx.stroke();
console.log(increment);
}
}, true);
}
您对 clearRect() 的使用没有问题。问题是你的代码只有一个开放路径,每次按下一个键都会扩展它,所以当你调用 ctx.fill() 和 ctx.stroke() 时,它会遵循这条越来越长的路径。
将 beginPath() 和 closePath() 添加到您的每个案例中,您将不再获得
多余的线等等。
像这样:
ctx.beginPath();
ctx.arc(canvas.width / 2 - increment, canvas.height / 2, 100, 0, 2 * Math.PI);
ctx.closePath();
ctx.fillStyle = "lightblue";
ctx.fill();
ctx.stroke();
我正在尝试使用键盘光标键在 Canvas 上制作圆圈动画。问题是 clearRect() 函数没有被正确使用,当你使用光标键时,有一条用圆圈绘制的奇怪的线。
我尝试重新排序包括 cleaRect() 函数的代码,但这没有帮助。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<script src="keycode.js" defer></script>
<title>Example Javascript</title>
</head>
<body onload="setup()">
<canvas width="1000" height="1000" style="border: 1px solid black">
Your browser does not support canvas.
</canvas><br>
<input type="button" value="Move">
</body>
</html>
这是javascript代码
console.log("Script is connected");
let canvas = window.document.querySelector("Canvas");
let ctx = canvas.getContext("2d");
let increment = 0;
function setup() {
ctx.arc(canvas.width / 2, canvas.height / 2, 100, 0, 2 * Math.PI);
ctx.fillStyle = "lightblue";
ctx.fill();
ctx.stroke();
window.addEventListener("keydown", function (event) {
if (event.keyCode === 37) {
ctx.clearRect(0, 0, 1000, 1000);
increment += 200;
ctx.arc(canvas.width / 2 - increment, canvas.height / 2, 100, 0, 2 * Math.PI);
ctx.fillStyle = "lightblue";
ctx.fill();
ctx.stroke();
console.log(increment);
}
if (event.keyCode === 38) {
ctx.clearRect(0, 0, 10000, 1000);
increment += 200;
ctx.arc(canvas.width / 2, canvas.height / 2 - increment, 100, 0, 2 * Math.PI);
ctx.fillStyle = "lightblue";
ctx.fill();
ctx.stroke();
console.log(increment);
}
if (event.keyCode === 39) {
ctx.clearRect(0, 0, 10000, 1000);
increment += 200;
ctx.arc(canvas.width / 2 + increment, canvas.height / 2, 100, 0, 2 * Math.PI);
ctx.fillStyle = "lightblue";
ctx.fill();
ctx.stroke();
console.log(increment);
}
if (event.keyCode === 40) {
ctx.clearRect(0, 0, 1000, 1000);
increment += 200;
ctx.arc(canvas.width / 2, canvas.height / 2 + increment, 100, 0, 2 * Math.PI);
ctx.fillStyle = "lightblue";
ctx.fill();
ctx.stroke();
console.log(increment);
}
}, true);
}
您对 clearRect() 的使用没有问题。问题是你的代码只有一个开放路径,每次按下一个键都会扩展它,所以当你调用 ctx.fill() 和 ctx.stroke() 时,它会遵循这条越来越长的路径。
将 beginPath() 和 closePath() 添加到您的每个案例中,您将不再获得 多余的线等等。
像这样:
ctx.beginPath();
ctx.arc(canvas.width / 2 - increment, canvas.height / 2, 100, 0, 2 * Math.PI);
ctx.closePath();
ctx.fillStyle = "lightblue";
ctx.fill();
ctx.stroke();