原始问题
我想知道为什么我无法从此代码中的圆圈中获取“dragstart”消息...
拖动时我无法收到“圈子”的消息。我尝试了一些来自http://www.html5canvastutorials.com的工作代码。在此代码中:http://www.html5canvastutorials.com/labs/html5-canvas-drag-and-drop-resize-and-invert-images/,它正在运行。
jsfiddle:http://jsfiddle.net/zoutepopcorn/YXJpH/
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<style>
body {
margin: 0px;
padding: 0px;
}
</style>
</head>
<body>
<div id="container"></div>
<script src="http://www.html5canvastutorials.com/libraries/kinetic-v4.1.2.js"></script>
<script src="setting.js"></script>
<script src="imgStyle.js"></script>
<script>
var stage;
var layer;
setupStage();
function setupStage() {
stage = new Kinetic.Stage({
container: "container",
width: 800,
height: 800
});
layer = new Kinetic.Layer();
messageLayer = new Kinetic.Layer();
stage.add(messageLayer);
}
function writeMessage(messageLayer, message) {
var context = messageLayer.getContext();
messageLayer.clear();
context.font = "18pt Calibri";
context.fillStyle = "black";
context.fillText(message, 10, 25);
}
function drawImage(imageObj) {
var darthVaderImg = new Kinetic.Image({
image: imageObj,
x: 100,
y: 100,
width: 200,
height: 137,
draggable: true
});
var group = new Kinetic.Group({ draggable: true });
group.add(darthVaderImg);
var circle = new Kinetic.Circle({ x: 105, y: 105, radius: 10, fill: 'red', stroke: 'black', name: 'circle', strokeWidth: 4, draggable: true });
group.add(circle)
group.on("dragstart", function() { writeMessage(messageLayer, "group"); } );
circle.on("dragstart", function() { writeMessage(messageLayer, "circle"); // <--- does not work in the GROUP!!! } );
layer.add(group);
stage.add(layer);
stage.add(messageLayer);
stage.draw();
}
var imageObj = new Image();
imageObj.onload = function() {
drawImage(imageObj);
};
imageObj.src = 'http://www.html5canvastutorials.com/demos/assets/darth-vader.jpg';
</script>
</body>
</html>
答案 0 :(得分:2)
您没有看到来自圈子的消息,因为圈子是群组的一部分,因此,只有一个事件“dragstart”可以在群组或圈子上识别,当您尝试移动圈子时,事件被识别对于该组,圆圈是该组的一部分。您可以在与“dragstart”相关联的功能中为组添加一个检查,以检查所选对象是否为“圈”并显示您的消息。