From 29ae6b8f0fe195939a606b9a981feb26ef80344b Mon Sep 17 00:00:00 2001 From: kangax Date: Tue, 26 Jul 2011 00:20:57 -0400 Subject: [PATCH] Closes #40. Objects, boundaries of which are fully contained within boundaries of other objects are now sent to back/forward properly (via canvas.sendBackwards/canvas.bringForward). --- dist/all.js | 18 +++++++++++++++--- src/canvas.class.js | 4 ++-- src/object.class.js | 12 ++++++++++++ 3 files changed, 29 insertions(+), 5 deletions(-) diff --git a/dist/all.js b/dist/all.js index 336defdd..f4f879a0 100644 --- a/dist/all.js +++ b/dist/all.js @@ -5563,7 +5563,7 @@ fabric.util.getElementOffset = getElementOffset; if (idx !== 0) { for (var i=idx-1; i>=0; --i) { - if (object.intersectsWithObject(this._objects[i])) { + if (object.intersectsWithObject(this._objects[i]) || object.isContainedWithinObject(this._objects[i])) { nextIntersectingIdx = i; break; } @@ -5590,7 +5590,7 @@ fabric.util.getElementOffset = getElementOffset; if (idx !== objects.length-1) { for (var i = idx + 1, l = this._objects.length; i < l; ++i) { - if (object.intersectsWithObject(objects[i])) { + if (object.intersectsWithObject(objects[i]) || object.isContainedWithinObject(this._objects[i])) { nextIntersectingIdx = i; break; } @@ -7006,6 +7006,7 @@ fabric.util.object.extend(fabric.Canvas.prototype, { } var thisCoords = getCoords(this.oCoords), otherCoords = getCoords(other.oCoords); + var intersection = fabric.Intersection.intersectPolygonPolygon( [thisCoords.tl, thisCoords.tr, thisCoords.br, thisCoords.bl], [otherCoords.tl, otherCoords.tr, otherCoords.br, otherCoords.bl] @@ -7014,6 +7015,16 @@ fabric.util.object.extend(fabric.Canvas.prototype, { return (intersection.status === 'Intersection'); }, + /** + * Returns true if object is fully contained within area of another object + * @method isContainedWithinObject + * @param {Object} other Object to test + * @return {Boolean} + */ + isContainedWithinObject: function(other) { + return this.isContainedWithinRect(other.oCoords.tl, other.oCoords.br); + }, + /** * Returns true if object is fully contained within area formed by 2 points * @method isContainedWithinRect @@ -7027,6 +7038,7 @@ fabric.util.object.extend(fabric.Canvas.prototype, { tr = new fabric.Point(oCoords.tr.x, oCoords.tr.y), bl = new fabric.Point(oCoords.bl.x, oCoords.bl.y), br = new fabric.Point(oCoords.br.x, oCoords.br.y); + return tl.x > selectionTL.x && tr.x < selectionBR.x && tl.y > selectionTL.y @@ -9838,7 +9850,7 @@ fabric.util.object.extend(fabric.Canvas.prototype, { * @property * @type String */ - fontFamily: 'Modernist_One_400', + fontFamily: 'Times_New_Roman', /** * @property diff --git a/src/canvas.class.js b/src/canvas.class.js index a7c6db1e..03edfcb2 100644 --- a/src/canvas.class.js +++ b/src/canvas.class.js @@ -1715,7 +1715,7 @@ // traverse down the stack looking for the nearest intersecting object for (var i=idx-1; i>=0; --i) { - if (object.intersectsWithObject(this._objects[i])) { + if (object.intersectsWithObject(this._objects[i]) || object.isContainedWithinObject(this._objects[i])) { nextIntersectingIdx = i; break; } @@ -1744,7 +1744,7 @@ // traverse up the stack looking for the nearest intersecting object for (var i = idx + 1, l = this._objects.length; i < l; ++i) { - if (object.intersectsWithObject(objects[i])) { + if (object.intersectsWithObject(objects[i]) || object.isContainedWithinObject(this._objects[i])) { nextIntersectingIdx = i; break; } diff --git a/src/object.class.js b/src/object.class.js index 31321617..54590c00 100644 --- a/src/object.class.js +++ b/src/object.class.js @@ -767,6 +767,7 @@ } var thisCoords = getCoords(this.oCoords), otherCoords = getCoords(other.oCoords); + var intersection = fabric.Intersection.intersectPolygonPolygon( [thisCoords.tl, thisCoords.tr, thisCoords.br, thisCoords.bl], [otherCoords.tl, otherCoords.tr, otherCoords.br, otherCoords.bl] @@ -775,6 +776,16 @@ return (intersection.status === 'Intersection'); }, + /** + * Returns true if object is fully contained within area of another object + * @method isContainedWithinObject + * @param {Object} other Object to test + * @return {Boolean} + */ + isContainedWithinObject: function(other) { + return this.isContainedWithinRect(other.oCoords.tl, other.oCoords.br); + }, + /** * Returns true if object is fully contained within area formed by 2 points * @method isContainedWithinRect @@ -788,6 +799,7 @@ tr = new fabric.Point(oCoords.tr.x, oCoords.tr.y), bl = new fabric.Point(oCoords.bl.x, oCoords.bl.y), br = new fabric.Point(oCoords.br.x, oCoords.br.y); + return tl.x > selectionTL.x && tr.x < selectionBR.x && tl.y > selectionTL.y