GenericRenderWindow

Introduction

vtkGenericRenderWindow provides a skeleton for implementing a render window
using one’s own OpenGL context and drawable.

Methods

delete

Release GL context

extend

Method used to decorate a given object (publicAPI+model) with vtkGenericRenderWindow characteristics.

Argument Type Required Description
publicAPI Yes object on which methods will be bounds (public)
model Yes object on which data structure will be bounds (protected)
initialValues IGenericRenderWindowInitialValues No (default: {})

getContainer

Get container element

getInteractor

Get interactor object

getOpenGLRenderWindow

getRenderWindow

getRenderer

newInstance

Method used to create a new instance of vtkGenericRenderWindow

Argument Type Required Description
initialValues IGenericRenderWindowInitialValues No for pre-setting some of its content

onResize

Method to register callback when the object is resize().

Argument Type Required Description
callback Yes function

Returns

Type Description
subscription object so you can easily unsubscribe later on

resize

Handle window resize

setBackground

Set background color

Argument Type Required Description
background RGBColor Yes The background color.

setContainer

Set thecontainer element

Argument Type Required Description
el HTMLElement Yes The container element.

Source

index.d.ts
import { vtkObject, vtkSubscription } from "../../../interfaces";
import { RGBColor } from "../../../types";
import vtkRenderer from "../../Core/Renderer";
import vtkRenderWindow from "../../Core/RenderWindow";
import vtkRenderWindowInteractor from "../../Core/RenderWindowInteractor";
import vtkOpenGLRenderWindow from "../../OpenGL/RenderWindow";


/**
*
*/
export interface IGenericRenderWindowInitialValues {
background?: RGBColor;
listenWindowResize?: boolean;
container?: HTMLElement,
}

export interface vtkGenericRenderWindow extends vtkObject {

/**
* Release GL context
*/
delete(): void;

/**
* Get container element
*/
getContainer(): HTMLElement;

/**
* Get interactor object
*/
getInteractor(): vtkRenderWindowInteractor;

/**
*
*/
getOpenGLRenderWindow(): vtkOpenGLRenderWindow;

/**
*
*/
getRenderWindow(): vtkRenderWindow;

/**
*
*/
getRenderer(): vtkRenderer;

/**
* Method to register callback when the object is resize().
*
* @param callback function
* @returns subscription object so you can easily unsubscribe later on
*/
onResize(callback: (instance: vtkObject) => any): vtkSubscription;

/**
* Handle window resize
*/
resize(): void;

/**
* Set background color
* @param {RGBColor} background The background color.
*/
setBackground(background: RGBColor): boolean;

/**
* Set thecontainer element
* @param {HTMLElement} el The container element.
*/
setContainer(el: HTMLElement): void;
}

/**
* Method used to decorate a given object (publicAPI+model) with vtkGenericRenderWindow characteristics.
*
* @param publicAPI object on which methods will be bounds (public)
* @param model object on which data structure will be bounds (protected)
* @param {IGenericRenderWindowInitialValues} [initialValues] (default: {})
*/
export function extend(publicAPI: object, model: object, initialValues?: IGenericRenderWindowInitialValues): void;

/**
* Method used to create a new instance of vtkGenericRenderWindow
* @param {IGenericRenderWindowInitialValues} [initialValues] for pre-setting some of its content
*/
export function newInstance(initialValues?: IGenericRenderWindowInitialValues): vtkGenericRenderWindow;

/**
* vtkGenericRenderWindow provides a skeleton for implementing a render window
* using one's own OpenGL context and drawable.
*/
export declare const vtkGenericRenderWindow: {
newInstance: typeof newInstance;
extend: typeof extend;
}
export default vtkGenericRenderWindow;
index.js
import macro from 'vtk.js/Sources/macros';
import vtkOpenGLRenderWindow from 'vtk.js/Sources/Rendering/OpenGL/RenderWindow';
import vtkRenderer from 'vtk.js/Sources/Rendering/Core/Renderer';
import vtkRenderWindow from 'vtk.js/Sources/Rendering/Core/RenderWindow';
import vtkRenderWindowInteractor from 'vtk.js/Sources/Rendering/Core/RenderWindowInteractor';
import vtkInteractorStyleTrackballCamera from 'vtk.js/Sources/Interaction/Style/InteractorStyleTrackballCamera';

// Load basic classes for vtk() factory
import 'vtk.js/Sources/Common/Core/Points';
import 'vtk.js/Sources/Common/Core/DataArray';
import 'vtk.js/Sources/Common/DataModel/PolyData';
import 'vtk.js/Sources/Rendering/Core/Actor';
import 'vtk.js/Sources/Rendering/Core/Mapper';

function vtkGenericRenderWindow(publicAPI, model) {
// Capture resize trigger method to remove from publicAPI
const invokeResize = publicAPI.invokeResize;
delete publicAPI.invokeResize;

// VTK renderWindow/renderer
model.renderWindow = vtkRenderWindow.newInstance();
model.renderer = vtkRenderer.newInstance();
model.renderWindow.addRenderer(model.renderer);

// OpenGLRenderWindow
model.openGLRenderWindow = vtkOpenGLRenderWindow.newInstance();
model.renderWindow.addView(model.openGLRenderWindow);

// Interactor
model.interactor = vtkRenderWindowInteractor.newInstance();
model.interactor.setInteractorStyle(
vtkInteractorStyleTrackballCamera.newInstance()
);
model.interactor.setView(model.openGLRenderWindow);
model.interactor.initialize();

// Expose background
publicAPI.setBackground = model.renderer.setBackground;

// Update BG color
publicAPI.setBackground(...model.background);

// Handle window resize
publicAPI.resize = () => {
if (model.container) {
const dims = model.container.getBoundingClientRect();
const devicePixelRatio = window.devicePixelRatio || 1;
model.openGLRenderWindow.setSize(
Math.floor(dims.width * devicePixelRatio),
Math.floor(dims.height * devicePixelRatio)
);
invokeResize();
model.renderWindow.render();
}
};

// Handle DOM container relocation
publicAPI.setContainer = (el) => {
if (model.container) {
model.interactor.unbindEvents(model.container);
}

// Switch container
model.container = el;
model.openGLRenderWindow.setContainer(model.container);

// Bind to new container
if (model.container) {
model.interactor.bindEvents(model.container);
}
};

// Properly release GL context
publicAPI.delete = macro.chain(
publicAPI.setContainer,
model.openGLRenderWindow.delete,
publicAPI.delete
);

// Handle size
if (model.listenWindowResize) {
window.addEventListener('resize', publicAPI.resize);
}
publicAPI.resize();
}

// ----------------------------------------------------------------------------
// Object factory
// ----------------------------------------------------------------------------

const DEFAULT_VALUES = {
background: [0.32, 0.34, 0.43],
listenWindowResize: true,
container: null,
};

// ----------------------------------------------------------------------------

export function extend(publicAPI, model, initialValues = {}) {
Object.assign(model, DEFAULT_VALUES, initialValues);

// Object methods
macro.obj(publicAPI, model);
macro.get(publicAPI, model, [
'renderWindow',
'renderer',
'openGLRenderWindow',
'interactor',
'container',
]);
macro.event(publicAPI, model, 'resize');

// Object specific methods
vtkGenericRenderWindow(publicAPI, model);
}

// ----------------------------------------------------------------------------

export const newInstance = macro.newInstance(extend);

// ----------------------------------------------------------------------------

export default { newInstance, extend };