简体   繁体   English

html5 canvas:clearRect()在函数内部不起作用

[英]html5 canvas: clearRect() not working inside function

Not sure why my clearRect() is not working inside my 'reset'. 不知道为什么我的clearRect()在'reset'中不起作用。 It however works in my 'up' function. 但是它在我的“向上”功能中起作用。 (My 'up' function is used to clear the canvas and then draw + translate the image up 10 pixels) (我的“向上”功能用于清除画布,然后将图像向上绘制+转换10个像素)

Assignment is to create an object on canvas that is able to be moved around through the user's button clicks. 分配是在画布上创建一个对象,该对象可以通过用户单击按钮来移动。 One of the buttons have to be a reset button which clears the canvas and redraws the image at the center of the canvas. 按钮之一必须是重置按钮,该按钮可以清除画布并在画布的中央重绘图像。

Here is my code below. 这是下面的代码。 There are supposed to be 4 functions for the movements but I only included the 'up' movement function here for a general idea: 应该有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();
};

Here is my HTML: 这是我的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>

I believe the issue is that the translation applied in up is persisting. 我认为,问题是,在应用的翻译up一直持续。 So the clear is firing, but then immediately redrawing the same image (try commenting out the draw functions in reset ). 因此clear是可行的,但是随后立即重绘相同的图像(尝试在reset注释掉绘图功能)。

To reset the translation, you can use ctx.setTransform(1, 0, 0, 1, 0, 0); 要重置翻译,可以使用ctx.setTransform(1, 0, 0, 1, 0, 0); , placing it somewhere before the drawing calls in reset . ,将其放置在图形调用reset之前的某个位置。

您的代码确实起作用,只是reset函数在up()放置它的最后位置重绘了该表面。

Along the same lines of what everyone else is saying. 其他人都在说同样的话。 You are redefining the coordinate system on your canvas each time you call translate. 每次调用平移时,您将在画布上重新定义坐标系。 The function is firing but it is not doing what you expect because the coordinates have been moved. 该函数正在触发,但未执行预期的操作,因为坐标已移动。 A way to keep track of that is to increment a variable outside the function everytime you shift the y coordinates and then add it back to when you "reset" the image. 跟踪此问题的一种方法是,每当您移动y坐标时在函数外部增加一个变量,然后在“重置”图像时将其重新添加回去。

 var surface = document.getElementById("drawingSurface"); var ctx = surface.getContext("2d"); var yShift = 0; function reset() { ctx.clearRect(0, 0, 499, 499); // <--- But this is not working ctx.beginPath(); ctx.strokeRect(200, 200+yShift, 100, 100);//Face ctx.fillRect(220, 225+yShift, 15, 15);//Left eye ctx.fillRect(265, 225+yShift, 15, 15);//Right eye ctx.fillRect(245, 250+yShift, 10, 10); // Nose ctx.fillRect(210, 185+yShift, 20, 15); // Left ear ctx.fillRect(270, 185+yShift, 20, 15); // Left ear ctx.closePath(); }; var up = function () { ctx.clearRect(0, 0, 499, 499); ctx.beginPath(); ctx.translate(0,-10); yShift += 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(); }; 
 <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> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM