5

更新的工作小提琴

原始问题

我想知道为什么我无法从这段代码中的圆圈中收到带有“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>
4

1 回答 1

2

您看不到来自圈子的消息,因为圈子是组的一部分,因此,在组或圈子上只能识别事件“dragstart”之一,当您尝试移动圈子时,该事件会被组识别因为圆圈是该组的一部分。您可能可以在与“dragstart”关联的函数中添加一个检查,以检查所选对象是否为“圆圈”并显示您的消息。

于 2012-12-04T22:46:28.783 回答