mirror of
https://github.com/scratchfoundation/paper.js.git
synced 2025-01-01 10:48:38 -05:00
Fix #981: Make sure event.delta is always calculated correctly.
The first mousemove / mousedrag events wrongly received the delta from the last mouseup event.
This commit is contained in:
parent
623ec73c7e
commit
b71e3a44d9
4 changed files with 31 additions and 40 deletions
|
@ -25,7 +25,7 @@
|
||||||
function onMouseDrag(event) {
|
function onMouseDrag(event) {
|
||||||
// If this is the first drag event,
|
// If this is the first drag event,
|
||||||
// add the strokes at the start:
|
// add the strokes at the start:
|
||||||
if (event.count == 1) {
|
if (event.count == 0) {
|
||||||
addStrokes(event.middlePoint, event.delta * -1);
|
addStrokes(event.middlePoint, event.delta * -1);
|
||||||
} else {
|
} else {
|
||||||
var step = event.delta / 2;
|
var step = event.delta / 2;
|
||||||
|
|
|
@ -53,9 +53,9 @@ var Tool = PaperScopeItem.extend(/** @lends Tool# */{
|
||||||
// DOCS: rewrite Tool constructor explanation
|
// DOCS: rewrite Tool constructor explanation
|
||||||
initialize: function Tool(props) {
|
initialize: function Tool(props) {
|
||||||
PaperScopeItem.call(this);
|
PaperScopeItem.call(this);
|
||||||
this._firstMove = true;
|
// -1 so first event is 0:
|
||||||
this._count = 0;
|
this._moveCount = -1;
|
||||||
this._downCount = -1; // So first is 0
|
this._downCount = -1;
|
||||||
this._set(props);
|
this._set(props);
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@ -288,11 +288,10 @@ var Tool = PaperScopeItem.extend(/** @lends Tool# */{
|
||||||
paper = this._scope;
|
paper = this._scope;
|
||||||
// If there is no mousedrag event installed, fall back to mousemove,
|
// If there is no mousedrag event installed, fall back to mousemove,
|
||||||
// with which we share the actual event handling code anyhow.
|
// with which we share the actual event handling code anyhow.
|
||||||
var move = mouse.move || mouse.drag && !this.responds(type);
|
if (mouse.drag && !this.responds(type))
|
||||||
// Make sure type is not 'mousedrag' if we fell back.
|
|
||||||
if (move)
|
|
||||||
type = 'mousemove';
|
type = 'mousemove';
|
||||||
var responds = this.responds(type),
|
var move = mouse.move || mouse.drag,
|
||||||
|
responds = this.responds(type),
|
||||||
minDistance = this.minDistance,
|
minDistance = this.minDistance,
|
||||||
maxDistance = this.maxDistance,
|
maxDistance = this.maxDistance,
|
||||||
called = false,
|
called = false,
|
||||||
|
@ -302,14 +301,17 @@ var Tool = PaperScopeItem.extend(/** @lends Tool# */{
|
||||||
// to respect their settings, if necessary.
|
// to respect their settings, if necessary.
|
||||||
// Returns true as long as events should be fired, false when the target
|
// Returns true as long as events should be fired, false when the target
|
||||||
// is reached.
|
// is reached.
|
||||||
function update(start, minDistance, maxDistance) {
|
function update(minDistance, maxDistance) {
|
||||||
var toolPoint = tool._point,
|
var pt = point,
|
||||||
pt = point;
|
// Set toolPoint to the previous point for moves or downPoint for
|
||||||
if (start) {
|
// clicks, so mouseup has a delta spanning over the full drag.
|
||||||
tool._count = 0;
|
// Use the current point if this is the first mousedown, so
|
||||||
} else {
|
// there's always a delta.
|
||||||
if (pt.equals(toolPoint))
|
toolPoint = move ? tool._point : tool._downPoint || pt;
|
||||||
|
if (move) {
|
||||||
|
if (tool._moveCount && pt.equals(toolPoint)) {
|
||||||
return false;
|
return false;
|
||||||
|
}
|
||||||
if (minDistance != null || maxDistance != null) {
|
if (minDistance != null || maxDistance != null) {
|
||||||
var vector = pt.subtract(toolPoint),
|
var vector = pt.subtract(toolPoint),
|
||||||
distance = vector.getLength();
|
distance = vector.getLength();
|
||||||
|
@ -322,44 +324,34 @@ var Tool = PaperScopeItem.extend(/** @lends Tool# */{
|
||||||
Math.min(distance, maxDistance)));
|
Math.min(distance, maxDistance)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
tool._count++;
|
tool._moveCount++;
|
||||||
}
|
}
|
||||||
if (responds) {
|
|
||||||
tool._point = pt;
|
tool._point = pt;
|
||||||
tool._lastPoint = move || mouse.drag
|
tool._lastPoint = toolPoint;
|
||||||
// Make sure mousemove events have lastPoint set even for
|
|
||||||
// the first move so event.delta is always defined for them.
|
|
||||||
? start && move ? pt : toolPoint
|
|
||||||
// Set lastPoint to previous downPoint, or current point if
|
|
||||||
// this is the first mousedown, so there's always a delta.
|
|
||||||
// This way mouseup has a delta spanning over the full drag.
|
|
||||||
: tool._downPoint || pt;
|
|
||||||
}
|
|
||||||
// Keep track of downPoint regardless of the value of response
|
|
||||||
if (mouse.down) {
|
if (mouse.down) {
|
||||||
|
tool._moveCount = -1;
|
||||||
tool._downPoint = pt;
|
tool._downPoint = pt;
|
||||||
tool._downCount++;
|
tool._downCount++;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
function emit(firstMove) {
|
function emit() {
|
||||||
if (responds) {
|
if (responds) {
|
||||||
called = tool.emit(type, new ToolEvent(tool, type, event))
|
called = tool.emit(type, new ToolEvent(tool, type, event))
|
||||||
|| called;
|
|| called;
|
||||||
tool._firstMove = firstMove;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (mouse.down) {
|
if (mouse.down) {
|
||||||
update(responds);
|
update();
|
||||||
emit(false);
|
emit();
|
||||||
} else if (mouse.up) {
|
} else if (mouse.up) {
|
||||||
update(false, null, maxDistance);
|
update(null, maxDistance);
|
||||||
emit(true);
|
emit();
|
||||||
} else if (responds) {
|
} else if (responds) {
|
||||||
while (update(this._firstMove, minDistance, maxDistance))
|
while (update(minDistance, maxDistance))
|
||||||
emit(false);
|
emit();
|
||||||
}
|
}
|
||||||
return called;
|
return called;
|
||||||
}
|
}
|
||||||
|
|
|
@ -158,9 +158,8 @@ var ToolEvent = Event.extend(/** @lends ToolEvent# */{
|
||||||
getCount: function() {
|
getCount: function() {
|
||||||
// Return downCount for both mouse down and up, since
|
// Return downCount for both mouse down and up, since
|
||||||
// the count is the same.
|
// the count is the same.
|
||||||
return /^mouse(down|up)$/.test(this.type)
|
return this.tool[/^mouse(down|up)$/.test(this.type)
|
||||||
? this.tool._downCount
|
? '_downCount' : '_moveCount'];
|
||||||
: this.tool._count;
|
|
||||||
},
|
},
|
||||||
|
|
||||||
setCount: function(count) {
|
setCount: function(count) {
|
||||||
|
|
|
@ -1349,7 +1349,7 @@ new function() { // Injection scope for event handling on the browser
|
||||||
// Only start dragging if the mousedown event has not
|
// Only start dragging if the mousedown event has not
|
||||||
// prevented the default.
|
// prevented the default.
|
||||||
dragItem = !prevented && item;
|
dragItem = !prevented && item;
|
||||||
downPoint = lastPoint = point;
|
downPoint = point;
|
||||||
} else if (mouse.up) {
|
} else if (mouse.up) {
|
||||||
// Emulate click / doubleclick, but only on item, not view
|
// Emulate click / doubleclick, but only on item, not view
|
||||||
if (!prevented && item === downItem) {
|
if (!prevented && item === downItem) {
|
||||||
|
|
Loading…
Reference in a new issue