html 画布 zindex

发布于 2024-10-17 19:11:01 字数 434 浏览 3 评论 0原文

如何更改画布内元素的 zindex?

示例代码:

fabric.Image.fromURL('http://nocookie.net/bw.jpg', function(image) {
    image.set('left', 200).set('top', 250);
    canvas.add(image);
});


var helloWorld = new fabric.Text('Hello world!', { 
  left: 0, 
  top: 0,
  fontfamily: 'delicious_500',
  zindex: 10
});

canvas.add(helloWorld);

使用fabric.js 库。

供将来参考:

fabric.Element.html#bringForward

How do I change the zindex of an element inside a canvas?

Example code:

fabric.Image.fromURL('http://nocookie.net/bw.jpg', function(image) {
    image.set('left', 200).set('top', 250);
    canvas.add(image);
});


var helloWorld = new fabric.Text('Hello world!', { 
  left: 0, 
  top: 0,
  fontfamily: 'delicious_500',
  zindex: 10
});

canvas.add(helloWorld);

Using the fabric.js library.

For future reference:

fabric.Element.html#bringForward

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

扮仙女 2024-10-24 19:11:01

我根本无法让这该死的东西工作,但这是我对发生的事情的猜测:

  1. Fabric 向该 URL 询问图像。当图像返回时,Fabric 被赋予了一个回调方法,因此在等待时,它会继续执行下一个任务
  2. Fabric 创建 Text 对象并将其添加到画布中。
  3. 最后,它注意到图像已加载(您提供的具体示例实际上对我进行了 404 处理),并将其传递给回调方法。

因为图像是在文本之后添加到画布上的,所以我假设 Fabric 的行为就像画布通常所做的那样:稍后添加的内容将绘制在已经存在的内容之上。 ...即使您为文本指定了 z 索引。

请记住,我无法让它发挥作用,就放弃了。所以我完全有可能完全错了。如果是这样,我有另一个理论:

您确定 zindex 是正确的属性吗?如果是,那么也尝试在图像上设置它:

image.set('left', 200).set('top', 250).set('zindex', 5);

如果您不确定,那么...就这样。

I can't get the dang thing working at all, but here's my guess as to what's happening:

  1. Fabric asks that URL for the image. Fabric has been given a callback method when the image is returned, so while it's waiting, it moves on to the next task
  2. Fabric creates the Text object and adds it to the canvas.
  3. Finally, it notices that the image has loaded (the specific example you provided actually 404'd on me), and passes it to the callback method.

Because the image is added to the canvas after the text, I'm assuming that Fabric is acting just as the canvas normally would: things that are added later are drawn on top of things that are already there. ...Even though you gave the text a z-index.

Remember, I couldn't get it to work and just gave up. So it's entirely possible I'm totally, completely wrong. If that is the case, I have another theory:

Are you sure that zindex is the correct property? If you are, then try setting it on the image, as well:

image.set('left', 200).set('top', 250).set('zindex', 5);

And if you aren't sure, then... there you go.

木落 2024-10-24 19:11:01

从 Fabric.js 版本 1.1.4 开始,可以使用 zIndex 操作的新方法:

canvas.moveTo(object, index);
object.moveTo(index);

我希望这就是您想要的:

http ://jsfiddle.net/Kienz/RRv3g/

Since fabric.js version 1.1.4 a new method for zIndex manipulation is available:

canvas.moveTo(object, index);
object.moveTo(index);

i hope this is what you want:

http://jsfiddle.net/Kienz/RRv3g/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文