Events 事件传递变量上的KineticJS

Events 事件传递变量上的KineticJS,events,canvas,kineticjs,Events,Canvas,Kineticjs,在jQuery中,可以将其他变量传递到事件中,以处理范围问题,如下所示: $('#element').click({index: i}, function(event){ console.log(event.data.index); }); 在KineticJS中如何实现这一点,因为on-event方法不提供此功能?我设法使用数组生成圆圈,但似乎无法将特定数据传递给每个事件: var data = [12.22, 34.45, 8.9]; var circles = []; for(

在jQuery中,可以将其他变量传递到事件中,以处理范围问题,如下所示:

$('#element').click({index: i}, function(event){
    console.log(event.data.index);
});
在KineticJS中如何实现这一点,因为on-event方法不提供此功能?我设法使用数组生成圆圈,但似乎无法将特定数据传递给每个事件:

var data = [12.22, 34.45, 8.9];
var circles = [];

for(var i in data){
circles[i] = new Kinetic.Circle({
    x : Math.random() * stage.getWidth(),
    y : Math.random() * stage.getHeight(),
    radius : 4,
    fill : 'white'
});

circles[i].on('mousemove', {value: data[i]}, function() {
    console.log('need to use value for this particular event here');
});

shapesLayer.add(circles[i]);
}

可以向circle添加新属性,并将数据值保存在其中,如下所示:

circles[i].setAttr('dataVal',data[i])

然后你可以像这样在事件处理程序中访问它

circles[i].on('mousemove', function() {
    console.log(this.getAttr(`dataVal`));
});

谢谢@Ani。当我发布这个问题时,我开始思考,我可能只是“注入”一个属性到对象中,然后像那样访问它。从编码标准来看,您的方法更好,但下面的方法也适用,即使它很粗糙:

circles[i].value = data[i];

circles[i].on('mouseover', function() {
    console.log(this.value);
});

回答得很好,非常有帮助。谢谢你的发帖。