Skip to content

Instantly share code, notes, and snippets.

@tobiu
Created September 6, 2022 21:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tobiu/335351184db3ed9c3ef4d84449f646cf to your computer and use it in GitHub Desktop.
Save tobiu/335351184db3ed9c3ef4d84449f646cf to your computer and use it in GitHub Desktop.
Neo.component.Canvas
import Component from './Base.mjs';
/**
* @class Neo.component.Canvas
* @extends Neo.component.Base
*/
class Canvas extends Component {
static getConfig() {return {
/**
* @member {String} className='Neo.component.Canvas'
* @protected
*/
className: 'Neo.component.Canvas',
/**
* @member {String} ntype='canvas'
* @protected
*/
ntype: 'canvas',
/**
* @member {Boolean} offscreen=true
*/
offscreen: true,
/**
* Only applicable if offscreen === true.
* true once the ownership of the canvas node got transferred to worker.Canvas.
* @member {Boolean} offscreenRegistered_=false
*/
offscreenRegistered_: false,
/**
* @member {Object} _vdom={tag: 'canvas'}
*/
_vdom:
{tag: 'canvas'}
}}
/**
* Triggered after the mounted config got changed
* @param {Boolean} value
* @param {Boolean} oldValue
* @protected
*/
afterSetMounted(value, oldValue) {
super.afterSetMounted(value, oldValue);
let me = this,
id = me.getCanvasId(),
offscreen = me.offscreen,
worker = Neo.currentWorker;
if (value && offscreen) {
worker.promiseMessage('main', {
action : 'getOffscreenCanvas',
appName: me.appName,
nodeId : id
}).then(data => {
worker.promiseMessage('canvas', {
action: 'registerCanvas',
node : data.offscreen,
nodeId: id
}, [data.offscreen]).then(() => {
me.offscreenRegistered = true;
});
});
} else if (offscreen) {
me.offscreenRegistered = false;
}
}
/**
* Override this method when using wrappers (e.g. D3)
* @returns {String}
*/
getCanvasId() {
return this.id;
}
}
Neo.applyClassConfig(Canvas);
export default Canvas;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment