开发者

Nested CANVAS elements

开发者 https://www.devze.com 2023-03-12 04:55 出处:网络
Is it possible to have aelement within anotherelement? I know i can layer them, but is this possible -

Is it possible to have a element within another element? I know i can layer them, but is this possible -

<canvas id="parent">
    <canvas id="child"></canvas&g开发者_如何学运维t;
</canvas>

I tried but doesnt seem to work.


The canvas specification does not allow for this. The canvas element may be used in an embedded content context; nesting a canvas element places it in a fallback content context, which is not supported.


If you want to handle background and foreground separately, you could use two canvas and put one above the other with css.

<canvas id="bg" width="640" height="480" style="position: absolute; z-index: 0">
</canvas>
<canvas id="fg" width="640" height="480" style="position: absolute; z-index: 1">
</canvas>

Actually this one of the tips to improve canvas performance. Source: HTML5 Rocks


You might want to consider doing a virtual embedding by translating to various positions on your canvas.

Here's an example of doing this:

http://marketimpacts.com/storage/9781118385357%20ls0702%20Complex%20Objects.htm

It's from:

http://www.amazon.com/HTML5-Canvas-For-Dummies-Cowan/dp/1118385357/ref=cm_cmu_up_thanks_hdr


You can, as Isaac Zepeda said in his answer, you need to have different canvas id's and z indexes. You also need to name almost all of the variables and functions for each canvas something different so that you don't draw on canvas A when you mean to draw on canvas B.

0

精彩评论

暂无评论...
验证码 换一张
取 消