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: {})

getApiSpecificRenderWindow

Get the render back-end specific render window.

getContainer

Get container element

getInteractor

Get interactor object

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 or RGBAColor Yes The background color.

Returns

Type Description
true if the background color actually changed, false otherwise

setContainer

Set thecontainer element

Argument Type Required Description
el HTMLElement Yes The container element.

Source

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

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

export interface vtkGenericRenderWindow extends vtkObject {
/**
* Release GL context
*/
delete(): void;

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

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

/**
* Get the render back-end specific render window.
*/
getApiSpecificRenderWindow(): vtkOpenGLRenderWindow | vtkWebGPURenderWindow;

/**
*
*/
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|RGBAColor} background The background color.
* @returns true if the background color actually changed, false otherwise
*/
setBackground(background: RGBColor | RGBAColor): 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 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';
import vtkURLExtract from 'vtk.js/Sources/Common/Core/URLExtract';

// 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';
import 'vtk.js/Sources/Rendering/Misc/RenderingAPIs';

// Process arguments from URL
const userParams = vtkURLExtract.extractURLParameters();

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._apiSpecificRenderWindow = model.renderWindow.newAPISpecificView(
userParams.viewAPI ?? model.defaultViewAPI
);
model.renderWindow.addView(model._apiSpecificRenderWindow);

// Interactor
model.interactor = vtkRenderWindowInteractor.newInstance();
model.interactor.setInteractorStyle(
vtkInteractorStyleTrackballCamera.newInstance()
);
model.interactor.setView(model._apiSpecificRenderWindow);
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._apiSpecificRenderWindow.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._apiSpecificRenderWindow.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._apiSpecificRenderWindow.delete,
() => {
window.removeEventListener('resize', publicAPI.resize);
},
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',
'_apiSpecificRenderWindow',
'interactor',
'container',
]);
macro.moveToProtected(publicAPI, model, ['_apiSpecificRenderWindow']);
macro.event(publicAPI, model, 'resize');

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

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

export const newInstance = macro.newInstance(extend);

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

export default { newInstance, extend };