Skip to content

Commit

Permalink
patch2
Browse files Browse the repository at this point in the history
  • Loading branch information
ShaMan123 committed Apr 4, 2022
1 parent 7d793b3 commit 2708029
Show file tree
Hide file tree
Showing 4 changed files with 227 additions and 13 deletions.
8 changes: 4 additions & 4 deletions src/mixins/animation.mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ fabric.util.object.extend(fabric.StaticCanvas.prototype, /** @lends fabric.Stati

return fabric.util.animate({
target: this,
startValue: object.left,
startValue: object.getX(),
endValue: this.getCenterPoint().x,
duration: this.FX_DURATION,
onChange: function(value) {
object.set('left', value);
object.setX(value);
_this.requestRenderAll();
onChange();
},
Expand Down Expand Up @@ -58,11 +58,11 @@ fabric.util.object.extend(fabric.StaticCanvas.prototype, /** @lends fabric.Stati

return fabric.util.animate({
target: this,
startValue: object.top,
startValue: object.getY(),
endValue: this.getCenterPoint().y,
duration: this.FX_DURATION,
onChange: function(value) {
object.set('top', value);
object.setY(value);
_this.requestRenderAll();
onChange();
},
Expand Down
68 changes: 64 additions & 4 deletions src/mixins/object_ancestry.mixin.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,31 @@
fabric.util.object.extend(fabric.Object.prototype, /** @lends fabric.Object.prototype */ {

/**
*
* @param {boolean} [strict] returns only ancestors that are objects (without canvas)
* @returns {(fabric.Object | fabric.StaticCanvas)[]} ancestors from bottom to top
*/
* Checks if object is decendant of target
* Should be used instead of @link {fabric.Collection.contains} for performance reasons
* @param {fabric.Object|fabric.StaticCanvas} target
* @returns {boolean}
*/
isDescendantOf: function (target) {
var parent = this.group || this.canvas;
while (parent) {
if (target === parent) {
return true;
}
else if (parent instanceof fabric.StaticCanvas) {
// happens after all parents were traversed through without a match
return false;
}
parent = parent.group || parent.canvas;
}
return false;
},

/**
*
* @param {boolean} [strict] returns only ancestors that are objects (without canvas)
* @returns {(fabric.Object | fabric.StaticCanvas)[]} ancestors from bottom to top
*/
getAncestors: function (strict) {
var ancestors = [];
var parent = this.group || (!strict ? this.canvas : undefined);
Expand All @@ -13,5 +34,44 @@ fabric.util.object.extend(fabric.Object.prototype, /** @lends fabric.Object.prot
parent = parent.group || (!strict ? parent.canvas : undefined);
}
return ancestors;
},

/**
*
* @param {fabric.Object} other
* @returns {{ index: number, otherIndex: number, ancestors: fabric.Object[] }} ancestors may include the passed objects if one is an ancestor of the other resulting in index of -1
*/
findCommonAncestors: function (other) {
if (this === other) {
return true;
}
else if (!other) {
return false;
}
var ancestors = this.getAncestors();
ancestors.unshift(this);
var otherAncestors = other.getAncestors();
otherAncestors.unshift(other);
for (var i = 0, ancestor; i < ancestors.length; i++) {
ancestor = ancestors[i];
for (var j = 0; j < otherAncestors.length; j++) {
if (ancestor === otherAncestors[j] && !(ancestor instanceof fabric.StaticCanvas)) {
return {
index: i - 1,
otherIndex: j - 1,
ancestors: ancestors.slice(i)
};
}
}
}
},

/**
*
* @param {fabric.Object} other
* @returns {boolean}
*/
hasCommonAncestors: function (other) {
return !!this.findCommonAncestors(other);
}
});
121 changes: 116 additions & 5 deletions src/mixins/object_geometry.mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,107 @@
*/
controls: { },

/**
* @returns {number} x position according to object's {@link fabric.Object#originX} property in canvas coordinate plane
*/
getX: function () {
return this.getXY().x;
},

/**
* @param {number} value x position according to object's {@link fabric.Object#originX} property in canvas coordinate plane
*/
setX: function (value) {
this.setXY(this.getXY().setX(value));
},

/**
* @returns {number} x position according to object's {@link fabric.Object#originX} property in parent's coordinate plane\
* if parent is canvas then this property is identical to {@link fabric.Object#getX}
*/
getRelativeX: function () {
return this.left;
},

/**
* @param {number} value x position according to object's {@link fabric.Object#originX} property in parent's coordinate plane\
* if parent is canvas then this method is identical to {@link fabric.Object#setX}
*/
setRelativeX: function (value) {
this.left = value;
},

/**
* @returns {number} y position according to object's {@link fabric.Object#originY} property in canvas coordinate plane
*/
getY: function () {
return this.getXY().y;
},

/**
* @param {number} value y position according to object's {@link fabric.Object#originY} property in canvas coordinate plane
*/
setY: function (value) {
this.setXY(this.getXY().setY(value));
},

/**
* @returns {number} y position according to object's {@link fabric.Object#originY} property in parent's coordinate plane\
* if parent is canvas then this property is identical to {@link fabric.Object#getY}
*/
getRelativeY: function () {
return this.top;
},

/**
* @param {number} value y position according to object's {@link fabric.Object#originY} property in parent's coordinate plane\
* if parent is canvas then this property is identical to {@link fabric.Object#setY}
*/
setRelativeY: function (value) {
this.top = value;
},

/**
* @returns {number} x position according to object's {@link fabric.Object#originX} {@link fabric.Object#originY} properties in canvas coordinate plane
*/
getXY: function () {
var relativePosition = this.getRelativeXY();
return this.group ?
fabric.util.transformPoint(relativePosition, this.group.calcTransformMatrix()) :
relativePosition;
},

/**
* @param {fabric.Point} point position according to object's {@link fabric.Object#originX} {@link fabric.Object#originY} properties in canvas coordinate plane
* @param {'left'|'center'|'right'|number} [originX] Horizontal origin: 'left', 'center' or 'right'
* @param {'top'|'center'|'bottom'|number} [originY] Vertical origin: 'top', 'center' or 'bottom'
*/
setXY: function (point, originX, originY) {
if (this.group) {
point = fabric.util.transformPoint(
point,
fabric.util.invertTransform(this.group.calcTransformMatrix())
);
}
this.setRelativeXY(point, originX, originY);
},

/**
* @returns {number} x position according to object's {@link fabric.Object#originX} {@link fabric.Object#originY} properties in parent's coordinate plane
*/
getRelativeXY: function () {
return new fabric.Point(this.left, this.top);
},

/**
* @param {fabric.Point} point position according to object's {@link fabric.Object#originX} {@link fabric.Object#originY} properties in parent's coordinate plane
* @param {'left'|'center'|'right'|number} [originX] Horizontal origin: 'left', 'center' or 'right'
* @param {'top'|'center'|'bottom'|number} [originY] Vertical origin: 'top', 'center' or 'bottom'
*/
setRelativeXY: function (point, originX, originY) {
this.setPositionByOrigin(point, originX || this.originX, originY || this.originY);
},

/**
* return correct set of coordinates for intersection
* this will return either aCoords or lineCoords.
Expand All @@ -88,8 +189,15 @@
* The coords are returned in an array.
* @return {Array} [tl, tr, br, bl] of points
*/
getCoords: function(absolute, calculate) {
return arrayFromCoords(this._getCoords(absolute, calculate));
getCoords: function (absolute, calculate) {
var coords = arrayFromCoords(this._getCoords(absolute, calculate));
if (this.group) {
var t = this.group.calcTransformMatrix();
return coords.map(function (p) {
return util.transformPoint(p, t);
});
}
return coords;
},

/**
Expand Down Expand Up @@ -630,9 +738,12 @@
scaleY: this.scaleY,
skewX: this.skewX,
skewY: this.skewY,
width: this.width,
height: this.height,
strokeWidth: this.strokeWidth
}, options || {});
// stroke is applied before/after transformations are applied according to `strokeUniform`
var preScalingStrokeValue, postScalingStrokeValue, strokeWidth = this.strokeWidth;
var preScalingStrokeValue, postScalingStrokeValue, strokeWidth = options.strokeWidth;
if (this.strokeUniform) {
preScalingStrokeValue = 0;
postScalingStrokeValue = strokeWidth;
Expand All @@ -641,8 +752,8 @@
preScalingStrokeValue = strokeWidth;
postScalingStrokeValue = 0;
}
var dimX = this.width + preScalingStrokeValue,
dimY = this.height + preScalingStrokeValue,
var dimX = options.width + preScalingStrokeValue,
dimY = options.height + preScalingStrokeValue,
finalDimensions,
noSkew = options.skewX === 0 && options.skewY === 0;
if (noSkew) {
Expand Down
43 changes: 43 additions & 0 deletions src/mixins/object_stacking.mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,5 +76,48 @@ fabric.util.object.extend(fabric.Object.prototype, /** @lends fabric.Object.prot
this.canvas.moveTo(this, index);
}
return this;
},

/**
*
* @param {fabric.Object} other object to compare against
* @returns {boolean | undefined} if objects do not share a common ancestor or they are strictly equal it is impossible to determine which is in front of the other; in such cases the function returns `undefined`
*/
isInFrontOf: function (other) {
if (this === other) {
return undefined;
}
var ancestors = this.getAncestors().reverse().concat(this);
var otherAncestors = other.getAncestors().reverse().concat(other);
var i, j, found = false;
// find the common ancestor
for (i = 0; i < ancestors.length; i++) {
for (j = 0; j < otherAncestors.length; j++) {
if (ancestors[i] === otherAncestors[j]) {
found = true;
break;
}
}
if (found) {
break;
}
}
if (!found) {
return undefined;
}
// compare trees from the common ancestor down
var tree = ancestors.slice(i),
otherTree = otherAncestors.slice(j),
a, b, parent;
for (i = 1; i < Math.min(tree.length, otherTree.length); i++) {
a = tree[i];
b = otherTree[i];
if (a !== b) {
parent = tree[i - 1];
return parent._objects.indexOf(a) > parent._objects.indexOf(b);
}
}
// happens if a is ancestor of b or vice versa
return tree.length > otherTree.length;
}
});

0 comments on commit 2708029

Please sign in to comment.