Add support for clipping objects. Closes #64

This commit is contained in:
kangax 2013-03-10 21:06:17 +01:00
parent 012c333bbb
commit 85fd7ee852
8 changed files with 35 additions and 15 deletions

View file

@ -245,6 +245,8 @@
var groupScaleFactor = Math.max(this.scaleX, this.scaleY);
this.clipTo && fabric.util.clipContext(this, ctx);
//The array is now sorted in order of highest first, so start from end.
for (var i = this.objects.length; i > 0; i--) {
@ -260,6 +262,7 @@
object.borderScaleFactor = originalScaleFactor;
object.hasRotatingPoint = originalHasRotatingPoint;
}
this.clipTo && ctx.restore();
if (!noTransform && this.active) {
this.drawBorders(ctx);

View file

@ -104,7 +104,9 @@
}
this._setShadow(ctx);
this.clipTo && fabric.util.clipContext(this, ctx);
this._render(ctx);
this.clipTo && ctx.restore();
this._removeShadow(ctx);
if (this.active && !noTransform) {

View file

@ -289,6 +289,13 @@
*/
includeDefaultValues: true,
/**
* Function that determines clipping of an object (context is passed as a first argument)
* @property
* @type Function
*/
clipTo: null,
/**
* List of properties to consider when checking if state of an object is changed (fabric.Object#hasStateChanged);
* as well as for history (undo/redo) purposes
@ -667,7 +674,9 @@
}
this._setShadow(ctx);
this.clipTo && fabric.util.clipContext(this, ctx);
this._render(ctx, noTransform);
this.clipTo && ctx.restore();
this._removeShadow(ctx);
if (this.active && !noTransform) {

View file

@ -553,14 +553,16 @@
? this.stroke.toLive(ctx)
: this.stroke;
}
ctx.beginPath();
this._setShadow(ctx);
this.clipTo && fabric.util.clipContext(this, ctx);
ctx.beginPath();
this._render(ctx);
if (this.fill) {
ctx.fill();
}
this.clipTo && ctx.restore();
this._removeShadow(ctx);
if (this.stroke) {

View file

@ -79,9 +79,11 @@
this.transform(ctx);
this._setShadow(ctx);
this.clipTo && fabric.util.clipContext(this, ctx);
for (var i = 0, l = this.paths.length; i < l; ++i) {
this.paths[i].render(ctx, true);
}
this.clipTo && ctx.restore();
this._removeShadow(ctx);
if (this.active) {

View file

@ -563,7 +563,7 @@
this.fire('before:render');
if (this.clipTo) {
this._clipCanvas(canvasToDrawOn);
fabric.util.clipCanvas(this, canvasToDrawOn);
}
if (this.backgroundColor) {
@ -616,17 +616,6 @@
return this;
},
/**
* @private
* @method _clipCanvas
*/
_clipCanvas: function(canvasToDrawOn) {
canvasToDrawOn.save();
canvasToDrawOn.beginPath();
this.clipTo(canvasToDrawOn);
canvasToDrawOn.clip();
},
/**
* @private
* @method _drawBackroundImage

View file

@ -287,10 +287,12 @@
}
this._setTextShadow(ctx);
this.clipTo && fabric.util.clipContext(this, ctx);
this._renderTextFill(ctx, textLines);
this._renderTextStroke(ctx, textLines);
this.clipTo && ctx.restore();
this.textShadow && ctx.restore();
this._renderTextStroke(ctx, textLines);
if (this.textAlign !== 'left' && this.textAlign !== 'justify') {
ctx.restore();
}

View file

@ -386,6 +386,16 @@
}
}
/**
* @method clipContext
*/
function clipContext(receiver, ctx) {
ctx.save();
ctx.beginPath();
receiver.clipTo(ctx);
ctx.clip();
}
fabric.util.removeFromArray = removeFromArray;
fabric.util.degreesToRadians = degreesToRadians;
fabric.util.radiansToDegrees = radiansToDegrees;
@ -402,5 +412,6 @@
fabric.util.drawDashedLine = drawDashedLine;
fabric.util.createCanvasElement = createCanvasElement;
fabric.util.createAccessors = createAccessors;
fabric.util.clipContext = clipContext;
})();