monkeydo/Monkeydo.mjs
Victor Westerlund 46f3a07c85
Replace setTimeout with requestAnimationFrame (#3)
* dev21w45-a

* dev21w45-b

* Implement requestAnimationFrame

This commit replaces setTimeout with a working example of requestAnimationFrame. Some further testing will have to be done

* Add support for loop
2021-12-26 14:07:37 +01:00

43 lines
No EOL
1.2 KiB
JavaScript

import { default as MonkeyMaster } from "./monkey/MonkeyMaster.mjs";
export default class Monkeydo extends MonkeyMaster {
constructor(methods) {
if(typeof methods !== "object") {
throw new TypeError(`Expected type 'object' but got '${typeof methods}' when initializing Monkeydo`);
}
super();
this.methods = {};
Object.assign(this.methods,methods);
}
// Execute a task
do(task) {
if(!task[1] in this.methods) return;
const args = task.splice(2);
this.methods[task[1]]?.(...args);
}
// Loop playback X times or negative number for infinite
async loop(times = 255) {
if(typeof times !== "number") {
times = parseInt(times);
}
times = Math.floor(times);
times = Math.min(Math.max(times,0),255); // Clamp number to 8 bits
return await this.setFlag("loop",times);
}
// Load Monkeydo manifest
async load(manifest) {
if(typeof manifest === "object") {
manifest = JSON.stringify(manifest);
}
return await this.loadManifest(manifest);
}
async play(manifest = null) {
if(!this.ready && !manifest) throw new Error("Can not start playback without a manifest");
if(manifest) await this.load(manifest);
return await this.start();
}
}