我正在制作应用程序,但我遇到了问题。当我按下按钮时,我需要在Canvas上添加新点(图像)。这是一个代码:
var ID = 0;
var points = [];
function addPoint(){
points.push({id: ID, posX: 0, posY: 0, url: "img/point.png"});
ID++;
showPoints();
}
function showPoints(){
var img = new Array();
var point = new Array();
var stage = new Kinetic.Stage({
container: 'cvsCroatia',
width: 574,
height: 508
});
var layer = new Kinetic.Layer();
for(var j=0; j < ID; j++){
img[j] = new Image();
img[j].src = 'img/point.png';
img[j].onload = (function(){
point[j] = new Kinetic.Image({
x: points[j].posX,
y: points[j].posY,
image: img[j],
width: 13,
height: 13,
name: img[j],
draggable: true
});
});
layer.add(point[j]);
}
stage.add(layer);
}
但是我有一个错误:
Uncaught TypeError:无法设置未定义的属性“索引”(kinetic-v4.4.0.min.js:29)
你有什么主意,怎么了?谢谢答案。艾伦
最佳答案
var img = new Array(); // the simplest solution is to make img visible outside your function
function showPoints(){
// var img = new Array(); // HERE img is a LOCAL variable, only visible to the showPoints() function
var point = new Array();
var stage = new Kinetic.Stage({
container: 'cvsCroatia',
width: 574,
height: 508
});
关于javascript - 使用Kinetic在Canvas上添加更多图像,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15833794/