【发布时间】:2016-11-01 05:50:57
【问题描述】:
不知道为什么我的 clearRect() 在我的“重置”中不起作用。然而,它适用于我的“向上”功能。 (我的'up'函数用于清除画布,然后将图像绘制+平移10像素)
分配是在画布上创建一个对象,该对象可以通过用户的按钮单击来移动。其中一个按钮必须是清除画布并在画布中心重绘图像的重置按钮。
下面是我的代码。运动应该有 4 个功能,但我在这里只包括了“向上”运动功能以作为一个总体思路:
var surface = document.getElementById("drawingSurface");
var ctx = surface.getContext("2d");
var reset = function () {
alert("Testing"); // <--- This works
ctx.clearRect(0, 0, 499, 499); // <--- But this is not working
ctx.beginPath();
ctx.strokeRect(200, 200, 100, 100);//Face
ctx.fillRect(220, 225, 15, 15);//Left eye
ctx.fillRect(265, 225, 15, 15);//Right eye
ctx.fillRect(245, 250, 10, 10); // Nose
ctx.fillRect(210, 185, 20, 15); // Left ear
ctx.fillRect(270, 185, 20, 15); // Left ear
ctx.closePath();
};
var up = function () {
ctx.clearRect(0, 0, 499, 499);
ctx.beginPath();
ctx.translate(0, -10);
ctx.strokeRect(200, 200, 100, 100);//Face
ctx.fillRect(220, 225, 15, 15);//Left eye
ctx.fillRect(265, 225, 15, 15);//Right eye
ctx.fillRect(245, 250, 10, 10); // Nose
ctx.fillRect(210, 185, 20, 15); // Left ear
ctx.fillRect(270, 185, 20, 15); // Left ear
ctx.closePath();
};
这是我的 HTML:
<canvas id="drawingSurface" width="500" height="500" style="border: 1px
solid black; background-color: #FFF;">Cat</canvas>
<br>
<button onclick="reset();">RESET</button>
<button onclick="up();">MOVE UP</button>
<button onclick="left();">MOVE LEFT</button>
<button onclick="right();">MOVE RIGHT</button>
<button onclick="down();">MOVE DOWN</button>
【问题讨论】:
-
运行代码时返回错误:'reset is not a function'。尝试将其更改为
function reset或window.reset = function
标签: javascript function html5-canvas