I am making an application where different rectangles are painted on a canvas and I am trying to do it with Backbone. I have a model called box:
Box = Backbone.Model.extend({
defaults: {
x: 0,
y: 0,
w: 1,
h: 1,
color: "#FF9000",
linewidth: 3,
id: 0,
},
drawBox: function(ctx) {
ctx.fillStyle = "#FF9000";
ctx.globalAlpha = 0.1;
ctx.fillRect(this.get("x"), this.get("y"), this.get("w"), this.get("h")); //transparent box in the back
ctx.globalAlpha = 1;
ctx.strokeStyle = this.get("color");
ctx.lineWidth = this.get("linewidth");
ctx.strokeRect(this.get("x"), this.get("y"), this.get("w"), this.get("h")); //rectangle on top
}
});
And I also have a collection of this Box model:
BoxSet = Backbone.Collection.extend({
model: Box
});
What I have in mind is to have a view where I can put every Box model in the BoxSet collection on a canvas using the drawBox method in the Box model, but so far all the tutorials and examples deal with simple text templates and I cannot figure out how to acomplish this.
Any ideas on how could this be done using Backbone views?
Thanks in advance.