使用面料我正在尝试使用两个圆形端点创建一条线。我可以在端点处移动圆圈并更新线条。但是,如果我移动线圈,则圆圈不会移动。有什么建议吗?
JSFiddle here http://jsfiddle.net/gfk0r2pf/10/
Value
答案 0 :(得分:1)
我已经通过你的小提琴解决了问题,请检查一下。现在它似乎工作.. 检查一下http://jsfiddle.net/Ahammadalipk/gfk0r2pf/14/
Change your code as below..
var self = this;
var canvas = new fabric.Canvas('c', {
selection: true
});
var line = new fabric.Line([50, 50, 100, 100], {
fill: 'red',
stroke: 'red',
strokeWidth: 2,
selectable: true,
hasControls: false,
hasBorders: false,
centeredRotation: false,
centeredScaling: false,
//originX: 'center',
//originY: 'center'
});
var circle1 = new fabric.Circle({
radius: 5,
fill: 'green',
left: 45,
top: 45,
hasControls: false,
hasBorders: false,
name: 'circle1'
});
var circle2 = new fabric.Circle({
radius: 5,
fill: 'green',
left: 95,
top: 95,
hasControls: false,
hasBorders: false,
name: 'circle2'
});
canvas.on('object:moving', function (options) {
var objType = options.target.get('type');
var p = options.target;
if (objType == 'line') {
var _l = line.left;
var _t = line.top;
circle1.set({
'left': (line.calcLinePoints().x1 + _l),
'top': (line.calcLinePoints().y1 + _t)
});
circle1.line.set({
'x1': circle1.left,
'y1': circle1.top
});
circle1.line.setCoords();
circle2.set({
'left': (line.calcLinePoints().x2 + _l),
'top': (line.calcLinePoints().y2 + _t)
});
circle2.line.set({
'x2': circle2.left,
'y2': circle2.top
});
circle2.line.setCoords();
canvas.renderAll();
}
if (objType == 'circle') {
if (p.name == 'circle1') {
line.set({
x1: circle1.getCenterPoint().x, y1: circle1.getCenterPoint().y, selectable: true
});
} else {
if (p.name == 'circle2') {
line.set({
x2: circle2.getCenterPoint().x, y2: circle2.getCenterPoint().y, selectable: true
});
}
}
}
line.setCoords();
circle1.setCoords();
circle2.setCoords();
canvas.renderAll();
});
canvas.add(line);
circle1.line=line;
circle2.line=line;
canvas.add(circle1);
canvas.add(circle2);
canvas.renderAll();
答案 1 :(得分:0)
您根据圆/线的初始位置进行计算。 需要计算每种情况下的每个位置。
请参阅小提琴:https://jsfiddle.net/251a9pog
我将circle1的颜色更改为蓝色,以查看两个圆圈的位置是否不同。
var self = this;
var canvas = new fabric.Canvas('c', {
selection: true
});
var line = new fabric.Line([50, 50, 100, 100], {
fill: 'red',
stroke: 'red',
strokeWidth: 2,
selectable: true,
hasControls: false,
hasBorders: false,
centeredRotation: false,
centeredScaling: false
});
var circle1 = new fabric.Circle({
radius: 5,
fill: 'blue',
left: 45,
top: 45,
hasControls: false,
hasBorders: false,
name: 'circle1'
});
var circle2 = new fabric.Circle({
radius: 5,
fill: 'green',
left: 95,
top: 95,
hasControls: false,
hasBorders: false,
name: 'circle2'
});
canvas.on('object:moving', function (options) {
var objType = options.target.get('type');
var p = options.target;
if (objType == 'line') {
var c1Left, c1Top, c2Left, c2Top;
// CALCULATE THE circle1 AND circle2 POINTS FOR EACH SCENARIO
if (circle1.top < circle2.top) {
if (circle1.left < circle2.left) {
c1Left = p.left - circle1.radius;
c1Top = p.top - circle1.radius;
c2Left = p.left + p.width - circle2.radius;
c2Top = p.top + p.height - circle2.radius;
}
else {
c1Left = p.left + p.width - circle1.radius;
c1Top = p.top - circle1.radius;
c2Left = p.left - circle1.radius;
c2Top = p.top + p.height - circle1.radius;
}
}
else {
if (circle1.left < circle2.left) {
c1Left = p.left - circle1.radius;
c1Top = p.top + p.height - circle1.radius;
c2Left = p.left + p.width - circle1.radius;
c2Top = p.top - circle1.radius;
}
else {
c1Left = p.left + p.width - circle1.radius;
c1Top = p.top + p.height - circle1.radius;
c2Left = p.left - circle1.radius;
c2Top = p.top - circle1.radius;
}
}
circle1.set({ left: c1Left, top: c1Top });
circle2.set({ left: c2Left, top: c2Top });
}
if (objType == 'circle') {
if (p.name == 'circle1') {
line.set({
// moving circle left + radius
x1: p.left + p.radius,
// moving circle top + radius
y1: p.top + p.radius,
// other circle left + radius
x2: circle2.left + circle2.radius,
// other circle top + radius
y2: circle2.top + circle2.radius,
selectable: true
});
} else if (p.name == 'circle2') {
line.set({
// moving circle left + radius
x1: p.left + p.radius,
// moving circle top + radius
y1: p.top + p.radius,
// other circle left + radius
x2: circle1.left + circle1.radius,
// other circle top + radius
y2: circle1.top + circle1.radius,
selectable: true
});
}
}
line.setCoords();
circle1.setCoords();
circle2.setCoords();
canvas.renderAll();
});
canvas.add(line);
canvas.add(circle1);
canvas.add(circle2);
canvas.renderAll();
答案 2 :(得分:0)
我们可以计算线拖动距离并将该距离值添加到圆圈当前值/
请看小提琴:https://jsfiddle.net/LordWolfer/2pf0Lm81/99/
var self = this;
var canvas = new fabric.Canvas('c', {
selection: true
});
var line = new fabric.Line([50, 50, 100, 100], {
fill: 'red',
stroke: 'red',
strokeWidth: 2,
selectable: true,
hasControls: false,
hasBorders: false,
centeredRotation: false,
centeredScaling: false,
perPixelTargetFind: true,
});
var circle1 = new fabric.Circle({
radius: 5,
fill: 'green',
left: 45,
top: 45,
hasControls: false,
hasBorders: false,
name: 'circle1'
});
var circle2 = new fabric.Circle({
radius: 5,
fill: 'green',
left: 95,
top: 95,
hasControls: false,
hasBorders: false,
name: 'circle2'
});
let diffX = 0,
diffY = 0,
circle1Left = circle1.left,
circle1Top = circle1.top,
circle2Left = circle2.left,
circle2Top = circle2.top;
canvas.on('object:moving', function(options) {
var objType = options.target.get('type');
var p = options.target;
var t = options.transform;
if (objType == 'line') {
var _l = options.pointer.x;
var _t = options.pointer.y;
diffX = (options.pointer.x - t.ex);
diffY = (options.pointer.y - t.ey);
circle1.set({
'left': (circle1Left + diffX),
'top': (circle1Top + diffY)
});
circle1.line.set({
'x1': circle1.left +5,
'y1': circle1.top +5
});
circle1.line.setCoords();
circle2.set({
'left': (circle2Left + diffX),
'top': (circle2Top + diffY)
});
circle2.line.set({
'x2': circle2.left + 5,
'y2': circle2.top +5
});
circle2.line.setCoords();
canvas.renderAll();
}
else if (objType == 'circle') {
if (p.name == 'circle1') {
line.set({
x1: circle1.getCenterPoint().x,
y1: circle1.getCenterPoint().y,
selectable: true
});
} else {
if (p.name == 'circle2') {
line.set({
x2: circle2.getCenterPoint().x,
y2: circle2.getCenterPoint().y,
selectable: true
});
}
}
circle1Left = circle1.left,
circle1Top = circle1.top,
circle2Left = circle2.left,
circle2Top = circle2.top;
}
line.setCoords();
circle1.setCoords();
circle2.setCoords();
canvas.renderAll();
});
canvas.add(line);
circle1.line = line;
circle2.line = line;
canvas.add(circle1);
canvas.add(circle2);
canvas.renderAll();