Spaces:
Build error
Build error
File size: 2,230 Bytes
30c32c8 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
const uid = require('../../util/uid');
class canvasStorage {
/**
* initiats the storage
*/
constructor () {
this.canvases = {};
}
attachRuntime (runtime) {
this.runtime = runtime;
}
/**
* gets a canvas with a given id
* @param {string} id the id of the canvas to get
* @returns {Object} the canvas object with this id
*/
getCanvas (id) {
return this.canvases[id];
}
/**
* deletes a canvas with a given id
* @param {string} id the canvas id to delete
* @returns {Object} the deleted canvas
*/
deleteCanvas (id) {
const orignal = this.canvases[id];
delete this.canvases[id];
return orignal;
}
/**
* creates a new canvas
* @param {string} name the name to give the new canvas
* @param {number} width the width of the canvas
* @param {number} height the height of the canvas
* @param {string} opt_id the id of the canvas
* @returns {Object} the new canvas object
*/
newCanvas (name, width, height, opt_id) {
width = width || this.runtime.stageWidth;
height = height || this.runtime.stageHeight;
const id = opt_id || uid();
const element = document.createElement('canvas');
element.id = id;
element.width = width;
element.height = height;
const skin = !this.runtime.renderer
? null
: this.runtime.renderer.createBitmapSkin(element, 1);
const data = {
name: name,
id: id,
element: element,
skinId: skin,
width: width,
height: height,
context: element.getContext('2d')
};
this.canvases[id] = data;
return data;
}
/**
* gets or creates a canvas with name equal to
* @param {String} name the name of the canvas
*/
getCanvasByName (name) {
return Object.values(this.canvases).find(canvas => canvas.name === name);
}
/**
* gets all canvases
* @returns {Array}
*/
getAllCanvases () {
return Object.values(this.canvases);
}
}
module.exports = canvasStorage;
|