import macro from 'vtk.js/Sources/macros';
const { vtkErrorMacro } = macro;
function vtkCanvasView(publicAPI, model) { model.classHierarchy.push('vtkCanvasView');
function updateWindow() { if (model._renderable) { model.canvas.setAttribute('width', model.size[0]); model.canvas.setAttribute('height', model.size[1]); }
if (model.viewStream) { model.viewStream.setSize(model.size[0], model.size[1]); }
model.canvas.style.display = model.useOffScreen ? 'none' : 'block';
if (model.el) { model.el.style.cursor = model.cursorVisibility ? model.cursor : 'none'; } } publicAPI.onModified(updateWindow);
publicAPI.setContainer = (el) => { if (model.el && model.el !== el) { if (model.canvas.parentNode !== model.el) { vtkErrorMacro('Error: canvas parent node does not match container'); }
model.el.removeChild(model.canvas);
if (model.el.contains(model.bgImage)) { model.el.removeChild(model.bgImage); } }
if (model.el !== el) { model.el = el; if (model.el) { model.el.appendChild(model.canvas); }
if (model.useBackgroundImage) { model.el.appendChild(model.bgImage); }
publicAPI.modified(); } };
publicAPI.setBackgroundImage = (img) => { model.bgImage.src = img.src; };
publicAPI.setUseBackgroundImage = (value) => { model.useBackgroundImage = value;
if ( model.useBackgroundImage && model.el && !model.el.contains(model.bgImage) ) { model.el.appendChild(model.bgImage); } else if ( !model.useBackgroundImage && model.el && model.el.contains(model.bgImage) ) { model.el.removeChild(model.bgImage); } };
publicAPI.setViewStream = (stream) => { if (model.viewStream === stream) { return false; } if (model.subscription) { model.subscription.unsubscribe(); model.subscription = null; } model.viewStream = stream; if (model.viewStream) { publicAPI.setUseBackgroundImage(true);
model.subscription = model.viewStream.onImageReady((e) => publicAPI.setBackgroundImage(e.image) ); model.viewStream.setSize(model.size[0], model.size[1]); model.viewStream.invalidateCache(); model.viewStream.render();
publicAPI.modified(); } return true; };
publicAPI.delete = macro.chain(publicAPI.setViewStream, publicAPI.delete);
model._renderable = publicAPI; model._renderers = [publicAPI]; publicAPI.traverseAllPasses = () => {}; publicAPI.isInViewport = () => true; publicAPI.getInteractive = () => true; }
const DEFAULT_VALUES = { canvas: null, size: [300, 300], cursorVisibility: true, cursor: 'pointer', useOffScreen: false, useBackgroundImage: false, };
export function extend(publicAPI, model, initialValues = {}) { Object.assign(model, DEFAULT_VALUES, initialValues);
model.canvas = document.createElement('canvas'); model.canvas.style.width = '100%';
model.bgImage = new Image(); model.bgImage.style.position = 'absolute'; model.bgImage.style.left = '0'; model.bgImage.style.top = '0'; model.bgImage.style.width = '100%'; model.bgImage.style.height = '100%'; model.bgImage.style.zIndex = '-1';
macro.obj(publicAPI, model, initialValues);
macro.get(publicAPI, model, ['useBackgroundImage', '_renderable']);
macro.setGet(publicAPI, model, [ 'canvas', 'cursor', 'useOffScreen', 'interactor', ]);
macro.setGetArray(publicAPI, model, ['size'], 2); macro.getArray(publicAPI, model, ['_renderers']); macro.moveToProtected(publicAPI, model, ['renderable', 'renderers']);
vtkCanvasView(publicAPI, model); }
export const newInstance = macro.newInstance(extend, 'vtkCanvasView');
export default { newInstance, extend };
|