javascript - KineticJS:两个可拖动形状之间的曲线

标签 javascript canvas html5-canvas kineticjs konvajs

我在我的项目中使用 KineticJS。我需要使用曲线连接两个形状。可以拖动其中一个形状。我能够在形状之间放置曲线。当用户开始拖动形状时就会出现问题。要求是它应该适当弯曲(请参阅屏幕截图),无论它们之间的距离以及它们相对于彼此的位置如何。我正在这样做:

var utils = {
  _getCenter: function(x1, y1, x2, y2) {
    return {
      x: (x1 + x2) / 2,
      y: (y1 + y2) / 2
    }
  },
  // Converts from degrees to radians.
  _radians: function(degrees) {
    return degrees * Math.PI / 180;
  },
  // Converts from radians to degrees.
  _degrees: function(radians) {
    return radians * 180 / Math.PI;
  }
};

function amplitude(point) {
  var rad_90 = utils._radians(90);
  var rad_45 = utils._radians(45);
  var rad_60 = utils._radians(60);
  console.log(rad_90);
  return {
    x: point.x * Math.cos(rad_60),
    y: point.y * Math.sin(rad_60)
  };
}
var width = window.innerWidth;
var height = window.innerHeight;

var stage = new Kinetic.Stage({
  container: 'container',
  width: width,
  height: height
});

var layer = new Kinetic.Layer();

var circle = new Kinetic.Circle({
  x: stage.getWidth() / 2,
  y: stage.getHeight() / 2,
  radius: 20,
  fill: 'red',
  stroke: 'black',
  strokeWidth: 2
});
var attachedCircle = new Kinetic.Circle({
  x: stage.getWidth() / 4,
  y: stage.getHeight() / 4,
  radius: 20,
  fill: 'red',
  stroke: 'black',
  strokeWidth: 2,
  draggable: true
});
var center = amplitude(utils._getCenter(circle.getX(), circle.getY(), attachedCircle.getX(), attachedCircle.getY()));

var line = new Kinetic.Line({
  points: [circle.getX(), circle.getY(), center.x, center.y, attachedCircle.getX(), attachedCircle.getY()],
  fill: 'black',
  stroke: 'green',
  strokeWidth: 3,
  /*
   * line segments with a length of 33px
   * with a gap of 10px
   */
  dash: [33, 10],
  id: 'line',
  tension: 0.5
});
attachedCircle.on('dragmove', function(e) {
  var targetCircle = e.target;
  var tempCenter = amplitude(utils._getCenter(circle.getX(), circle.getY(), targetCircle.getX(), targetCircle.getY()));
  console.log(tempCenter);
  line.setPoints([circle.getX(), circle.getY(), tempCenter.x, tempCenter.y, targetCircle.getX(), targetCircle.getY()]);
});




// add the shape to the layer
layer.add(line);

layer.add(attachedCircle);
layer.add(circle);
// add the layer to the stage
stage.add(layer);

我不知道我错过了什么。我创建了plunkr for this

最佳答案

要定义幅度函数,您需要使用两个输入点:

function amplidure2(p1, p2) {
  var alpha = Math.atan((p1.x - p2.x) / (p1.y - p2.y)) + Math.PI / 2;
  if (p1.y < p2.y) {
    alpha += Math.PI;
  }
  var center = utils._getCenter(p1.x, p1.y, p2.x, p2.y);
  var r = 50;
  return {
    x: center.x + r * Math.sin(alpha),
    y: center.y + r * Math.cos(alpha)
  }
}

DEMO

关于javascript - KineticJS:两个可拖动形状之间的曲线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33736115/

相关文章:

javascript - 保存行程样式时出现问题

canvas - 在 JavaFX 中使用 MouseEvent 和 MouseClicked 选择并移动 Canvas 图像

javascript - 在 "for in"循环中访问子对象?

javascript - 在 <span> onClick 中选择文本

javascript - 错误 : [ng:areq] Argument 'demoCtrl' is not a function, 未定义

javascript - kineticjs中如何通过json在舞台上绘制图层

javascript - 增加 HTML5 Canvas 分辨率

javascript - 我可以以某种方式将当前运行的 javascript 函数记录到 Chrome 开发者控制台中吗?

javascript - 在新窗口或选项卡中打开 AJAX 链接

javascript - html Canvas 动画闪烁