Skip to content

Commit

Permalink
fixed selection, added test (#3254)
Browse files Browse the repository at this point in the history
  • Loading branch information
asturur authored Sep 11, 2016
1 parent aa53e08 commit c34027c
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 9 deletions.
31 changes: 26 additions & 5 deletions src/canvas.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,23 @@

/**
* Indicates which key enable multiple click selection
* values: altKey, shiftKey, ctrlKey
* values: altKey, shiftKey, ctrlKey, cmdKey
* @since 1.6.2
* @type String
* @default
*/
selectionKey: 'shiftKey',

/**
* Indicates which key enable alternative selection
* in case of target overlapping with active object
* values: altKey, shiftKey, ctrlKey, cmdKey
* @since 1.6.5
* @type null|String
* @default
*/
altSelectionKey: null,

/**
* Color of selection
* @type String
Expand Down Expand Up @@ -398,7 +408,6 @@
vptPointer = this.restorePointerVpt(pointer),
p = fabric.util.transformPoint(vptPointer, invertedM);
return fabric.util.transformPoint(p, vpt);
//return { x: p.x * vpt[0], y: p.y * vpt[3] };
},

/**
Expand Down Expand Up @@ -1006,22 +1015,34 @@
var ignoreZoom = true,
pointer = this.getPointer(e, ignoreZoom),
activeGroup = this.getActiveGroup(),
activeObject = this.getActiveObject();
activeObject = this.getActiveObject(),
activeTarget;

// first check current group (if one exists)
// active group does not check sub targets like normal groups.
// if active group just exits.
if (activeGroup && !skipGroup && this._checkTarget(pointer, activeGroup)) {
return activeGroup;
}

if (activeObject && this._checkTarget(pointer, activeObject)) {
// if we hit the corner of an activeObject, let's return that.
if (activeObject && activeObject._findTargetCorner(pointer)) {
return activeObject;
}
if (activeObject && this._checkTarget(pointer, activeObject)) {
if (!this.preserveObjectStacking) {
return activeObject;
}
else {
activeTarget = activeObject;
}
}

this.targets = [];

var target = this._searchPossibleTargets(this._objects, pointer);
if (e[this.altSelectionKey] && target && activeTarget && target !== activeTarget) {
target = activeTarget;
}
this._fireOverOutEvents(target, e);
return target;
},
Expand Down
4 changes: 4 additions & 0 deletions src/mixins/object_geometry.mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,10 @@
return this;
},

/*
* calculate rotation matrix of an object
* @return {Array} rotation matrix for the object
*/
_calcRotateMatrix: function() {
if (this.angle) {
var theta = degreesToRadians(this.angle), cos = Math.cos(theta), sin = Math.sin(theta);
Expand Down
14 changes: 10 additions & 4 deletions test/unit/canvas.js
Original file line number Diff line number Diff line change
Expand Up @@ -346,17 +346,23 @@
test('findTarget preserveObjectStacking true', function() {
ok(typeof canvas.findTarget == 'function');
canvas.preserveObjectStacking = true;
var rect = makeRect({ left: 0, top: 0 }),
rectOver = makeRect({ left: 0, top: 0 }),
var rect = makeRect({ left: 0, top: 0, width: 30, height: 30 }),
rectOver = makeRect({ left: 0, top: 0, width: 30, height: 30 }),
target,
pointer = { clientX: 5, clientY: 5 };
pointer = { clientX: 15, clientY: 15, 'shiftKey': true },
pointer2 = { clientX: 4, clientY: 4 };
canvas.add(rect);
canvas.add(rectOver);
target = canvas.findTarget(pointer);
equal(target, rectOver, 'Should return the rectOver, rect is not considered');
canvas.setActiveObject(rect);
target = canvas.findTarget(pointer);
equal(target, rect, 'Should return the rect, because it is active');
equal(target, rectOver, 'Should still return rectOver because is above active object');
target = canvas.findTarget(pointer2);
equal(target, rect, 'Should rect because a corner of the activeObject has been hit');
canvas.altSelectionKey = 'shiftKey';
target = canvas.findTarget(pointer);
equal(target, rect, 'Should rect because active and altSelectionKey is pressed');
canvas.preserveObjectStacking = false;
});

Expand Down

0 comments on commit c34027c

Please sign in to comment.