简体   繁体   English

你如何清除javascript中的画布?

[英]How do you clear canvases in javascript?

https://codepen.io/MabelJane/pen/ROjONN https://codepen.io/MabelJane/pen/ROjONN

Hello, I have been creating a green screen algorithm on code pen which I have put a link to above. 您好,我一直在代码笔上创建一个绿屏算法,我已经在上面添加了一个链接。 Most of it seems to be working except from the JavaScript that is supposed to clear the canvases. 除了应该清除画布的JavaScript之外,大部分内容似乎都在工作。 I think it will probably be something quite simple but I can't work it out. 我认为它可能会非常简单,但我无法解决这个问题。 I put the code that I think is what doesn't work below. 我把我认为的代码放在下面不起作用。 Thank you 🙂 谢谢你

function clear() {
    var fgCanvas = document.getElementById("can1");
    var bgCanvas = document.getElementById("can2");
    var fgContext = fgCanvas.getContext("2d");
    var bgContext = bgCanvas.getContext("2d");
    fgContext.clearRect(0, 0, fgCanvas.width, fgCanvas.height);
    bgContext.clearRect(0, 0, bgCanvas.width, bgCanvas.height);
}

It seems that Javascript doesn't like the function name you're using - clear() - thus the function won't ever be called. 似乎Javascript不喜欢你正在使用的函数名 - clear() - 因此函数不会被调用。 Try renaming the function to clearIt() or something like that. 尝试将函数重命名为clearIt()或类似的东西。 Apart from that your code for cleaning the canvases is correct. 除此之外,您的清洁画布的代码是正确的。

Type clear in your console: 在控制台中键入clear:

clear
ƒ clear() { [Command Line API] }

clear function is basically for clearing console terminal as far as I know, so just rename it or create an object and declare clear method as a property instead of declaring it for window object. clear功能基本上是为了清除控制台终端,据我所知,所以只需重命名它或创建一个对象并声明clear方法作为属性而不是为window对象声明它。

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

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