Skip to content

Commit

Permalink
newScaleX undefined fixed (fabricjs#5464)
Browse files Browse the repository at this point in the history
* newScaleX undefined fixed
  • Loading branch information
asturur authored Jan 7, 2019
1 parent 97b5b66 commit eb04d24
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 21 deletions.
5 changes: 1 addition & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,7 @@ jobs:
- stage: Unit Tests
env: CANVAS=canvas-prebuilt
node_js: "8"
cache:
directories:
- ''
install: npm install && npm remove canvas && npm install canvas-prebuilt@^1.6.11
install: npm install && npm remove canvas && npm install canvas-prebuilt@^1.6.11 [email protected]
addons:
apt:
packages: # avoid installing packages
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
"testem:ci": "testem ci"
},
"optionalDependencies": {
"canvas": "^1.6.12",
"canvas": "^1.6.13",
"jsdom": "^9.12.0",
"xmldom": "0.1.x"
},
Expand Down
26 changes: 12 additions & 14 deletions src/canvas.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -896,12 +896,10 @@
*/
_setObjectScale: function(localMouse, transform, lockScalingX, lockScalingY, by, lockScalingFlip, _dim) {
var target = transform.target, forbidScalingX = false, forbidScalingY = false, scaled = false,
changeX, changeY, scaleX, scaleY;

scaleX = localMouse.x * target.scaleX / _dim.x;
scaleY = localMouse.y * target.scaleY / _dim.y;
changeX = target.scaleX !== scaleX;
changeY = target.scaleY !== scaleY;
scaleX = localMouse.x * target.scaleX / _dim.x,
scaleY = localMouse.y * target.scaleY / _dim.y,
changeX = target.scaleX !== scaleX,
changeY = target.scaleY !== scaleY;

if (lockScalingFlip && scaleX <= 0 && scaleX < target.scaleX) {
forbidScalingX = true;
Expand All @@ -921,10 +919,10 @@
forbidScalingY || lockScalingY || (target.set('scaleY', scaleY) && (scaled = scaled || changeY));
}
else if (by === 'x' && !target.get('lockUniScaling')) {
forbidScalingX || lockScalingX || (target.set('scaleX', scaleX) && (scaled = scaled || changeX));
forbidScalingX || lockScalingX || (target.set('scaleX', scaleX) && (scaled = changeX));
}
else if (by === 'y' && !target.get('lockUniScaling')) {
forbidScalingY || lockScalingY || (target.set('scaleY', scaleY) && (scaled = scaled || changeY));
forbidScalingY || lockScalingY || (target.set('scaleY', scaleY) && (scaled = changeY));
}
transform.newScaleX = scaleX;
transform.newScaleY = scaleY;
Expand All @@ -942,15 +940,15 @@
lastDist = _dim.y * transform.original.scaleY / target.scaleY +
_dim.x * transform.original.scaleX / target.scaleX,
scaled, signX = localMouse.x < 0 ? -1 : 1,
signY = localMouse.y < 0 ? -1 : 1;
signY = localMouse.y < 0 ? -1 : 1, newScaleX, newScaleY;

// We use transform.scaleX/Y instead of target.scaleX/Y
// because the object may have a min scale and we'll loose the proportions
transform.newScaleX = signX * Math.abs(transform.original.scaleX * dist / lastDist);
transform.newScaleY = signY * Math.abs(transform.original.scaleY * dist / lastDist);
scaled = transform.newScaleX !== target.scaleX || transform.newScaleY !== target.scaleY;
target.set('scaleX', transform.newScaleX);
target.set('scaleY', transform.newScaleY);
newScaleX = signX * Math.abs(transform.original.scaleX * dist / lastDist);
newScaleY = signY * Math.abs(transform.original.scaleY * dist / lastDist);
scaled = newScaleX !== target.scaleX || newScaleY !== target.scaleY;
target.set('scaleX', newScaleX);
target.set('scaleY', newScaleY);
return scaled;
},

Expand Down
9 changes: 7 additions & 2 deletions src/mixins/textbox_behavior.mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,18 @@
fabric.Canvas.prototype._setObjectScale = function(localMouse, transform,
lockScalingX, lockScalingY, by, lockScalingFlip, _dim) {

var t = transform.target;
var t = transform.target, scaled,
scaleX = localMouse.x * t.scaleX / _dim.x,
scaleY = localMouse.y * t.scaleY / _dim.y;
if (by === 'x' && t instanceof fabric.Textbox) {
var tw = t._getTransformedDimensions().x;
var w = t.width * (localMouse.x / tw);
transform.newScaleX = scaleX;
transform.newScaleY = scaleY;
if (w >= t.getMinWidth()) {
scaled = w !== t.width;
t.set('width', w);
return true;
return scaled;
}
}
else {
Expand Down
16 changes: 16 additions & 0 deletions test/unit/textbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,4 +154,20 @@
assert.deepEqual(line2, expected2, 'wrapping without reserved');
assert.deepEqual(textbox.dynamicMinWidth, 90, 'wrapping without reserved');
});
QUnit.test('_scaleObject with textbox', function(assert) {
var text = new fabric.Textbox('xa xb xc xd xe ya yb id', { strokeWidth: 0 });
canvas.add(text);
var canvasEl = canvas.getElement(),
canvasOffset = fabric.util.getElementOffset(canvasEl);
var eventStub = {
clientX: canvasOffset.left + text.width,
clientY: canvasOffset.top + text.oCoords.mr.corner.tl.y + 1,
};
var originalWidth = text.width;
canvas._setupCurrentTransform(eventStub, text, true);
var scaled = canvas._scaleObject(eventStub.clientX + 20, eventStub.clientY, 'x');
assert.equal(scaled, true, 'return true if textbox scaled');
assert.equal(text.width, originalWidth + 20, 'width increased');
assert.equal(canvas._currentTransform.newScaleX, text.width / originalWidth, 'newScaleX is not undefined');
});
})();

0 comments on commit eb04d24

Please sign in to comment.