0

好的,所以我是 KineticJS 的新手,在使用它时,我似乎无法将图像背景与该图像上方的矩形相结合。现在,如果我删除背景图像,则显示矩形。即使我将矩形代码放在图像上方,仍然不会显示在顶部。当然这是我想念的简单的东西,但我似乎无法弄清楚它是什么,也无法在 stackoverflow 上找到类似的问题。谢谢您的帮助。

这是我的代码:

<!DOCTYPE HTML>
<html>
<head>
    <style>
    </style>
</head>
<body>
    <div id="container"></div>

    <script src="http://d3lp1msu2r81bx.cloudfront.net/kjs/js/lib/kinetic-v4.5.4.min.js"></script>
    <script defer="defer">
        var stage = new Kinetic.Stage({
            container: 'container',
            width: 600,
            height: 450
        });

        //Layer for our background
        var background_layer = new Kinetic.Layer();

        //Canvas background image
        var canvasBackgroundImage = new Image();
        canvasBackgroundImage.onload = function() {
            var backgroundImage = new Kinetic.Image({
                x: 0,
                y: 0,
                image: canvasBackgroundImage,
                width: 600,
                height: 450
            });

            background_layer.add(backgroundImage);
            stage.add(background_layer);
        };
        canvasBackgroundImage.src = 'images/quiz_back.jpg'; //Location of our background

        //Question container
        var question_container_layer = new Kinetic.Layer();
        var question_container = new Kinetic.Rect({
            x: 100,
            y: 100,
            width: 200,
            height: 200,
            fill: 'green',
            stroke: 'black',
            strokeWidth: 2
        });
        question_container_layer.add(question_container);
        stage.add(question_container_layer);

    </script>
</body>

4

1 回答 1

2

您的问题是您要添加background_layercanvasBackgroundImage.onload函数内的阶段。Javascript 将运行整个脚本,并question_container_layer首先添加到舞台,然后当您的图像加载时,background_layer将添加到舞台。结果,背景图像总是出现在矩形层的顶部。

要解决此问题,请将图层添加到您的onload功能之外的阶段:

<script defer="defer">
    var stage = new Kinetic.Stage({
        container: 'container',
        width: 600,
        height: 450
    });

    //Layer for our background
    var background_layer = new Kinetic.Layer();
    var question_container_layer = new Kinetic.Layer();
    stage.add(background_layer);
    stage.add(question_container_layer);

    //Canvas background image
    var canvasBackgroundImage = new Image();
    canvasBackgroundImage.onload = function() {
        var backgroundImage = new Kinetic.Image({
            x: 0,
            y: 0,
            image: canvasBackgroundImage,
            width: 600,
            height: 450
        });
        background_layer.add(backgroundImage);
        background_layer.draw();
    };
    canvasBackgroundImage.src = 'http://www.html5canvastutorials.com/demos/assets/darth-vader.jpg'; //Location of our background

    //Question container

    var question_container = new Kinetic.Rect({
        x: 50,
        y: 50,
        width: 200,
        height: 200,
        fill: 'green',
        stroke: 'black',
        strokeWidth: 2
    });
    question_container_layer.add(question_container);

    question_container_layer.draw();

</script>

为避免将来出现这种情况,除非您将动态图层添加到舞台,否则我建议在执行任何其他操作之前将所有已知图层添加到舞台。这样您就可以控制图层的顺序。

或者,您可以使用该zIndex属性对图层进行排序。见这里:Kinetic.Container#setZIndex

于 2013-07-17T03:20:47.067 回答