在kineticjs中创建变量

时间:2015-01-22 16:58:58

标签: javascript image kineticjs

fiddle中,将图像放入容器后,每个图像都存储在名为image的变量中。但是我想将每个丢弃的图像存储在不同的变量名称中。它不起作用(image1.onload)。我怎么能这样做?

   if($(ui.helper).hasClass("sticky")){
            console.log();
             var image = new Kinetic.Image({
                name: data,
                id: "image"+(imageCount++),
                x: $stageContainer.width() / 2 - ($(ui.helper).width() / 2),
                y: $stageContainer.height() / 2 - ($(ui.helper).height() / 2),
                image: theImage,
                draggable: false
            });
        }else{

            var image1 = new Kinetic.Image({
                name: data,
                id: "image"+(imageCount++),
                x: x,
                y: $stageContainer.height() / 2 - ($(ui.helper).height() / 2),
                image: theImage,
                draggable: true
      });
    }

1 个答案:

答案 0 :(得分:1)

我认为使用数组可能是最佳选择。

var images = [];

images.push(new Kinetic.Image({
    name: data,
    id: "image"+(imageCount++),
    x: x,
    y: $stageContainer.height() / 2 - ($(ui.helper).height() / 2),
    image: theImage,
    draggable: true
}))

您可以使用数组中的索引引用它们,然后像​​images[0].onload