Skip to content

Commit ec121ca

Browse files
committed
Separate requesting and handling of animation frames.
Closes paperjs#176.
1 parent cd40bd2 commit ec121ca

1 file changed

Lines changed: 19 additions & 17 deletions

File tree

src/ui/View.js

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -145,11 +145,11 @@ var View = Base.extend(Callback, /** @lends View# */{
145145
onFrame: {
146146
install: function() {
147147
/*#*/ if (options.browser) {
148-
// Call the onFrame handler straight away and initialize the
148+
// Request a frame handler straight away to initialize the
149149
// sequence of onFrame calls.
150150
if (!this._requested) {
151151
this._animate = true;
152-
this._handleFrame(true);
152+
this._requestFrame();
153153
}
154154
/*#*/ } // options.browser
155155
},
@@ -169,21 +169,23 @@ var View = Base.extend(Callback, /** @lends View# */{
169169
_time: 0,
170170
_count: 0,
171171

172-
_handleFrame: function(request) {
173-
this._requested = false;
174-
// See if we need to stop due to a call to uninstall()
175-
if (!this._animate)
176-
return;
172+
_requestFrame: function() {
173+
var that = this;
174+
DomEvent.requestAnimationFrame(function() {
175+
that._requested = false;
176+
// Do we need to stop due to a call to the frame event's uninstall()
177+
if (!that._animate)
178+
return;
179+
// Request next frame already before handling the current frame
180+
that._requestFrame();
181+
that._handleFrame();
182+
}, this._element);
183+
this._requested = true;
184+
},
185+
186+
_handleFrame: function() {
177187
// Set the global paper object to the current scope
178188
paper = this._scope;
179-
if (request) {
180-
// Request next frame already
181-
this._requested = true;
182-
var that = this;
183-
DomEvent.requestAnimationFrame(function() {
184-
that._handleFrame(true);
185-
}, this._element);
186-
}
187189
var now = Date.now() / 1000,
188190
delta = this._before ? now - this._before : 0;
189191
this._before = now;
@@ -213,11 +215,11 @@ var View = Base.extend(Callback, /** @lends View# */{
213215
time: 0,
214216
count: 0
215217
};
216-
if (++this._frameItemCount == 1)
218+
if (++this._frameItemCount === 1)
217219
this.attach('frame', this._handleFrameItems);
218220
} else {
219221
delete items[item._id];
220-
if (--this._frameItemCount == 0) {
222+
if (--this._frameItemCount === 0) {
221223
// If this is the last one, just stop animating straight away.
222224
this.detach('frame', this._handleFrameItems);
223225
}

0 commit comments

Comments
 (0)