我有一个HTML5画布,显示了许多图像和四个描述框。目前可以在画布上拖放图像,但我想添加一些功能,以便在将图像拖动到正确的描述框时将其删除。
我已经尝试编写以下函数,但它目前似乎没有做任何事情......即如果我将图像拖到其描述框并放下它,它仍然保留在画布上:
function initStage(images){
var stage = new Kinetic.Stage({
container: "container",
width: 1000,
height: 500
});
var descriptionLayer = new Kinetic.Layer();
//var imagesLayer = new Kinetic.Layer();
var allImages = [];
var currentScore = 0;
var descriptionBoxes = {
assetsDescriptionBox: {
x: 70,
y: 400
},
liabilitiesDescriptionBox: {
x: 300,
y: 400
},
incomeDescriptionBox: {
x: 530,
y: 400
},
expenditureDescriptionBox: {
x: 760,
y: 400
},
};
/*Code to detect whether image has been dragged to correct description box */
for (var key in sources){
/*Anonymous function to induce scope */
(function(){
var privateKey = key;
var imageSource = sources[key];
/*Check if image has been dragged to the correct box, and add it to that box's
array and remove from canvas if it has */
canvasImage.on("dragend", function(){
var descriptionBox = descriptionBoxes[privateKey];
if(!canvasImage.inRightPlace && isNearDescriptionBox(itemImage, descriptionBox)){
context.remove(canvasImage);
/*Will need to add a line in here to add the image to the box's array */
}
})
})();
}
}
我编写的代码基于我在http://www.html5canvastutorials.com/labs/html5-canvas-animals-on-the-beach-game-with-kineticjs/
找到的教程任何人都可以发现我做错了什么,以及如何确保图片在拖动到相应的描述框时从画布中移除?
答案 0 :(得分:1)
这个例子让我感到烦恼,因为它看起来很旧,所以我稍微编辑了一下......
http://jsfiddle.net/LTq9C/1/
...请记住,我不能肯定我所有的编辑都是最好的做事方式,我是新人和所有人;)
在这里,我再次对其进行了编辑,以显示正在删除的图像......
animal.on("dragend", function() {
var outline = outlines[privKey + "_black"];
if (!animal.inRightPlace && isNearOutline(animal, outline)) {
animal.remove();
animalLayer.draw();
}
});