mirror of
https://github.com/scratchfoundation/paper.js.git
synced 2025-01-20 22:39:50 -05:00
Move onFrame handler to DocumentView.
This commit is contained in:
parent
f69b0a404a
commit
c1dc823706
2 changed files with 53 additions and 38 deletions
|
@ -132,11 +132,12 @@ var PaperScript = this.PaperScript = new function() {
|
|||
function run(code, scope) {
|
||||
with (scope) { // Safe one indentation by grouping try and with
|
||||
paper = scope;
|
||||
var doc = scope.document;
|
||||
var doc = scope.document,
|
||||
view = doc.activeView,
|
||||
// TODO: Add support for multiple tools
|
||||
var tool = scope.tool =
|
||||
tool = scope.tool =
|
||||
/on(?:Key|Mouse)(?:Up|Down|Move|Drag)/.test(code)
|
||||
&& new Tool(null, scope);
|
||||
&& new Tool(null, scope);
|
||||
// Define variables for potential handlers, so eval() calls below to
|
||||
// fetch their values do not require try-catch around them.
|
||||
var onEditOptions, onSelect, onDeselect, onReselect, onMouseDown,
|
||||
|
@ -151,38 +152,13 @@ var PaperScript = this.PaperScript = new function() {
|
|||
}
|
||||
);
|
||||
}
|
||||
// TODO: Move onFrame support to DocumentView
|
||||
var onFrame = eval('onFrame');
|
||||
if (onFrame) {
|
||||
var lastTime,
|
||||
totalTime = 0,
|
||||
count = 0;
|
||||
function frame(dontSwitch) {
|
||||
if (!dontSwitch)
|
||||
paper = scope;
|
||||
// Request next frame already
|
||||
DomEvent.requestAnimationFrame(frame, doc && doc.canvas);
|
||||
var time = Date.now() / 1000;
|
||||
// Time elapsed since last redraw in seconds:
|
||||
var delta = lastTime ? time - lastTime : 0;
|
||||
// Time since first call of frame() in seconds:
|
||||
totalTime += delta;
|
||||
onFrame({
|
||||
delta: delta,
|
||||
time: totalTime,
|
||||
count: count++
|
||||
});
|
||||
// Automatically redraw document each frame.
|
||||
if (doc)
|
||||
doc.redraw();
|
||||
lastTime = time;
|
||||
};
|
||||
// Call the onFrame handler and redraw the document:
|
||||
frame(true);
|
||||
} else {
|
||||
// Automatically redraw document at the end.
|
||||
if (doc)
|
||||
doc.redraw();
|
||||
if (view) {
|
||||
if (onFrame) {
|
||||
view.setOnFrame(onFrame);
|
||||
} else {
|
||||
// Automatically draw view at the end.
|
||||
view.draw();
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
|
|
@ -165,13 +165,52 @@ var DocumentView = this.DocumentView = Base.extend({
|
|||
return this._getInverse()._transformPoint(Point.read(arguments));
|
||||
},
|
||||
|
||||
setOnFrame: function(onFrame) {
|
||||
this._onFrame = onFrame;
|
||||
var that = this,
|
||||
running = false,
|
||||
lastTime,
|
||||
totalTime = 0,
|
||||
count = 0;
|
||||
function frame(dontSwitch) {
|
||||
if (!that._onFrame) {
|
||||
running = false;
|
||||
return;
|
||||
}
|
||||
if (!dontSwitch)
|
||||
paper = that._document._scope;
|
||||
// Request next frame already
|
||||
DomEvent.requestAnimationFrame(frame, that._canvas);
|
||||
running = true;
|
||||
var time = Date.now() / 1000,
|
||||
delta = lastTime ? time - lastTime : 0;
|
||||
totalTime += delta;
|
||||
that._onFrame({
|
||||
delta: delta, // Time elapsed since last redraw in seconds
|
||||
time: totalTime, // Time since first call of frame() in seconds
|
||||
count: count++
|
||||
});
|
||||
// Automatically draw view on each frame.
|
||||
that.draw();
|
||||
lastTime = time;
|
||||
};
|
||||
// Call the onFrame handler straight away, initializing the sequence
|
||||
// of onFrame calls.
|
||||
if (!running)
|
||||
frame(true);
|
||||
},
|
||||
|
||||
getOnFrame: function() {
|
||||
return this._onFrame;
|
||||
},
|
||||
|
||||
_createEvents: function() {
|
||||
var scope = this._document._scope,
|
||||
var that = this,
|
||||
scope = this._document._scope,
|
||||
tool,
|
||||
timer,
|
||||
curPoint,
|
||||
dragging = false,
|
||||
that = this;
|
||||
dragging = false;
|
||||
|
||||
function viewToArtwork(event) {
|
||||
return that.viewToArtwork(DomEvent.getOffset(event));
|
||||
|
|
Loading…
Reference in a new issue