【问题标题】:FabricJs - How to reevaluate the points within an object after the object is rotatedFabricJs - 如何在对象旋转后重新评估对象内的点
【发布时间】:2020-05-28 09:18:11
【问题描述】:

var canvas = new fabric.Canvas("c", {
  selection: false
});

var refRect = new fabric.Rect({
  left: 250,
  top: 150,
  width: 200,
  height: 100,
  fill: 'transparent',
  stroke: 'blue',
  originX: 'center',
  originY: 'center'
});

canvas.add(refRect);

var refPoints = [
{ x: 0, y: 0 }, { x: 200, y: 0 }, { x: 0, y: 100 },
  { x: 200, y: 100 }, { x: 30, y: 50 },
  { x: 50, y: 30 }

];

refPoints.map(function(point) {
  return new fabric.Circle({
    left: refRect.left - (refRect.width / 2) + point.x,
    top: refRect.top - (refRect.height / 2) + point.y,
    radius: 10,
    fill: 'blue',
    originX: 'center',
    originY: 'center'
  });
}).forEach(function(refPoint) {
  canvas.add(refPoint);

});


var tarRect = new fabric.Rect({
    left: 250, top: 150,
    width: 200, height: 100,
    fill: 'transparent',
    angle:45,
    stroke: 'red',
    originX: 'center',
    originY: 'center'
});

canvas.add(tarRect);

var matrix = tarRect.calcTransformMatrix();

refPoints.map(function(point) {
  return new fabric.Circle({
    left: tarRect.left - (tarRect.width / 2) + point.x,
    top: tarRect.top - (tarRect.height / 2) + point.y,
    radius: 5,
    fill: 'red',
    originX: 'center',
    originY: 'center'
  });
}).forEach(function(refPoint) {
  canvas.add(refPoint);
});
canvas {
  border: 1px solid;
}
<script src="https://cdn.jsdelivr.net/npm/fabric@1.7.11/dist/fabric.js"></script>
<canvas id="c" width="500" height="300"></canvas>

我有一个织物矩形对象和一堆相对于矩形的点。我能够使用简单的逻辑在矩形内定位点

现在如果我将矩形对象旋转一个角度,有没有办法评估点相对于旋转矩形的位置。

我不是数学专家,所以我需要一些指导,请帮忙。

【问题讨论】:

  • 您希望您的点在旋转后附加到矩形上吗?

标签: javascript fabricjs coordinate-transformation


【解决方案1】:

也许您可以使用结构组而不是您的数学逻辑。

 const group = new fabric.Group([...points, rect]);

 canvas.add(group);

我准备了一个示例来详细说明我的解决方案,您可以在此处查看 https://codesandbox.io/s/mutable-bash-k01wv?file=/src/index.js

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2020-07-04
  • 2018-04-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-16
  • 1970-01-01
  • 2017-03-24
相关资源
最近更新 更多