gpt4 book ai didi

fabricjs - 如何获得组fabricjs内对象的左上角

转载 作者:行者123 更新时间:2023-12-03 14:43:48 28 4
gpt4 key购买 nike

如何将组内三角形的 Canvas 相对位置(顶部,左侧)作为波纹管图像?

我关注了这个话题:How to get the canvas-relative position of an object that is in a group?但只有在组不轮换时才正确。

enter image description here

最佳答案

您可以在这里找到工作示例:http://jsfiddle.net/mmalex/2rsevdLa/

fabricJs: calculate canvas space coordinates of child object in the group

Fabricjs 提供了如何将转换应用于对象的全面解释:http://fabricjs.com/using-transformations

快速回答:组内对象的坐标是一个点 [0,0],与组中对象的变换方式完全相同。

按照我在代码中的评论来了解这个想法。

    // 1. arrange canvas layout with group of two rectangles
var canvas = new fabric.Canvas(document.getElementById('c'));

var rect = new fabric.Rect({
width: 100,
height: 100,
left: 50,
top: 50,
fill: 'rgba(255,0,0,0.25)'
});
var smallRect = new fabric.Rect({
width: 12,
height: 12,
left: 150 - 12,
top: 50 + 50 - 12 / 2 - 10,
fill: 'rgba(250,250,0,0.5)'
});

// 2. add a position marker (red dot) for visibility and debug reasons
var refRect = new fabric.Rect({
width: 3,
height: 3,
left: 100,
top: 100,
fill: 'rgba(255,0,0,0.75)'
});

var group = new fabric.Group([rect, smallRect], {
originX: 'center',
originY: 'center'
});
canvas.add(group);
canvas.add(refRect);
canvas.renderAll();

// 3. calculate coordinates of child object in canvas space coords
function getCoords() {
// get transformation matrixes for object and group individually
var mGroup = group.calcTransformMatrix(true);

// flag true means that we need local transformation for the object,
// i.e. how object is positioned INSIDE the group
var mObject = smallRect.calcTransformMatrix(true);

console.log("group: ", fabric.util.qrDecompose(mGroup));
console.log("rect: ", fabric.util.qrDecompose(mObject));

// get total transformattions that were applied to the child object,
// the child is transformed in following order:
// canvas zoom and pan => group transformation => nested object => nested object => etc...
// for simplicity, ignore canvas zoom and pan
var mTotal = fabric.util.multiplyTransformMatrices(mGroup, mObject);
console.log("total: ", fabric.util.qrDecompose(mTotal));

// just apply transforms to origin to get what we want
var c = new fabric.Point(0, 0);
var p = fabric.util.transformPoint(c, mTotal);
console.log("coords: ", p);
document.getElementById("output").innerHTML = "Coords: " + JSON.stringify(p);

// do some chores, place red point
refRect.left = p.x - 3 / 2;
refRect.top = p.y - 3 / 2;
canvas.bringToFront(refRect);
canvas.renderAll();
}

关于fabricjs - 如何获得组fabricjs内对象的左上角,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52697422/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com