[英]Layering Canvas on another Canvas?
Im currently doing something that goes like: 我目前正在做这样的事情:
However, its a pain having to redraw the entire canvas including the non-moving PNGs. 但是,必须重新绘制包括不移动的PNG在内的整个画布是很痛苦的。
Im wondering if it would be smarter or better to have a single canvas for the Background/hexgrid and create the PNGs on additional, small canvas elements which are then zIndex'ed on top of the Background canvas. 我想知道为背景/混合网格创建单个画布,然后在其他较小的画布元素上创建PNG,然后将zIndex放在背景画布的顶部,会更好还是更好。 I then could make the animations (PNGs moving) easier. 然后,我可以使动画(PNG移动)更容易。
However, im running into problems in regards to actually placing the "new" canvas's at the right x/y coordinates of the Background canvas. 但是,在将“新”画布实际放置在“背景”画布的正确x / y坐标方面遇到了问题。
this.drawShipImage = function(name, id, x, y){
var div = document.getElementById("grid");
// initial, big canvas with background is created elsewhere but has the same width/height than "grid"
var canvas = document.createElement("canvas");
canvas.id = name + id;
canvas.width = 30;
canvas.height = 30;
canvas.style.position = "absolute";
canvas.style.zIndex = 1;
var context = canvas.getContext("2d");
context.scale(0.75, 0.75);
context.drawImage(ship, 0, 0);
div.appendChild(canvas);
}
Basicly, this function has 2 important Parameters, that is the x/y Parameter where the new canvas SHOULD be drawn in context to the width/height of the Background canvas. 基本上,此函数有两个重要的参数,即x / y参数,应在上下文中根据背景画布的宽度/高度绘制新画布。 However im unable to conclude a way to get the Image to appear at the right coordinates. 但是,im无法断言使图像显示在正确坐标处的方法。
I suppose the only way it could work is adding canvas.style.margin = x/y-ish into the mix ? 我想它唯一可行的方法是将canvas.style.margin = x / y-ish添加到混合中?
Using a second canvas (or just an img element holding your background) might be a good solution since your background will be redrawn less frequently than your animated foreground. 使用第二个画布(或仅保留背景的img元素)可能是一个很好的解决方案,因为与动画前景相比,重绘背景的频率较低。
This is particularly true if your device has a GPU because the background element will be cached in the GPU and will redraw very quickly as compared with the relatively slower drawings on your animating foreground canvas. 如果您的设备具有GPU,则尤其如此,因为与动画前景画布上相对较慢的绘图相比,背景元素将被缓存在GPU中,并且将非常快速地重绘。
You can use the CSS position
property to stack 2 elements. 您可以使用CSS position
属性来堆叠2个元素。
position
property to relative (meaning children will be positioned relative to the wrapper instead of relative to the page). 将包装器div的position
属性设置为相对(意味着子项将相对于包装器而不是相对于页面放置)。 position
to 'absolute' (meaning they are subject to being positioned relative to the wrapper). 将2个canvas(或canvas + img元素)的position
为“绝对”(意味着它们相对于包装器而言是定位的)。 top
& left
properties of the child elements to 0 so they will overlap. 将子元素的top
和left
属性设置为0,以便它们重叠。 The default top & left values are zero, so assigning them to zero is optional--but explicitly assigning them to zero is more clear. 默认的左上角值是零,因此将它们分配为零是可选的-但是更明确地将它们分配为零。 HTML HTML
<div id='wrapper'>
<canvas id="canvasBottom" width=300 height=200></canvas>
<canvas id="canvasTop" width=300 height=200></canvas>
</div>
CSS CSS
#wrapper{
position:relative;
width:300px;
height:200px;
}
#canvasTop,#canvasBottom{
position:absolute; top:0px; left:0px;
border:1px solid green;
width:300px;
height:200px;
}
#canvasTop{
border:1px solid red;
}
It's really easy to layer canvas elements via KineticJS
from Eric Rowell. 通过Eric Rowell的KineticJS
对画布元素进行KineticJS
非常容易。 It's a very useful library to work with the HTML5 canvas element. 这是使用HTML5 canvas元素的非常有用的库。
The lib is not maintained anymore but its very stable. lib不再维护,但非常稳定。 Get it here: KineticJS Download 在这里获取: KineticJS下载
Find the reference here: KineticJS Reference 在这里找到参考: KineticJS参考
If you search a little bit on the web for examples you will clearly understand that every node in KineticJS is an own canvas element. 如果您在网上搜索一些示例,您将清楚地了解KineticJS中的每个节点都是一个自己的canvas元素。 This library relies on layering canvas elements via groups and so on. 该库依靠通过组等对画布元素进行分层。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.