javascript对象MVC画布'类'清理
javascript Object MVC Canvas 'class' decloration
嗨,我是整个OOJ和原型模式的新手,在构建包含大量html5画布标签的应用程序时,我正在努力学习这一点。
我想创建一个初始的canvas对象来容纳我执行canvas标记所需的标准选项的基础,这些选项需要是动态值,所以下面是我的构造函数(如果这是不正确的,请原谅我的术语)
//create the view object
var view = view ||{};
//generic canvas obj
view.pageCanvas = function(){
//incase I need 'this in a new context'
var This = this;
//get the canvas
this.canvas = function(id){
document.getElementById(id).getContext('2d');
};
//draw a line
this.line = function (x,y){
return this.canvas.lineTo(x,y);
};
//set the line width
this.linewidth = function(width){
this.canvas.lineWidth(width);
};
//set the stroke colour
this.lineCol = function(colour){
return this.canvas.strokeStyle = colour;
}
//draw the line
this.build = this.canvas.stroke();
};
然后我会用下面这样的东西来称之为
Background = new view.pageCanvas();
Background.canvas('BG');
Background.canvas.line(400 , 0);
Background.canvas.line(0,500);
Background.canvas.linewidth(10);
Background.canvas.lineCol( "blue"); // line color
Background.canvas.build();
我在这方面出了问题,因为我在"this.canvas.stroke()"上出现了类型错误
有人能告诉我哪里出了问题吗?或者这种模式是否真的不是最好的方法?最终,我希望设计是可扩展的
提前感谢:)
在您的情况下,this.canvas是一个函数,因此您无法解析this.canvas.stroke(),因为您定义的this.canva是一个接受DOM中canvas标记id的函数。
与其将this.cnv作为一个函数,不如将其作为document.getElementById("),这样它将成为对画布对象的引用。
//create the view object
var view = view ||{};
view.setCanvas = function(id) {
this.canvas = document.getElementById(id).getContext('2d');
};
//generic canvas obj
view.pageCanvas = function() {
//incase I need 'this in a new context'
var This = this;
//draw a line
this.line = function (x,y){
return this.canvas.lineTo(x,y);
};
//set the line width
this.linewidth = function(width){
this.canvas.lineWidth(width);
};
//set the stroke colour
this.lineCol = function(colour){
return this.canvas.strokeStyle = colour;
}
//draw the line
this.build = this.canvas.stroke();
};
所以setCanvas看起来就像上面的一样,你可以根据需要改变你想用它做什么。
相关文章:
- 引用对象中的通用值
- jQuery匹配JSON对象的部分文本
- 节点导出返回一个空对象
- 如何在Javascript中将JSon对象转换为数组
- 我可以在json对象中添加一个函数吗
- 使用JS将数组转换为json对象
- 全局变量和全局对象的属性之间有什么区别吗
- 比较从函数和生成的日期对象
- Javascript,访问一个主要对象模块模式中的每个对象
- JavaScript清理“”中引用的(本地)对象;延迟的“;功能
- 清理在构造函数中引发错误的对象
- 数据清理 JavaScript 对象
- 如何清理节点中的对象数组?手动遍历它返回“对象对象”
- 创建一个联系人数据数组的已清理联系人对象
- 对象数组中的数据清理:无法进行循环工作
- javascript对象MVC画布'类'清理
- 如何清理XML对象以避免“序言中不允许的内容”;错误
- 在删除对象之前运行Javascript清理代码
- 清理对象数组
- Ajax(XMLHttpRequest)对象在使用后进行清理