Kinetic.js:为什么图像仅在第二次调用函数时显示?

时间:2014-01-14 08:09:49

标签: kineticjs

然后显示两个图像

function addImage(src){

var imageObj = new Image();
imageObj.src = src;

    var imagenew = new Kinetic.Image({
      x: 80,
      y: 80,
      image: imageObj,
      width: 65, 
      height: 65,
  name:'imagename',
  draggable:true
  }); // end new image

layer.add(imagenew);
layer.draw();

} //结束addImage

感谢

1 个答案:

答案 0 :(得分:1)

您必须使用onload回调来加载imageObj时间:

var imagenew;

var imageObj = new Image();
imageObj.onload=function(){
    imagenew = new Kinetic.Image({
        x: 80,
        y: 80,
        image: imageObj,
        width: 65, 
        height: 65,
        name:'imagename',
        draggable:true
    }); // end new image
    layer.add(imagenew);
    layer.draw();
}
imageObj.src = src;