Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
272 views
in Technique[技术] by (71.8m points)

javascript - How to get the canvas-relative position of an object that is in a group?

Normally an object's position relative to the canvas can be gotten from it's .left and .top attributes, but these become relative to the group if the object is in a selection/group. Is there a way to get their position relative to the canvas?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

When an object is inside a group, its coordinates relative to the canvas will depend on the origin of the group (and origin of the object as well).

Lets say we have this code which has a rect and a circle added to a group.

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.5)'
});
var circle = new fabric.Circle({
    radius: 50,
    left: 175,
    top: 75,
    fill: '#aac'
});

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

Below are the three cases possible for centering the group:

  1. Origin of group set to center(as in the code above):

    As shown in the figure below, rect.left gives us distance of left of object from center of the group. rect.group.left gives us the distance of center of group from left of canvas.

    So distance of rect from left of canvas = rect.left + rect.group.left (http://jsfiddle.net/uue3hcj6/3/)

    enter image description here

  2. Origin of group is set to top/left (also the default setting)

    rect.left gives us distance of left of object from center of the group. rect.group.left gives us the distance of left of group from left of canvas. Now to calculate the remaining distance, we have to add half of width of the group.

    So distance of rect from left of canvas = rect.left + rect.group.left + rect.group.width/2 (http://jsfiddle.net/uue3hcj6/6/)

    enter image description here

  3. Origin of group is set to bottom/right

    rect.left gives us distance of left of object from center of the group. rect.group.left gives us the distance of right of group from left of canvas. Now to calculate the total distance, we have to subtract the half of width of the group.

    So distance of rect from left of canvas = rect.left + rect.group.left - rect.group.width/2(http://jsfiddle.net/uue3hcj6/7/)

    enter image description here

Note: Similar cases are possible for object as well.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...