mirror of
https://github.com/scratchfoundation/scratch-vm.git
synced 2025-06-28 15:20:28 -04:00
Feed in requestAnimationFrame events to VM
Not sure exactly how to use these yet, but it seems helpful to have them in there.
This commit is contained in:
parent
bb68fcab25
commit
660029010d
4 changed files with 26 additions and 17 deletions
|
@ -79,6 +79,13 @@ window.onload = function() {
|
|||
// Run threads
|
||||
vm.start();
|
||||
|
||||
// Inform VM of animation frames.
|
||||
var animate = function() {
|
||||
window.vm.animationFrame();
|
||||
requestAnimationFrame(animate);
|
||||
};
|
||||
requestAnimationFrame(animate);
|
||||
|
||||
// Handlers for green flag and stop all.
|
||||
document.getElementById('greenflag').addEventListener('click', function() {
|
||||
vm.greenFlag();
|
||||
|
|
|
@ -236,32 +236,20 @@ Runtime.prototype.targetForThread = function (thread) {
|
|||
};
|
||||
|
||||
/**
|
||||
* setInterval implementation that works in a WebWorker or not.
|
||||
* @param {?Function} fcn Function to call.
|
||||
* @param {number} interval Interval at which to call it.
|
||||
* @return {number} Value returned by setInterval.
|
||||
* Handle an animation frame from the main thread.
|
||||
*/
|
||||
Runtime.prototype._setInterval = function(fcn, interval) {
|
||||
var setInterval = null;
|
||||
if (typeof window !== 'undefined' && window.setInterval) {
|
||||
setInterval = window.setInterval;
|
||||
} else if (typeof self !== 'undefined' && self.setInterval) {
|
||||
setInterval = self.setInterval;
|
||||
} else {
|
||||
return;
|
||||
Runtime.prototype.animationFrame = function () {
|
||||
if (self.renderer) {
|
||||
self.renderer.draw();
|
||||
}
|
||||
return setInterval(fcn, interval);
|
||||
};
|
||||
|
||||
/**
|
||||
* Set up timers to repeatedly step in a browser
|
||||
*/
|
||||
Runtime.prototype.start = function () {
|
||||
this._setInterval(function() {
|
||||
self.setInterval(function() {
|
||||
this._step();
|
||||
if (self.renderer) {
|
||||
self.renderer.draw();
|
||||
}
|
||||
}.bind(this), Runtime.THREAD_STEP_INTERVAL);
|
||||
};
|
||||
|
||||
|
|
10
src/index.js
10
src/index.js
|
@ -91,6 +91,13 @@ VirtualMachine.prototype.getPlaygroundData = function () {
|
|||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* Handle an animation frame.
|
||||
*/
|
||||
VirtualMachine.prototype.animationFrame = function () {
|
||||
this.runtime.animationFrame();
|
||||
};
|
||||
|
||||
/*
|
||||
* Worker handlers: for all public methods available above,
|
||||
* we must also provide a message handler in case the VM is run
|
||||
|
@ -127,6 +134,9 @@ if (ENV_WORKER) {
|
|||
threads: self.vmInstance.runtime.threads
|
||||
});
|
||||
break;
|
||||
case 'animationFrame':
|
||||
self.vmInstance.animationFrame();
|
||||
break;
|
||||
default:
|
||||
if (e.data.id == 'RendererConnected') {
|
||||
//initRenderWorker();
|
||||
|
|
|
@ -63,6 +63,10 @@ VirtualMachine.prototype.stopAll = function () {
|
|||
this.vmWorker.postMessage({method: 'stopAll'});
|
||||
};
|
||||
|
||||
VirtualMachine.prototype.animationFrame = function () {
|
||||
this.vmWorker.postMessage({method: 'animationFrame'});
|
||||
};
|
||||
|
||||
/**
|
||||
* Export and bind to `window`
|
||||
*/
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue