I've been doing tests with multiple canvases, i.e. layers.
Here is what I've found out:
• Having multiple canvases breaks some of the event handlers. For example, the Mouse Move event handler died cause it was being blocked by the other canvas.
Solution: Put all canvases in a div called "holder", tie all event handlers to the div.
• I can dynamically create, remove, and re-order canvases using javascript
Uses: This means people will be able to make more layers in their game whenever they want.
• You can draw to a canvas, and then draw that canvas to another canvas. And draw that canvas to another canvas. I call this, canvasception.
Meaning: You can greatly enhance the speed of a game that has customizable characters. Imagine, your character has equipped a hat, a sword, a shield, and a mustache. To draw all those, you'd have to redraw them every frame like this: (1) Draw character (2) Draw Hat (3) Draw sword (4) Draw shield (5) Draw mustache. Redrawing the whole character every frame is sloooooooow. Therefore the solution is to create a new canvas. Except it's called an Off-Screen canvas. You draw the player to the Off-Screen canvas once. Then you can draw the Off-Screen canvas to the On-Screen canvas whenever you need to show the player. This greatly increases game speed!
• Canvases are transparent at first.
So: They are perfect for layers! Especially when you want to add dynamic lighting to a scene.
Pretty awesome, huh?
So here's a layer specification I'm mocking up:You start with 1 canvas by default.
You can create a canvas by:
var myCanvas = CreateCanvas(SizeX, SizeY)
Or get the main canvas by:
var myCanvas = GetMainCanvas()
Creating a canvas doesn't automatically add it to the screen, to add a canvas do:
You can move canvases so one will be on top of the other by setting the Z-Index:
SetCanvasZIndex(myCanvas, 10)
You can take canvases off the screen by:
You can set the main canvas by:
You're probably wondering, why do I need to set a main canvas?
Well, when ever you do a draw command, it automatically draws it to the main canvas. If you want to draw to a different canvas, you have to set your other canvas as the main canvas.
How do you guys feel about this layer specification? Should I call them layers or canvases?
I'll start coding this in when I get some approval.