3

这是我目前的状态:http: //jsfiddle.net/andrewgable/Xr6mc/

    <!DOCTYPE HTML>
<html>
  <head>
    <style>
      body {
        margin: 0px;
        padding: 0px;
      }

    </style>
  </head>
  <body>
    <div id="container"></div>
    <script src="http://www.html5canvastutorials.com/libraries/kinetic-v4.3.0-beta2.js"></script>
    <script>
      var stage = new Kinetic.Stage({
        container: 'container',
        width: 578,
        height: 200
      });

      var lineLayer = new Kinetic.Layer();
      var flowerLayer = new Kinetic.Layer();
      var centerLayer = new Kinetic.Layer();

      var flower = new Kinetic.Group({
        x: stage.getWidth() / 2,
        y: stage.getHeight() / 2
      });

      // build stem
      var stem = new Kinetic.Line({
        strokeWidth: 10,
        stroke: 'green',
        points: [{
          x: flower.getX(),
          y: flower.getY()
        }, {
          x: stage.getWidth() / 2,
          y: stage.getHeight() + 10
        }]
      });

      // build center
      var center = new Kinetic.Circle({
        x: 0,
        y: 0,
        radius: 6,
        fill: 'black',
        draggable: true,
        x: stage.getWidth() / 2,
        y: stage.getHeight() / 2
      });

      center.on('mouseover', function() {
        this.setFill('orange');
        flowerLayer.draw();
        document.body.style.cursor = 'pointer';
      });

      center.on('mouseout', function() {
        this.setFill('black');
        flowerLayer.draw();
        document.body.style.cursor = 'default';
      });


      stage.on('mouseup', function() {
        document.body.style.cursor = 'default';
      });

      lineLayer.add(stem);
      flowerLayer.add(flower);
      centerLayer.add(center);
      stage.add(lineLayer);
      stage.add(flowerLayer);
      stage.add(centerLayer);

      // keep step and flower position in sync with center
      center.on('dragstart', (function() {
        center.getLayer().afterDraw(function() {
          stem.attrs.points[0] = center.getPosition();
          flower.setPosition(center.getPosition());
          lineLayer.draw();
          flowerLayer.draw();
        });
      }));

    </script>
  </body>
</html>

只是试图使这条线可拖动(简单,将其设置为拖动)。

我希望这条线有两个锚点,都可以向任何方向拖动。

正如你所看到的,我只需要做一个“锚”点。

如果没有锚点移动,我无法弄清楚使这成为可能的逻辑..

谢谢你的帮助。

4

2 回答 2

2

看看http://jsfiddle.net/bxGMw/

同样的逻辑可以从这个页面直接复制到你想要的。您可以只使用 buildAnchors 函数为形状创建锚点,并使用更新函数 redraw();

function buildAnchor(layer, x, y) {
            var anchor = new Kinetic.Circle({
                x: x,
                y: y,
                radius: 8,
                stroke: '#666',
                fill: '#ddd',
                strokeWidth: 2,
                draggable: true
            });

            // add hover styling
            anchor.on('mouseover', function() {
                document.body.style.cursor = 'pointer';
                this.setStrokeWidth(4);
                layer.draw();
            });
            anchor.on('mouseout', function() {
                document.body.style.cursor = 'default';
                this.setStrokeWidth(2);
                layer.draw();
            });

            layer.add(anchor);
            return anchor;
        }
于 2013-01-14T22:38:58.107 回答
0

将此代码放在最后并删除代码group1

stem.on('dragmove', (function () {
  //  stem.getLayer().afterDraw(function () {
  center.setPosition([stem.getPosition().x + stem.getPoints()[0].x, stem.getPosition().y + stem.getPoints()[0].y]);
  c2.setPosition([stem.getPosition().x + stem.getPoints()[1].x, stem.getPosition().y + stem.getPoints()[1].y]);
  flower.setPosition(center.getPosition());
  lineLayer.draw();
  centerLayer.draw();
  flowerLayer.draw();
  //  });
}));

var c2 = new Kinetic.Circle({
  x: 100,
  y: 100,
  radius: 6,
  fill: 'blue',
  draggable: true,
});
centerLayer.add(c2);
c2.on('dragstart', (function () {
  c2.getLayer().afterDraw(function () {
    stem.attrs.points[1].x = c2.getX()-stem.getX();
    stem.attrs.points[1].y = c2.getY()-stem.getY();
    lineLayer.draw();
    flowerLayer.draw();
  });
}));

也改变:

center.on('dragstart', (function () {
  center.getLayer().afterDraw(function () {
   stem.attrs.points[0].x = center.getX()-stem.getX();
   stem.attrs.points[0].y = center.getY()-stem.getY();    
   flower.setPosition(center.getPosition());
   lineLayer.draw();
   flowerLayer.draw();
  });
}));
于 2013-01-14T00:42:21.757 回答