只是让你的代码更简单:
//// INIT CANVAS
var stage = new Konva.Stage({
container : "container",
width : 400,
height : 300
});
var layer = new Konva.Layer();
stage.add(layer);
/////MENU
var pics = ["http://konvajs.github.io/assets/lion.png", "http://konvajs.github.io/assets/monkey.png"];
var $menu = $('#menu');
$.each(pics, function( index, value ) {
$("<img/>") // create image
.attr('src', value) // set src to image link
.appendTo($menu)
.on('click', function() {
// this here is image object
var src = this.src;
// create new Konva.Image from src attribute
Konva.Image.fromURL(src, function(image) {
// make it draggable
image.setAttrs({
draggable: true
});
// append to layer
layer.add(image);
// update layer
layer.draw();
})
});
});
演示:https ://jsfiddle.net/6tnb2q2q/ (点击图片添加到舞台)