简体   繁体   English

如何控制画布对象的z-index?

[英]How can I control z-index of canvas objects?

When I draw some objects in canvas, sometimes new objects places under old ones. 当我在画布中绘制一些对象时,有时候新对象会放在旧对象下面。 For example, I add some images to canvas and then draw a line. 例如,我将一些图像添加到画布然后绘制一条线。 when I run code images are top of the line. 当我运行代码图像是最重要的。 I searched web for a solution. 我在网上寻找解决方案。 But there was not an straight-forward solution. 但没有直接的解决方案。 :( In some cases, it have seen as a bug! in explorers. How can I control z-index? :(在某些情况下,它已被视为一个错误!在探险家。我怎么能控制z-index?

Edit: this is my code 编辑:这是我的代码

<img id="black_checkers_piece" src="egsdk/BlackCheckersPiece.png" style="display: none;">
<canvas id="c1" style="border:1px solid #998800"></canvas>
<script type="text/javascript">
var canvasID = document.getElementById('c1');
var ctx = canvasID.getContext('2d');
var img = new Image();
img.src = document.getElementById('black_checkers_piece').src;
img.onload = function(){
ctx.drawImage(img, 10, 10, 32, 32);
}
ctx.beginPath();
ctx.strokeStyle = 'red';
ctx.moveTo(0, 0);
ctx.lineTo(50, 50);
ctx.stroke();
</script>

You are most likely waiting for images to load before drawing them. 在绘制图像之前,您很可能正在等待图像加载。

Additionally, you are probably drawing lines as soon as you can. 此外,您可能会尽快绘制线条。

You need to wait for all images to load, then draw everything in the order you want (from farthest z-order to closest). 您需要等待所有图像加载, 然后按照您想要的顺序绘制所有图像(从最远的z顺序到最近的顺序)。 Do not draw anything until all images have been loaded, or your z-order will not be correct. 在加载所有图像或z顺序不正确之前,请勿绘制任何内容

If you want to show items on the screen before all images are loaded, you must redraw everything on each image load, not just draw that one image. 如果要在加载所有图像之前在屏幕上显示项目,则必须重新绘制每个图像加载的所有内容 ,而不是仅绘制一个图像。

Here's an example, where I draw 3 objects: A circle, an image, and a window. 这是一个例子,我绘制了3个对象:圆形,图像和窗口。 I draw them immediately, whether the image is loaded or not, but I draw all of them again once the image is loaded to ensure that the image is correctly in between the other two objects: 我立刻吸引他们,图像是否被加载或没有,但我再次提请他们全部一次加载图像,以确保图像的正确其他两个物体之间:

http://jsfiddle.net/U5bXf/32/ http://jsfiddle.net/U5bXf/32/

The shorthand relevant code (in case jsfiddle goes down) is: 速记相关代码(如果jsfiddle发生故障)是:

var img = new Image()
img.onload = function() {
  draw()
};
img.src = "http://placekitten.com/100/140";

draw();

function draw() {
// object one

// Now we'll draw the image in between, if its loaded
ctx.drawImage(img, 100, 30);

// object two
}

Code below this line is a response to the comments 此行下方的代码是对评论的回复

You've since posted code, but you'll notice that the only thing you are drawing in the onload function is the image. 你已经发布了代码,但你会注意到你在onload函数中绘制的唯一内容就是图像。

The onload function usually happens after the other drawing commands (unless the page is already loaded, then it could be either). onload函数通常其他绘图命令之后发生(除非页面已经加载,然后它可以是)。 You need to move all drawing code into the onload function or else make your own drawing function that loads everything. 您需要将所有绘图代码移动到onload函数中,或者创建自己的绘图函数来加载所有内容。 For example: 例如:

<img id="black_checkers_piece" src="egsdk/BlackCheckersPiece.png" style="display: none;">
<canvas id="c1" style="border:1px solid #998800"></canvas>
<script type="text/javascript">
var canvasID = document.getElementById('c1');
var ctx = canvasID.getContext('2d');
var img = new Image();
img.src = document.getElementById('black_checkers_piece').src;
img.onload = function(){
  // assuming we want the image drawn before the line:
  ctx.drawImage(img, 10, 10, 32, 32);

  ctx.beginPath();
  ctx.strokeStyle = 'red';
  ctx.moveTo(0, 0);
  ctx.lineTo(50, 50);
  ctx.stroke();
}

Canvas doesn't have any concept of 'z-index.' Canvas没有'z-index'的概念。 There are no objects in a canvas - canvases are just bitmaps, just pixels. 画布中没有对象 - 画布只是位图,只是像素。

Are you sure you're drawing the line and the images on the canvas, and not just creating new elements (like img elements?). 您确定要在画布上绘制线条和图像,而不仅仅是创建新元素(如img元素吗?)。

If you are, then check the order in which you're calling the drawing methods like fill() and stroke(). 如果是,请检查您调用绘图方法的顺序,如fill()和stroke()。 Shapes aren't actually drawn until you do that (no matter where you have your moveTo and lineTo calls). 在您执行此操作之前,实际上不会绘制形状(无论您在哪里进行moveTo和lineTo调用)。

I have been using fabric.js and it has some features that you may find useful. 我一直在使用fabric.js,它有一些你可能会觉得有用的功能。 While not z-index per se, it can set a background or an overlay - below is an example found on http://fabricjs.com/customization/ 虽然不是z-index本身,但它可以设置背景或叠加 - 下面是http://fabricjs.com/customization/上的示例。

var canvas = new fabric.Canvas('c13'); var canvas = new fabric.Canvas('c13'); canvas.add(new fabric.Circle({ radius: 30, fill: '#f55', top: 100, left: 100 })); canvas.add(new fabric.Circle({radius:30,fill:'#f55',top:100,left:100})); canvas.setOverlayImage('../assets/jail_cell_bars.png', canvas.renderAll.bind(canvas)); canvas.setOverlayImage('../ assets / jail_cell_bars.png',canvas.renderAll.bind(canvas)); this.__canvases.push(canvas); 此.__ canvases.push(帆布);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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