Skip to content

Commit

Permalink
Merge pull request #10 from kangax/master
Browse files Browse the repository at this point in the history
update to master
  • Loading branch information
asturur committed May 9, 2015
2 parents 5411085 + 934fe47 commit b214818
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 36 deletions.
18 changes: 12 additions & 6 deletions src/canvas.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -499,10 +499,13 @@
*/
_setObjectScale: function(localMouse, transform, lockScalingX, lockScalingY, by, lockScalingFlip) {
var target = transform.target, forbidScalingX = false, forbidScalingY = false,
strokeWidth = target.stroke ? target.strokeWidth : 0;
vLine = target.type === 'line' && target.width === 0,
hLine = target.type === 'line' && target.height === 0,
strokeWidthX = hLine ? 0 : target.strokeWidth,
strokeWidthY = vLine ? 0 : target.strokeWidth;

transform.newScaleX = localMouse.x / (target.width + strokeWidth / 2);
transform.newScaleY = localMouse.y / (target.height + strokeWidth / 2);
transform.newScaleX = localMouse.x / (target.width + strokeWidthX);
transform.newScaleY = localMouse.y / (target.height + strokeWidthY);

if (lockScalingFlip && transform.newScaleX <= 0 && transform.newScaleX < target.scaleX) {
forbidScalingX = true;
Expand Down Expand Up @@ -536,9 +539,12 @@
_scaleObjectEqually: function(localMouse, target, transform) {

var dist = localMouse.y + localMouse.x,
strokeWidth = target.stroke ? target.strokeWidth : 0,
lastDist = (target.height + (strokeWidth / 2)) * transform.original.scaleY +
(target.width + (strokeWidth / 2)) * transform.original.scaleX;
vLine = target.type === 'line' && target.width === 0,
hLine = target.type === 'line' && target.height === 0,
strokeWidthX = hLine ? 0 : target.strokeWidth,
strokeWidthY = vLine ? 0 : target.strokeWidth,
lastDist = (target.height + strokeWidthY) * transform.original.scaleY +
(target.width + strokeWidthX) * transform.original.scaleX;

// We use transform.scaleX/Y instead of target.scaleX/Y
// because the object may have a min scale and we'll loose the proportions
Expand Down
9 changes: 6 additions & 3 deletions src/mixins/canvas_gestures.mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,13 @@
target._scaling = true;

var constraintPosition = target.translateToOriginPoint(target.getCenterPoint(), t.originX, t.originY),
halfStrokeWidth = target.stroke ? target.strokeWidth / 2 : 0;
vLine = target.type === 'line' && target.width === 0,
hLine = target.type === 'line' && target.height === 0,
strokeWidthX = hLine ? 0 : target.strokeWidth,
strokeWidthY = vLine ? 0 : target.strokeWidth;

this._setObjectScale(new fabric.Point((t.scaleX * s * (target.width + halfStrokeWidth)),
(t.scaleY * s * (target.height + halfStrokeWidth))),
this._setObjectScale(new fabric.Point((t.scaleX * s * (target.width + strokeWidthX)),
(t.scaleY * s * (target.height + strokeWidthY))),
t, lockScalingX, lockScalingY, null, target.get('lockScalingFlip'));

target.setPositionByOrigin(constraintPosition, t.originX, t.originY);
Expand Down
4 changes: 2 additions & 2 deletions src/mixins/itext.svg_export.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/* _TO_SVG_START_ */
(function() {
var toFixed = fabric.util.toFixed,
NUM_FRACTION_DIGITS = fabric.Object.NUM_FRACTION_DIGITS;

/* _TO_SVG_START_ */
fabric.util.object.extend(fabric.IText.prototype, /** @lends fabric.IText.prototype */ {

/**
Expand Down Expand Up @@ -115,5 +115,5 @@
].join('');
}
});
/* _TO_SVG_END_ */
})();
/* _TO_SVG_END_ */
5 changes: 4 additions & 1 deletion src/shapes/group.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,11 @@
}

ctx.save();
this.clipTo && fabric.util.clipContext(this, ctx);
if (this.transformMatrix) {
ctx.transform.apply(ctx, this.transformMatrix);
}
this.transform(ctx);
this.clipTo && fabric.util.clipContext(this, ctx);
// the array is now sorted in order of highest first, so start from end
for (var i = 0, len = this._objects.length; i < len; i++) {
this._renderObject(this._objects[i], ctx);
Expand Down
1 change: 1 addition & 0 deletions src/shapes/itext.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,7 @@
ctx.save();
ctx.transform.apply(ctx, this.canvas.viewportTransform);
this.transform(ctx);
this.transformMatrix && ctx.transform.apply(ctx, this.transformMatrix);
}
else {
ctx = this.ctx;
Expand Down
22 changes: 5 additions & 17 deletions src/shapes/object.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -979,8 +979,6 @@
this.clipTo && fabric.util.clipContext(this, ctx);
this._render(ctx, noTransform);
this.clipTo && ctx.restore();
this._removeShadow(ctx);
this._restoreCompositeOperation(ctx);

ctx.restore();
},
Expand Down Expand Up @@ -1102,9 +1100,6 @@
ctx.fill();
}
ctx.restore();
if (this.shadow && !this.shadow.affectStroke) {
this._removeShadow(ctx);
}
},

/**
Expand All @@ -1116,7 +1111,12 @@
return;
}

if (!this.shadow.affectStroke) {
this._removeShadow(ctx);
}

ctx.save();

if (this.strokeDashArray) {
// Spec requires the concatenation of two copies the dash list when the number of elements is odd
if (1 & this.strokeDashArray.length) {
Expand All @@ -1138,7 +1138,6 @@
}
this._stroke ? this._stroke(ctx) : ctx.stroke();
}
this._removeShadow(ctx);
ctx.restore();
},

Expand Down Expand Up @@ -1476,19 +1475,8 @@
*/
_setupCompositeOperation: function (ctx) {
if (this.globalCompositeOperation) {
this._prevGlobalCompositeOperation = ctx.globalCompositeOperation;
ctx.globalCompositeOperation = this.globalCompositeOperation;
}
},

/**
* Restores previously saved canvas globalCompositeOperation after obeject rendering
* @param {CanvasRenderingContext2D} ctx Rendering canvas context
*/
_restoreCompositeOperation: function (ctx) {
if (this.globalCompositeOperation && this._prevGlobalCompositeOperation) {
ctx.globalCompositeOperation = this._prevGlobalCompositeOperation;
}
}
});

Expand Down
1 change: 0 additions & 1 deletion src/shapes/path_group.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@
this.paths[i].render(ctx, true);
}
this.clipTo && ctx.restore();
this._removeShadow(ctx);
ctx.restore();
},

