javascript – Html 5画布矩形删除和绘制后
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了javascript – Html 5画布矩形删除和绘制后,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2140字,纯文字阅读大概需要4分钟。
内容图文
我试图画一个矩形,擦除它并在画布上重绘另一个矩形.
这三个操作的结果是有两个矩形.
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);
结果:
解决方法:
由于您使用当前工作路径绘制矩形,因此需要使用beginPath重置当前工作路径.否则,您只是将新的子路径添加到当前工作路径,这就是为什么在Entitia.draw中使用笔划绘制两个矩形的原因.
您可以随时使用strokeRect以避免与填充/抚摸当前工作路径混淆,但如果您计划在将来添加任意形状,最好的办法是简单地添加this.context.beginPath()行作为Entitia.draw的第一个声明.
Entita.prototype.draw = function () {
this.context.beginPath();
//Other statements for defining
//current working path and stroking/filling
//it
};
内容总结
以上是互联网集市为您收集整理的javascript – Html 5画布矩形删除和绘制后全部内容,希望文章能够帮你解决javascript – Html 5画布矩形删除和绘制后所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。