简体   繁体   English

在onmouseover和onmouseout上更新Canvas内容

[英]Updating Canvas content on onmouseover and onmouseout

I'm working on creating a game using HTML5 and in my work i'm usually update canvas content based on onmouseover and onmouseout . 我正在使用HTML5创建游戏,在我的工作中,我通常会根据onmouseover和onmouseout更新画布内容。 on onmouseover i'm doing that onmouseover我正在那样做

                    cxt.beginPath();
                    cxt.moveTo(10, 10);
                    cxt.lineTo(40, 40);
                    cxt.moveTo(40, 10);
                    cxt.lineTo(10, 40);
                    cxt.stroke();
                    cxt.closePath();
                    content[canvasNumber - 1] = 'Value';

now and on onmouseout event i want to clear the content of that canvas without overriding the current content !!, how can i do that ?! 现在和onmouseout事件我想清除画布的内容而不覆盖当前内容!!,我该怎么做?!

I think I know what you mean, but you have to be a little more careful with your language. 我想我知道你的意思,但你必须对你的语言更加小心。 You say: 你说:

without overriding the current content !! 没有覆盖当前内容!!

But the X you just drew is current content! 但你刚才画的X 当前的内容! I think what you really meant to say was "without clearing the content that was there before". 我认为你真正要说的是“没有清除以前的内容”。

Of course we'll always try our hardest to answer your questions, but please try very hard to use exact and careful language when asking them. 当然,我们总是会尽力回答您的问题,但在提问时请尽量使用准确而细致的语言。 English might not be your first language, and you probably speak it better than I speak my second language, but even if you're not sure of the words, even if you have to say the same thing in a few different ways, more language is worth it just to be sure we understand. 英语可能不是你的第一语言,你可能比我说第二语言说的更好,但即使你不确定这些词,即使你不得不用几种不同的方式说同样的话,更多的语言是值得的,只是为了确保我们理解。

Thanks! 谢谢! Onto the answer now. 现在在答案上。


If you want to clear the X without overriding the content that was there before, you have to save the content that was there before. 如果要在不覆盖之前的内容的情况下清除X,则必须保存之前的内容。

To save a canvas bitmap we can use what's called an "in-memory canvas" or "offscreen canvas." 为了保存画布位图,我们可以使用所谓的“内存中画布”或“屏幕外画布”。 You make these by using document.createElement and never adding them to the DOM 您可以使用document.createElement来创建它们,而不是将它们添加到DOM中

Here's a code example of saving the bitmap when mouseover occurs and restoring that bitmap when mouseout occurs: 下面是发生mouseover时保存位图的代码示例,并在发生mouseout时恢复该位图:

http://jsfiddle.net/simonsarris/uRXeN/ http://jsfiddle.net/simonsarris/uRXeN/

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

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