gpt4 book ai didi

javascript - Html 5 Canvas 矩形删除和绘制后

转载 作者:搜寻专家 更新时间:2023-10-31 08:33:40 24 4
gpt4 key购买 nike

我尝试画一个矩形,删除它并在 Canvas 中重新绘制另一个矩形。
这三个操作的结果是有两个矩形。

HTML 5 API javascript:http://pastebin.com/Qgf38C7m

function Oggetto(idname,nome,posizione_x,posizione_y,width,height,doption){
this.nome = nome ;
this.posizione_x = posizione_x ;
this.posizione_y = posizione_y ;
this.width = width ;
this.height = height ;
this.doption = doption ;
this.idname = idname ;
console.log(this.idname);
this.context = document.getElementById(idname).getContext("2d");
}
Oggetto.prototype.draw = function () {
};
Oggetto.prototype.clear = function () {
};



function Entita(idname,nome,posizione_x,posizione_y,width,height,doption){
Oggetto.call(this,idname,nome,posizione_x,posizione_y,width,height,doption);
}
Entita.prototype.draw = function (){
this.context.rect(this.posizione_x,this.posizione_y,this.width,this.height);
this.context.stroke();
};
Entita.prototype.clear = function () {
// this.context.clearRect(this.posizione_x, this.posizione_y, this.width, this.height);
//Richiamo il metodo per la creazione di un rettangolo con background
this.context.clearRect(this.posizione_x-4, this.posizione_y-4, this.width+10, this.height+10);

};
Entita.prototype.enlarge = function (w,h) {
this.clear();
this.width = w;
this.height = h;
this.draw();
};
Entita.prototype = new Oggetto();

调用它的 javascript 代码:

 e =new Entita("ke","pio",10,10,100,100,"prova");   
e.draw();
e.enlarge(400,200);

结果:
http://postimg.org/image/vpgg20nyt/

最佳答案

由于您正在使用当前工作路径 绘制矩形,因此您需要使用beginPath 重置当前工作路径。否则,您只是将新的子路径添加到当前工作路径,这就是为什么在 Entitia.draw 中使用 stroke 绘制两个矩形的原因。

您总是可以使用 strokeRect 来避免与当前工作路径的填充/描边混淆,但是如果您打算在将来添加任意形状,最好的选择是简单地添加this.context.beginPath() 行作为 Entitia.draw 的第一条语句。

Entita.prototype.draw = function () {
this.context.beginPath();
//Other statements for defining
//current working path and stroking/filling
//it
};

Working Fiddle .

关于javascript - Html 5 Canvas 矩形删除和绘制后,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16876778/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com