Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
需要使用此函数使用javascript销毁画布上的项目的帮助吗_Javascript_Html_Canvas_Mootools - Fatal编程技术网

需要使用此函数使用javascript销毁画布上的项目的帮助吗

需要使用此函数使用javascript销毁画布上的项目的帮助吗,javascript,html,canvas,mootools,Javascript,Html,Canvas,Mootools,我使用mootools和mootools画布库编写了以下代码 CANVAS.init({ canvasElement : 'canvas', interactive : true }); var itemlayer = CANVAS.layers.add({ id : 'items' }); for(var j = 0; j < 5; j++) { for(var i = 0; i < 5; i++) { itemlayer.add({ id : 'i

我使用mootools和mootools画布库编写了以下代码

CANVAS.init({ canvasElement : 'canvas', interactive : true });

var itemlayer = CANVAS.layers.add({ id : 'items' });

for(var j = 0; j < 5; j++)
{
for(var i = 0; i < 5; i++)
{

    itemlayer.add({

        id : 'item-'+i + '-' + j,
        x : 51 * i,
        y : 51 * j,
        w : 50,
        h : 50,
        state : 'normal',
        interactive : true, //although they have no interactive events!
        colors : { normal : '#f00', hover : '#00f' },
        events : {
            onDraw : function(ctx){

                    ctx.fillStyle = this.colors[this.state];
                    ctx.fillRect(this.x,this.y,this.w,this.h);

                    this.setDims(this.x,this.y,this.w,this.h);
            }
        }

    });


}

}

CANVAS.addThread(new Thread({
            id : 'myThread',
            onExec : function(){
                    CANVAS.clear().draw();
            }
    }));

问题是,无论我怎么做,我都无法从画布上销毁对象!正确的方法是什么


请参阅

我不熟悉mootools的内部结构,但它看起来像item.destroy()只发送事件,而不是实际销毁画布上的项目。画布仅仅是一个位图——它不像SVG那样具有各个原语的层或记录

您需要自己实际销毁矩形,方法是在没有矩形的情况下重新绘制画布、在其上绘制或使用:

ctx.clearRect(this.x, this.y, this.w, this.h);
这将清除画布上由参数定义的矩形内的所有信息


编辑:在进行一点阅读之后,看起来您将destroy()函数传递给画布项的构造函数。此函数必须包含要销毁项目的任何代码(clearRect、fillRect,无论什么)。

我不熟悉mootools的内部结构,但它看起来像item.destroy()只发送事件,而不是实际销毁画布上的项目。画布仅仅是一个位图——它不像SVG那样具有各个原语的层或记录

您需要自己实际销毁矩形,方法是在没有矩形的情况下重新绘制画布、在其上绘制或使用:

ctx.clearRect(this.x, this.y, this.w, this.h);
这将清除画布上由参数定义的矩形内的所有信息


编辑:在进行一点阅读之后,看起来您将destroy()函数传递给画布项的构造函数。此函数必须包含要销毁项目的任何代码(clearRect、fillRect等)。

Mootools是一个基于类的JavaScript框架。为了使用它,您需要像调用类而不是直接调用构造函数一样调用对象

画布库是该规则的一个例外,因为它是一个“静态”类。但是,在使用层方法之前,需要初始化层类

使用图层类的add方法时,要求将新项目添加到该图层类中。您可以使用此方法两次,一次在循环之前,一次在循环内部。但是,在任何时候都不能初始化图层类。因此,我假设在循环初始化类之前是这种情况。这需要替换为
var itemlayer=new Layer('items')

在循环中使用
itemlayer.add
时,您将和对象传递到层对象,然后层对象将自动为您创建CanvasItem对象。然后它将这些对象返回给您。由于destroy方法是CanvasItem类的成员,因此需要在变量中捕获它们才能调用它。因为它发生在循环内部,所以如果希望删除循环外部的对象,则需要一个数组

CANVAS.init({ canvasElement : 'canvas', interactive : true });

var itemlayer = new Layer('items');
var itemArray = new Array;
for(var j = 0; j < 5; j++)
{
for(var i = 0; i < 5; i++)
{

    item Array[j][i] = itemlayer.add({

        id : 'item-'+i + '-' + j,
        x : 51 * i,
        y : 51 * j,
        w : 50,
        h : 50,
        state : 'normal',
        interactive : true, //although they have no interactive events!
        colors : { normal : '#f00', hover : '#00f' },
        events : {
            onDraw : function(ctx){

                    ctx.fillStyle = this.colors[this.state];
                    ctx.fillRect(this.x,this.y,this.w,this.h);

                    this.setDims(this.x,this.y,this.w,this.h);
            }
        }

    });


}
CANVAS.init({canvasElement:'CANVAS',interactive:true});
var itemlayer=新图层(“项目”);
var itemArray=新数组;
对于(var j=0;j<5;j++)
{
对于(变量i=0;i<5;i++)
{
项目数组[j][i]=itemlayer.add({
id:'项目-'+i+'-'+j,
x:51*i,
y:51*j,
w:50,
h:50,
声明:“正常”,
interactive:true,//尽管它们没有交互事件!
颜色:{normal:'#f00',悬停:'#00f'},
活动:{
onDraw:函数(ctx){
ctx.fillStyle=this.colors[this.state];
ctx.fillRect(this.x,this.y,this.w,this.h);
this.setDims(this.x,this.y,this.w,this.h);
}
}
});
}
然后,当您希望销毁某个项目时,只要知道其j和i索引,就可以使用
itemArray[j][i].destroy()将其删除


最后,请记住,销毁方法仅记录为触发CanvasItem的销毁事件。根据库实现的内容,您可能需要编写自己的销毁事件,以便将其从画布中实际删除。

Mootools是一个基于类的JavaScript框架。为了使用它,您需要调用对象如果它们是类而不是直接的构造函数,则为

画布库是该规则的一个例外,因为它是一个“静态”类。但是,在使用层方法之前,需要初始化层类

当您使用Layer类的add方法时,您要求将新项添加到该Layer类中。您使用此方法两次,一次在循环之前,一次在循环内部。但是,在任何时候都不会初始化Layer类。因此,我假设在循环之前应该初始化该类。这需要重新设置放置在
var itemlayer=新层('items');

在循环中使用
itemlayer.add
时,您将and对象传递给Layer对象,然后Layer对象会自动为您创建CanvasItem对象。然后,它会将这些对象返回给您。由于destroy方法是CanvasItem类的一个成员,您需要在变量中捕获它们才能调用它。因为它发生了在循环内部,如果希望删除循环外部的对象,则需要一个数组

CANVAS.init({ canvasElement : 'canvas', interactive : true });

var itemlayer = new Layer('items');
var itemArray = new Array;
for(var j = 0; j < 5; j++)
{
for(var i = 0; i < 5; i++)
{

    item Array[j][i] = itemlayer.add({

        id : 'item-'+i + '-' + j,
        x : 51 * i,
        y : 51 * j,
        w : 50,
        h : 50,
        state : 'normal',
        interactive : true, //although they have no interactive events!
        colors : { normal : '#f00', hover : '#00f' },
        events : {
            onDraw : function(ctx){

                    ctx.fillStyle = this.colors[this.state];
                    ctx.fillRect(this.x,this.y,this.w,this.h);

                    this.setDims(this.x,this.y,this.w,this.h);
            }
        }

    });


}
CANVAS.init({canvasElement:'CANVAS',interactive:true});
var itemlayer=新图层(“项目”);
var itemArray=新数组;
对于(var j=0;j<5;j++)
{
对于(变量i=0;i<5;i++)
{
项目数组[j][i]=itemlayer.add({
id:'项目-'+i+'-'+j,
x:51*i,
y:51*j,
w:50,
h:50,
声明:“正常”,
interactive:true,//尽管它们没有交互事件!
颜色:{normal:'#f00',悬停:'#00f'},
活动:{
onDraw:函数(ctx){
ctx.fillStyle=this.colors[this.state];
ctx.fillRect(this.x,this.y,this.w,t