【发布时间】:2022-08-24 03:43:17
【问题描述】:
我不明白为什么这段代码不起作用。它应该只绘制一个覆盖屏幕的白色矩形。然后随机放置一个蓝点并等待循环完成。然后通过再次绘制白色矩形并关闭点然后重新绘制它来重复循环。
<!DOCTYPE html>
<html lang=\"en-US\">
<head>
<meta charset=\"UTF-8\"/>
<title>Peripheral vision checker</title>
<script type=\"application/javascript\">
function draw() {
// draw crosshairs
var onFor = 1000;
const intervalID = setInterval(mytimer, onFor);
function mytimer()
{
// draw white rects
function getRandomInt(max) {
return Math.floor(Math.random() * max);
}
var x = 1280;//will be equal to window height
var y = 720;//will be equal to window width
const canvas = document.getElementById(\'canvas\');
const ctx = canvas.getContext(\'2d\');
ctx.fillStyle = \'white\';
var xcoor =getRandomInt(x);
var ycoor =getRandomInt(y);
ctx.fillRect(0, 0, x, y);
ctx.fillStyle = \'blue\';
var radius = 10;
moveTo(xcoor,ycoor);
ctx.arc(xcoor, ycoor, radius, 0, 2 * Math.PI);
//console.log(xcoor + \' \' + ycoor);//just temporary, to see they work
ctx.fill();
}
}
</script>
</head>
<h1>Peripheral vision checker</h1>
<body onload=\"draw();\">
<canvas id=\"canvas\" width=\"1280\" height=\"720\"></canvas>
</body>
</html>
标签: javascript html canvas