【问题标题】:KineticJS Z-Index doesnt applyKineticJS Z-Index 不适用
【发布时间】:2013-01-04 08:27:29
【问题描述】:

你好 stackoverflow 社区

我正在尝试使用 KineticJS 对图像进行分层。不幸的是,它不起作用。 就像您在代码 sn-p 中看到的那样,我正在设置两张图片的 Z-Index,当我记录它们时,它们的顺序仍然相同。 可以看到函数startClouds(field)。此函数创建 Z-Index 为 2 的云,这实际上是有效的。一个小时以来我一直在尝试解决这个问题,但我真的不知道为什么它不起作用。

var title = new Image();
var title_p = new Image();
title.src = '/test/images/title_bg.png';
title_p.src = '/test/images/title_pic.png';

title.onload = function(){
    var title_background = new Kinetic.Image({
        x: 0,
        y: 0,
        image: title
    });
    field.add(title_background);
    title_background.setZIndex(1);
    field.draw();
    console.log('Z-Index of background: '+title_background.getZIndex());

    var title_pic = new Kinetic.Image({
        x: 0,
        y: 0,
        image: title_p
    });
    field.add(title_pic);
    title_pic.setZIndex(3);
    field.draw();
    console.log('Z-Index of Title: '+title_pic.getZIndex());

    startClouds(field);

    var start = new Kinetic.Text({
        x: 240,
        y: 160,
        text: '[Press Any Key to Start]',
        fontSize: 22,
        fontFamily: 'Calibri',
        fill: 'white'
    });
    field.add(start);
    field.draw();
    stage.add(field);
}

感谢您的帮助

【问题讨论】:

  • 如果您将代码放在 jsfiddle 中会有所帮助,这样人们就可以看到控制台日志并可以使用您的代码。
  • 嗯...等一下,我必须更改路径。
  • 如果以下答案对您有所帮助,请告诉我。

标签: canvas z-index kineticjs


【解决方案1】:

是否有可能修复有助于解决此问题的 jsFiddle?我真的很想看到它,因为我对 Kinetic z-Index 感到很困惑。这里接受的答案充满了断开的链接,因为像 http://www.wallerdeknaller.ch/test/td_functions.js 这样的文件不再存在。

我认为如果 jsFiddle 是完全自封装的,其中包含所有必要的代码和 css,则它的效果最好。或者至少,从 CDN 加载库。如果您不想为后代修复小提琴的麻烦,如果您将文件发送给我,我很乐意这样做。

我知道这不遵循 SO 回答指南,我只是没有 50 名声望来评论正确答案。很抱歉!

【讨论】:

  • 您好 Oscar,抱歉,这些文件已被移动。但作为提示!在使用 KineticJS 半年之后,我使用了 moveDown()、moveUp()、moveToBottom() 和 moveToTop() 方法。它比设置 Z-Indexes 效果更好。不知何故,我没有通过设置绝对 Z-Indexes 来实现它。如果您有任何问题,请不要介意给我发消息。
【解决方案2】:

在 kineticjs 中,每次向图层添加新图像时,都会自动设置索引。索引从 0 开始

首先,像这样重新排序您的代码:

 //create objects first
 var title_background = new Kinetic.Image({
    x: 0,
    y: 0,
    image: title
 });
 var title_pic = new Kinetic.Image({
    x: 0,
    y: 0,
    image: title_p
 });
 var start = new Kinetic.Text({
    x: 240,
    y: 160,
    text: '[Press Any Key to Start]',
    fontSize: 22,
    fontFamily: 'Calibri',
    fill: 'white'
});
// now add objects to layer
field.add(title_background);   // z-index of 0
field.add(title_pic);          // z-index of 1
field.add(start);              // z-index of 2

startClouds(field);   // anything created by startClouds() is going to have z-index > 2

如果你想在 z-indexes 周围移动东西,尽量避免

 .setZIndex()  //note: use this AFTER all the objects have been added to the layer

作用与用途

 .moveToTop() and .moveToBottom() //this way the movement is relative rather than specific.

如果您需要更多帮助,请将一些功能代码粘贴到 jsfiddle 中,我可以为您提供更多帮助。

【讨论】:

  • 所以我做了这个小小提琴。 jsfiddle.net/QKJbA/1,你的代码 sn-p 并没有真正改变什么:)
  • 好吧。其实云已经不重要了。其实我只是想改变title_background和title_pic的Z-Index。
  • 你希望每个的索引是什么?总是在最前面?
  • 基本上,您希望背景和标题的 z-index 行为是什么?
  • 其实我只是想改变它们 xD 这听起来很简单,但它不会工作!
猜你喜欢
  • 1970-01-01
  • 2019-04-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-10
  • 1970-01-01
相关资源
最近更新 更多