gpt4 book ai didi

javascript - 图片源无效错误

转载 作者:行者123 更新时间:2023-11-28 08:21:05 24 4
gpt4 key购买 nike

var nodeIconImg = new Image();
nodeIconImg.onload = function () {

var nodeIcon = new Kinetic.Image({
image: nodeIconImg,
width: 30,
height: 30
});
}

var url = '<?= $baseurl; ?>/img/' + node.main_photo;
nodeIconImg.src = url;

这是kinetic.js中发生错误的核心代码行(恰好在drawImage方法中):

drawImage: function () {
var a = arguments,
b = this._context;
3 === a.length ? b.drawImage(a[0], a[1], a[2]) : 5 === a.length ? b.drawImage(a[0], a[1], a[2], a[3], a[4]) : 9 === a.length && b.drawImage(a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7], a[8])
}

错误信息是:

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

有什么想法吗?有人遇到过这个问题吗?

最佳答案

onload 函数完成后,您的 nodeIcon 不在范围内。

因此,当您稍后在代码中尝试使用 nodeIcon 时,它不再定义并引发错误。

您需要在 onload 函数外部声明 var nodeIcon

var nodeIcon;

var nodeIconImg = new Image();
nodeIconImg.onload = function () {

nodeIcon = new Kinetic.Image({
image: nodeIconImg,
width: 30,
height: 30
});

}
var url = "ship.png";
nodeIconImg.src = url;

或者将nodeIcon添加到onload函数内的图层

var nodeIconImg = new Image();
nodeIconImg.onload = function () {

var nodeIcon = new Kinetic.Image({
image: nodeIconImg,
width: 30,
height: 30
});
layer.add(nodeIcon);
layer.draw();

}
var url = "ship.png";
nodeIconImg.src = url;

关于javascript - 图片源无效错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22951994/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com