Expand Down
9 changes: 4 additions & 5 deletions src/shapes/text.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -374,8 +374,6 @@
this._setupCompositeOperation(ctx);
this._renderTextFill(ctx);
this._renderTextStroke(ctx);
this._restoreCompositeOperation(ctx);
this._removeShadow(ctx);
ctx.restore();
},

Expand Down Expand Up @@ -529,9 +527,6 @@
);
lineHeights += heightOfLine;
}
if (this.shadow && !this.shadow.affectStroke) {
this._removeShadow(ctx);
}
},

/**
Expand All @@ -545,6 +540,10 @@

var lineHeights = 0;

if (!this.shadow.affectStroke) {
this._removeShadow(ctx);
}

ctx.save();

if (this.strokeDashArray) {
Expand Down
19 changes: 18 additions & 1 deletion test/unit/group.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
(function() {

var canvas = this.canvas = new fabric.Canvas();
var el = fabric.document.createElement('canvas');
el.width = 600; el.height = 600;

var canvas = this.canvas = fabric.isLikelyNode ? fabric.createCanvasForNode() : new fabric.Canvas(el);

// function _createImageElement() {
// return fabric.isLikelyNode ? new (require('canvas').Image)() : fabric.document.createElement('img');
Expand Down Expand Up @@ -497,6 +500,20 @@ test('toObject without default values', function() {
equal(rect2.canvas, canvas);
equal(rect1.canvas, canvas);
});

test('test group transformMatrix', function() {
var rect1 = new fabric.Rect({ top: 100, left: 100, width: 10, height: 10, strokeWidth: 0 }),
rect2 = new fabric.Rect({ top: 120, left: 120, width: 10, height: 10, strokeWidth: 0 }),
group = new fabric.Group([ rect1, rect2 ]),
ctx = canvas.contextContainer, isTransparent = fabric.util.isTransparent;
canvas.add(group);
equal(isTransparent(ctx, 80, 80, 0), true);
equal(isTransparent(ctx, 101, 101, 0), false);
group.transformMatrix = [1.2, 0, 0, 1.2, 1, 1];
canvas.renderAll();
equal(isTransparent(ctx, 101, 101, 0), true);
equal(isTransparent(ctx, 131, 131, 0), false);
});
// asyncTest('cloning group with image', function() {
// var rect = new fabric.Rect({ top: 100, left: 100, width: 30, height: 10 }),
// img = new fabric.Image(_createImageElement()),
Expand Down

0 comments on commit b214818

Please sign in to comment.