简体   繁体   English

在另一个画布上层叠画布?

[英]Layering Canvas on another Canvas?

Im currently doing something that goes like: 我目前正在做这样的事情:

  • create a canvas and append it to a divider 创建画布并将其附加到分隔线
  • set a background image via CSS on that canvas. 通过CSS在该画布上设置背景图像。
  • draw a hexgrid on the canvas 在画布上画一个六边形网格
  • draw PNGs on the canvas. 在画布上绘制PNG。
  • animate those PNGs to display "movement". 对这些PNG进行动画处理以显示“运动”。

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个元素。

  1. Wrap your 2 canvases (or canvas + img elements) in a wrapping div. 将2个画布(或canvas + img元素)包裹在一个包裹div中。
  2. Set the wrapper div's position property to relative (meaning children will be positioned relative to the wrapper instead of relative to the page). 将包装器div的position属性设置为相对(意味着子项将相对于包装器而不是相对于页面放置)。
  3. Set the 2 canvases (or canvas + img elements) position to 'absolute' (meaning they are subject to being positioned relative to the wrapper). 将2个canvas(或canvas + img元素)的position为“绝对”(意味着它们相对于包装器而言是定位的)。
  4. Set the top & left properties of the child elements to 0 so they will overlap. 将子元素的topleft属性设置为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;
    }

Layer canvas elements via KineticJS 通过KineticJS图层画布元素

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM