Uncaught InvalidStateError:尝试使用不可用或不再可用的对象

时间:2014-04-08 14:01:48

标签: javascript php image canvas kineticjs

Uncaught InvalidStateError: An attempt was made to use an object that is not, or is no longer, usable. 

在这行代码中发生:

var nodeIconImg = new Image();
                var nodeIcon = new Kinetic.Image({
                  image: nodeIconImg,
                  width: 30,
                  height: 30,
                  cornerRadius: radius
                });
            if(node.main_photo == 'no_photo.png')
            {
                var url = '<?= URL::to(''); ?>/img/' + node.main_photo;
                nodeIconImg.src = url;
            }

我正在使用kinetic js,字符串是正确的,图像文件存在,但在我分配src后发生错误,无法弄明白,我也试过了:

var url = escape('<?= URL::to(''); ?>/img/' + node.main_photo);

但它没有帮助,任何想法??

这个错误意味着什么?我该怎么做才能解决它?

1 个答案:

答案 0 :(得分:0)

在创建Kinetic.Image对象之前,您必须等到加载图像。

var img = new Image();
img.onload = function() {
    var image = new Kinetic.Image({
       image : img
    });
}
img.src = 'url';

演示:http://jsbin.com/gibeq/1/edit