mirror of
https://github.com/scratchfoundation/paper.js.git
synced 2025-01-01 02:38:43 -05:00
Have Tool convert points from view coordinates to artwork coordinates.
This commit is contained in:
parent
74f797a9dd
commit
4fa293ec6d
1 changed files with 77 additions and 69 deletions
146
src/tool/Tool.js
146
src/tool/Tool.js
|
@ -1,74 +1,82 @@
|
|||
Tool = ToolHandler.extend({
|
||||
beans: true,
|
||||
Tool = ToolHandler.extend(new function() {
|
||||
function viewToArtwork(event, document) {
|
||||
var point = Point.read(event.offset.x, event.offset.y);
|
||||
// TODO: always the active view?
|
||||
return document.activeView.viewToArtwork(point);
|
||||
};
|
||||
|
||||
return {
|
||||
beans: true,
|
||||
|
||||
initialize: function(handlers, doc) {
|
||||
this.base(handlers);
|
||||
if (Paper.document)
|
||||
this.document = Paper.document;
|
||||
},
|
||||
|
||||
setDocument: function(doc) {
|
||||
if (this._document)
|
||||
$(this._document.canvas).removeEvents();
|
||||
this._document = doc || Paper.document;
|
||||
var that = this, curPoint;
|
||||
var dragging = false;
|
||||
var events = {
|
||||
dragstart: function(e) {
|
||||
curPoint = new Point(e.offset);
|
||||
that.onHandleEvent('MOUSE_DOWN', curPoint, null, null);
|
||||
if (that.onMouseDown)
|
||||
that._document.redraw();
|
||||
if (that.eventInterval != -1)
|
||||
this.intervalId = setInterval(events.drag, that.eventInterval);
|
||||
dragging = true;
|
||||
},
|
||||
drag: function(e) {
|
||||
if (e) curPoint = new Point(e.offset);
|
||||
if (curPoint) {
|
||||
that.onHandleEvent('MOUSE_DRAG', curPoint, null, null);
|
||||
if (that.onMouseDrag)
|
||||
initialize: function(handlers, doc) {
|
||||
this.base(handlers);
|
||||
if (Paper.document)
|
||||
this.document = Paper.document;
|
||||
},
|
||||
|
||||
setDocument: function(doc) {
|
||||
if (this._document)
|
||||
$(this._document.canvas).removeEvents();
|
||||
this._document = doc || Paper.document;
|
||||
var that = this, curPoint;
|
||||
var dragging = false;
|
||||
var events = {
|
||||
dragstart: function(e) {
|
||||
curPoint = viewToArtwork(e);//new Point(e.offset);
|
||||
that.onHandleEvent('MOUSE_DOWN', curPoint, null, null);
|
||||
if (that.onMouseDown)
|
||||
that._document.redraw();
|
||||
}
|
||||
},
|
||||
dragend: function(e) {
|
||||
curPoint = null;
|
||||
if (this.eventInterval != -1)
|
||||
clearInterval(this.intervalId);
|
||||
that.onHandleEvent('MOUSE_UP', new Point(e.offset), null, null);
|
||||
if (that.onMouseUp)
|
||||
that._document.redraw();
|
||||
dragging = false;
|
||||
},
|
||||
mousemove: function(e) {
|
||||
if(!dragging) {
|
||||
that.onHandleEvent('MOUSE_MOVE', new Point(e.offset), null, null);
|
||||
if (that.onMouseMove)
|
||||
if (that.eventInterval != -1)
|
||||
this.intervalId = setInterval(events.drag, that.eventInterval);
|
||||
dragging = true;
|
||||
},
|
||||
drag: function(e) {
|
||||
if (e) curPoint = viewToArtwork(e);//new Point(e.offset);
|
||||
if (curPoint) {
|
||||
that.onHandleEvent('MOUSE_DRAG', curPoint, null, null);
|
||||
if (that.onMouseDrag)
|
||||
that._document.redraw();
|
||||
}
|
||||
},
|
||||
dragend: function(e) {
|
||||
curPoint = null;
|
||||
if (this.eventInterval != -1)
|
||||
clearInterval(this.intervalId);
|
||||
that.onHandleEvent('MOUSE_UP', viewToArtwork(e), null, null);
|
||||
if (that.onMouseUp)
|
||||
that._document.redraw();
|
||||
dragging = false;
|
||||
},
|
||||
mousemove: function(e) {
|
||||
if (!dragging) {
|
||||
that.onHandleEvent('MOUSE_MOVE', viewToArtwork(e), null, null);
|
||||
if (that.onMouseMove)
|
||||
that._document.redraw();
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
$(doc.canvas).addEvents(events);
|
||||
},
|
||||
|
||||
/**
|
||||
* The fixed time delay between each call to the {@link #onMouseDrag}
|
||||
* event. Setting this to an interval means the {@link #onMouseDrag} event
|
||||
* is called repeatedly after the initial {@link #onMouseDown} until the
|
||||
* user releases the mouse.
|
||||
*
|
||||
* Sample code:
|
||||
* <code>
|
||||
* // Fire the onMouseDrag event once a second,
|
||||
* // while the mouse button is down
|
||||
* tool.eventInterval = 1000;
|
||||
* </code>
|
||||
*
|
||||
* @return the interval time in milliseconds
|
||||
*/
|
||||
eventInterval: -1,
|
||||
|
||||
getDocument: function() {
|
||||
return this._document;
|
||||
}
|
||||
};
|
||||
$(doc.canvas).addEvents(events);
|
||||
},
|
||||
|
||||
/**
|
||||
* The fixed time delay between each call to the {@link #onMouseDrag}
|
||||
* event. Setting this to an interval means the {@link #onMouseDrag} event
|
||||
* is called repeatedly after the initial {@link #onMouseDown} until the
|
||||
* user releases the mouse.
|
||||
*
|
||||
* Sample code:
|
||||
* <code>
|
||||
* // Fire the onMouseDrag event once a second,
|
||||
* // while the mouse button is down
|
||||
* tool.eventInterval = 1000;
|
||||
* </code>
|
||||
*
|
||||
* @return the interval time in milliseconds
|
||||
*/
|
||||
eventInterval: -1,
|
||||
|
||||
getDocument: function() {
|
||||
return this._document;
|
||||
}
|
||||
};
|
||||
});
|
Loading…
Reference in a new issue