我正在使用FabricJS应用,我希望用户通过简单的单击即可将对象移动到画布原点,即使在旋转或缩放对象或一组对象之后也是如此。 为此,在单击时,当用户单击时,我将执行以下代码:
obj.left= obj.getBoundingRect().width/2;
obj.top=obj.getBoundingRect().height/2;
它非常适合矩形或文本,但是在使用形状,svg甚至对象组时根本不起作用。我不知道为什么!
下面是完整的代码。
重要提示:请在浏览器中启用跨域访问,以加载svg。
this.__canvas = new fabric.Canvas('meCanvas', {
preserveObjectStacking: true,
height: 400,
width: 400,
backgroundColor: '#1F1F1F',
canvasKey:'azpoazpoaz'
});
let rect = new fabric.Rect({
fill: 'red',
width: 200,
height: 100,
left: 100,
top: 100,
originX: 'center',
originY: 'center',
fontWeight: 'normal'
});
let text = new fabric.IText('Text', {
fontFamily: 'Times',
fontSize: 18,
fill: 'white',
left: 100,
top: 100,
originX: 'center',
originY: 'center',
fontWeight: 'normal'
});
let url = 'https://svgshare.com/i/9DX.svg';
fabric.loadSVGFromURL(url,(objects,options)=> {
let loadedObjects = fabric.util.groupSVGElements(objects, options);
loadedObjects.left=50;
loadedObjects.top=150;
this.__canvas.add(loadedObjects);
});
let url2 = 'https://svgshare.com/i/9Cp.svg';
fabric.loadSVGFromURL(url2,(objects,options)=> {
let loadedObjects = fabric.util.groupSVGElements(objects, options);
loadedObjects.left=100;
loadedObjects.top=200;
loadedObjects.angle=20;
this.__canvas.add(loadedObjects);
});
this.__canvas.add(rect);
this.__canvas.add(text);
this.__canvas.renderAll();
$('#pushorigin').click((e)=>{
let obj = this.__canvas.getActiveObject();
obj.left= obj.getBoundingRect().width/2;
obj.top=obj.getBoundingRect().height/2;
this.__canvas.requestRenderAll();
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/2.4.3/fabric.js"></script>
<script src="https://code.jquery.com/jquery-3.3.1.js"></script>
<div style='display: inline-block'>
<div>
<canvas id='meCanvas' ref='meFabric'/>
</div>
<div>
<button id='pushorigin'>Push to origin</button>
</div>
</div>
如果选择文本或矩形,然后单击“推到原点”按钮,它们将转到原点(左上角)。但是,相同的操作不适用于svg(三角形和多边形),而且如果我们拉伸这些svg甚至旋转它们,甚至会变得更糟,它们将无法到达原点。使用一组对象时也是一样!
我不知道该怎么办,我迷路了。我已经搜索了几天,什么也没找到。
请帮助!
答案 0 :(得分:1)
为所有对象设置originX: 'center',originY: 'center'
,您的解决方案将起作用。
this.__canvas = new fabric.Canvas('meCanvas', {
preserveObjectStacking: true,
height: 400,
width: 400,
backgroundColor: '#1F1F1F',
canvasKey: 'azpoazpoaz'
});
let rect = new fabric.Rect({
fill: 'red',
width: 200,
height: 100,
left: 100,
top: 100,
originX: 'center',
originY: 'center',
fontWeight: 'normal'
});
let text = new fabric.IText('Text', {
fontFamily: 'Times',
fontSize: 18,
fill: 'white',
left: 100,
top: 100,
originX: 'center',
originY: 'center',
fontWeight: 'normal'
});
let url = 'https://svgshare.com/i/9DX.svg';
fabric.loadSVGFromURL(url, (objects, options) => {
let loadedObjects = fabric.util.groupSVGElements(objects, options);
loadedObjects.set({
left: 50,
top: 150,
originX: 'center',
originY: 'center'
})
this.__canvas.add(loadedObjects);
});
let url2 = 'https://svgshare.com/i/9Cp.svg';
fabric.loadSVGFromURL(url2, (objects, options) => {
let loadedObjects = fabric.util.groupSVGElements(objects, options);
loadedObjects.set({
left: 100,
top: 200,
angle: 20,
originX: 'center',
originY: 'center'
})
this.__canvas.add(loadedObjects);
});
this.__canvas.add(rect);
this.__canvas.add(text);
this.__canvas.renderAll();
$('#pushorigin').click((e) => {
let obj = this.__canvas.getActiveObject();
obj.set({
left: obj.getBoundingRect().width / 2,
top: obj.getBoundingRect().height / 2
}).setCoords()
this.__canvas.requestRenderAll();
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/2.4.3/fabric.js"></script>
<script src="https://code.jquery.com/jquery-3.3.1.js"></script>
<div style='display: inline-block'>
<div>
<canvas id='meCanvas' ref='meFabric'/>
</div>
<div>
<button id='pushorigin'>Push to origin</button>
</div>
</div>