Normally, people who draw a lot of stuff in a canvas will make their own shape objects with style properties. For example: http://jsfiddle.net/b93cc3ww/
context = document.getElementById("myCanvas").getContext("2d");
function Rectangle(params) {
this.x = params.x || 0;
this.y = params.y || 0;
this.width = params.width || 0;
this.height = params.height || 0;
this.fillStyle = params.fillStyle || "#FFFFFF";
this.strokeStyle = params.strokeStyle || "#000000";
this.lineWidth = params.lineWidth || 0;
}
Rectangle.prototype.draw = function () {
if (this.fillStyle) {
context.fillStyle = this.fillStyle;
context.fillRect(this.x, this.y, this.width, this.height)
}
if (this.strokeStyle && this.lineWidth) {
context.strokeStyle = this.strokeStyle;
context.lineWidth = this.lineWidth;
context.strokeRect(this.x, this.y, this.width, this.height);
}
}
rectangles = [
new Rectangle({
x: 10,
y: 10,
width: 300,
height: 150,
fillStyle: "#FF0000"
}),
new Rectangle({
x: 250,
y: 10,
width: 100,
height: 80,
fillStyle: "#00FF00",
strokeStyle: "#00AA00",
lineWidth: 5
}),
new Rectangle({
x: 10,
y: 200,
width: 250,
height: 80,
strokeStyle: "#FF0000",
lineWidth: 1
})
]
for (var i = 0; i < rectangles.length; ++i) {
rectangles[i].draw();
}
If you like the way CSS works, you could make a shape object which can take a "class" parameter and then store a list of "clases" in an array at the top of your code.