-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix group initialization #2101
Fix group initialization #2101
Conversation
I'm OK with this PR but I'd love to have 2 things before merging:
|
@kangax Thank you for your reply! |
Sorry, in #2095, @chrisrickard reported a bug of this PR. |
|
I detect the issue #926 which produce this bug, and it is merged #1868 in Dec, 2014. The proposed code in this PR is consistent with after #1868 about a initialization. For better consistency for parsing a json data which is produced before #1868, we need to fix function 2nd proposition:
to
,or, 3rd proposition:
to
These (2nd or 3rd) fix would be more stronger than this PR for changing delegatedProperties at later version. I vacillate between them, because it is a problem about consistency with a previous version. I'm sorry for long post. |
I am sorry for changing very often. I choose the 3rd proposition in previous comment. |
@sapics can i ask you why you remove - options = options || { }; in favor of + if (options) { I imagine and undestand than creating i think it was made like that to avoid checking and more code nesting and indent level. Did you change for a matter of efficency or something else? |
i add, before merging, we should consider this: does this make sense:
Do we expect the group to have objects filled with red or not? Do we expect the group to have objects filled with red or not? json loading back is still a problem that we have to take care of. I m really asking, not pretending i know the answer. |
@asturur Thank you for asking! V1.4.13 and before: not fill red to objects. After v1.5.0 situation is done by request of #926, I feel that it is not expected, because #926 talks only about |
but in normal code after initialization, a red fill will fill all. and this will stay, right? Do you think is possible to handle this where the problem came out, in the load from json of group? would an hack, like disabling temporarly the delegated properties when loading back from json or from object, be enough? or too bad? i think that if delegated properties exist we should use them also in initialize. i m sharing thoughts. |
Yes, Sorry, I was thinking only about consistency between versions. Therefore, you proposed that remove delegatedProperties at |
yes but before make you waste time, i will look to see if it is possible and where and propose here. |
Thanks a lot! If we fix only
|
Ok finally on computer, i can explain myself better. Something like this, could solve both:
fabric.Group.fromObject = function(object, callback) {
fabric.util.enlivenObjects(object.objects, function(enlivenedObjects) {
delete object.objects;
- callback && callback(new fabric.Group(enlivenedObjects, object));
+ callback && callback(new fabric.Group(enlivenedObjects, object, true)); //name to be decided
});
}; initialize: function(objects, options, skipPropagation) {
options = options || { };
+ skipPropagation && this.callSuper('initialize', options);
this._objects = objects || [];
for (i = this._objects.length; i--; ) {
this._objects[i].group = this;
}
this.originalState = { };
if (options.originX) {
this.originX = options.originX;
}
if (options.originY) {
this.originY = options.originY;
}
this._calcBounds();
this._updateObjectsCoords();
- this.callSuper('initialize', options);
+ !skipPropagation && this.callSuper('initialize', options); |
Yeah! because of _calcBounds will overwrite the dimension we just parsed. top, left, are overwritten, width and height are recalculated same. If we are restoring from object, and so if skipPropagation is true ( and so the name sucks, it should be something like
this may be too much skipping... and create other errors. - this.set(this._getBounds(aX, aY, onlyWidthHeight));
+ var dimensions = this._getBounds(aX, aY, onlyWidthHeight)
+ this.set('width', dimensions.width);
+ this.set('height', dimensions.height);
+ if (/*check for left not defined in this*/) -> this.set('left', dimensions.left);
+ if (/*check for top not defined in this*/) -> this.set('top', dimensions.top); |
Your first suggestion looks very cool!! Now, I am in home. I will do coding tommorow! |
I add commit with your first idea,
Thank you very much! It pass all of unit test! I am torn between we need to setCoords to objects or not, like
|
i think we need to call _updateObjectsCoords always, i would not avoid it Is nearly done. https://github.com/kangax/fabric.js/pull/2101/files#diff-b290a59d2e985ee793daf97398d5688dR105 (we need to squeeze those commits when the fix is done, can you use 'git rebase -i' ? if not i can explain you how) |
@@ -349,7 +352,7 @@ | |||
this._setObjectPosition(object); | |||
|
|||
object.setCoords(); | |||
object.hasControls = object.__origHasControls; | |||
object.hasControls = !!object.__origHasControls; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What does !! mean?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we can use _updateObjectsCoords
, this fix is unnecessary.
If we cannot use _updateObjectsCoords
, when we use loadFromJSON
from json created by older version of fabric.js, _objects have no __origHasControls
.
Therefore, I wish to not object.hasControls = undefined
, but object.hasControls = !!undefined = false
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok i learned something new!
I think we have to use _updateObjectsCoords... i need time to test it.
Thank you for analizing! Thank you for your kindness! I know new feature of git! git is really difficult, but much fun! Sorry, I am rookie in git and github... Thank you very much for many helps!! |
9cb497e
to
d1b8a11
Compare
9f44710
to
e21f1f4
Compare
@asturur Now version is uploaded to https://jsfiddle.net/sapics/mq00xq3y/122/. This version dose not use _updateObjectsCoords. |
I have this PR in my list of things to finish ASAP. |
https://jsfiddle.net/asturur/mq00xq3y/125/ Adding this to show that this indeed fixes also #2119. |
@sapics, this is another proposal, is just a matter of code style more than some meaningfull changes. What do you think about it? i still did not test it. i'm going out now just want to share this - initialize: function(objects, options) {
+ initialize: function(objects, options, instanceFromObj) {
options = options || { };
+ this._objects = [];
+ instanceFromObj && this.callSuper('initialize', options);
this._objects = objects || [];
- for (var i = this._objects.length; i--; ) {
- this._objects[i].group = this;
- }
this.originalState = { };
if (options.originX) {
this.originX = options.originX;
}
if (options.originY) {
this.originY = options.originY;
}
+ if (!instanceFromObj) {
this._calcBounds();
- this._updateObjectsCoords();
this.callSuper('initialize', options);
+ }
+ this._updateObjectsCoords(instanceFromObj);
this.setCoords();
this.saveCoords();
},
/**
* @private
*/
- _updateObjectsCoords: function() {
- this.forEachObject(this._updateObjectCoords, this);
+ _updateObjectsCoords: function(instanceFromObj) {
+ this.forEachObject(this._updateObjectCoords, this, instanceFromObj);
},
/**
* @private
*/
- _updateObjectCoords: function(object) {
+ _updateObjectCoords: function(object, instanceFromObj) {
+ if (!instanceFromObj) {
var objectLeft = object.getLeft(),
objectTop = object.getTop(),
center = this.getCenterPoint();
object.set({
originalLeft: objectLeft,
originalTop: objectTop,
left: objectLeft - center.x,
top: objectTop - center.y
});
+ }
- //cannot we set object.group here?
+ object.group = this;
object.setCoords();
// do not display corners of objects enclosed in a group
object.__origHasControls = object.hasControls;
object.hasControls = false;
}, |
@asturur I am sorry for answer late, i was going out. Thank you for your new proposal! It looks much simpler and it might work well! I guess that |
yes we can live group association to object in initialize. but the other change to me looks good. i would like to hear also @kangax opinion about those changes. so we can merge it |
} | ||
else { | ||
for (i = this._objects.length; i--; ) { | ||
this._objects[i].setCoords(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can't we call _updateObjectCoords here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes we can! I would fix by @asturur suggestion.
d35b5ad
to
746b87d
Compare
* @return {Object} thisArg | ||
*/ | ||
initialize: function(objects, options) { | ||
initialize: function(objects, options, alreadyGrouped) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
alreadyGrouped
→ isAlreadyGrouped
@sapics Left couple comments. Please update and we're good to go! Thanks again. |
746b87d
to
e07350c
Compare
+changelog |
#2095 @chrisrickard reported a problem of
group.toObject
. This PR would fix it.There are two fixed points.
I removed delegatedProperties at toObject function, but it is also possible to fix by changing group initialization code. I felt that fix at toObject will be better for consistency.
2. At toObject, _objects copied normally. However, at initialization, _objects coordinates are updated (https://github.com/kangax/fabric.js/blob/master/src/shapes/group.class.js#L70).It makes change position of grouped objects.
To fix it, I use _restoreObjectsState, before copying _objects.
closes #2119
closes #2095