简体   繁体   English

我如何清除画布?

[英]How do i clear a canvas?

By some starnge reason it does not work for me to do the 由于某些严重原因,我无法执行

canvas.width = canvas.width;

here is my code: 这是我的代码:

function startDraw(){
document.getElementById('PaintArea').setAttribute('onmousemove', 'getMouse(event)');
}
function stopDraw(){
document.getElementById('PaintArea').setAttribute('onmousemove', '');
}
function Paint(){
var Size = document.getElementById('Size').value;
var Opacity = document.getElementById('opa').value;
var color = document.getElementById('color').value;
canvas = document.getElementById('PaintArea');
if(canvas.getContext){
    var ctx = canvas.getContext('2d');

    ctx.fillStyle = color;
    ctx.globalAlpha = Opacity;
    ctx.beginPath();
    ctx.arc(musX-10, musY-10, Size, 0, Math.PI*2); 
    ctx.closePath();
    ctx.fill();
}
}

function clear(){       
canvas.width = canvas.width;
}

function getMouse(event) {
musY = event.clientY;
musX = event.clientX;
Paint();
}

button: 按钮:

<button onclick="clear()">Clear</button>

in the chrome console it says : "document.clear() is deprecated. This method doesn't do anything." 在chrome控制台中,它说:“不建议使用document.clear()。此方法不执行任何操作。”

i also have these global varables: 我也有这些全局变量:

var musX;
var musY;
var canvas; 

Guessing based on the error message... try this: 根据错误消息猜测...请尝试以下操作:

<button onclick="window.clear();">Clear</button>

If that works, consider using a less vague function name, something like clearCanvas() 如果clearCanvas() ,请考虑使用不太模糊的函数名称,例如clearCanvas()

 <button onclick="clear()">Clear</button> 

in the chrome console it says: "document.clear() is deprecated. This method doesn't do anything." 在chrome控制台中,它说:“反对使用document.clear()。此方法不执行任何操作。”

You have run into the legacy scope chain augmentation problem . 您已经遇到了遗留范围链扩充问题

In this case, clear is the name of a property of the object referred to by the host-defined document property, which is in the scope chain of the code in the event-handler attribute value. 在这种情况下, clear是主机定义的document属性所引用的对象的属性名称,该名称在事件处理程序属性值的代码范围链中。 In order to access your functions reliably (cross-browser), you need to circumvent the scope chain. 为了可靠地访问您的功能(跨浏览器),您需要绕开作用域链。

Possible solutions: 可能的解决方案:

  1. Make your functions methods of your own object: 使函数成为您自己的对象的方法:

     var myObject = { startDraw: function (…) { // … }, // … clear: function (…) { // … } }; … <button onclick="myObject.clear()">Clear</button> 
  2. Access the global object, whose methods your functions are, directly: 直接访问其功能所在的全局对象:

     var _global = this; // … function clear () { // … } … <button onclick="_global.clear()">Clear</button> 

    (You may choose any other variable identifier in place of _global as long as it does not collide with a built-in or host defined symbol. _global is an identifier unlikely to collide.) (您可以选择其他任何变量标识符来代替_global ,只要它不与内置符号或主机定义的符号发生冲突即可_global是不太可能发生冲突的标识符。)

Approach 1 is recommended as it reduces the number of user-defined global symbols, thus the likelihood of collision with built-in and host-defined symbols. 推荐使用方法1,因为它减少了用户定义的全局符号的数量,从而减少了与内置符号和主机定义符号冲突的可能性。

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

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