Primer on VTK.wasm
This primer introduces the vtk namespace and the core interaction patterns used to create and manipulate VTK objects. If you want to jump straight to rendering, go to WebGL2 rendering within HTML canvas
All of these code snippets load the vtk-wasm JS library and the binaries using a <script> tag in the HTML - for convenience. See HTML Script Tag for details.
Create objects
Start by instantiating the class you need from the vtk namespace.
Print objects to console
Once you have an object, inspection usually comes next. Use toString() to invoke the underlying C++ vtkObject::Print() implementation, or use toJSON() to retrieve a JSON representation. In practice, the two outputs are often very similar.
The second console.log prints camera.state instead of camera because objects created through the vtk namespace are returned as JavaScript Proxy instances. In browser developer tools, the state property provides the most useful serialized view of that proxy.
The full proxy structure looks like this. Uncomment the last line to inspect it in the developer console:
Proxy(Object) {id: 1, obj: {…}, set: ƒ, observe: ƒ, toJSON: ƒ, …}
[[Handler]] : Object
get : get(d,h,O){return d[h]!==void 0?d[h]:d.userData[h]!==void 0?d.userData[h]:h==="then"?O:h==="state"?e.get?V(e.get(n)):(e.updateStateFromObject(n),V(e.getState(n))):h==="delete"?a:l[h]?l[h]():(d[h]=async(...Ee)=> {…}
set : ƒ set(d,h,O)
[[Prototype]] : Object
[[Target]] : Object
id : 1
obj : {Id: 1}
observe : ƒ p(d,h)
set : ƒ S(d)
toJSON : ƒ b()
toString : ƒ u()
unObserve : ƒ c(d)
unObserveAll : ƒ o()
userData : {}
[[Prototype]] : Object
[[IsRevoked]] : falseEdit object properties
Object properties can be read and assigned with standard . notation.
Call functions on objects
Member functions are also accessed with . notation. These calls return Promise objects, so use await when you need the resolved result.
Pass objects as arguments
The same object model extends to method arguments. When an API expects another VTK object, pass the proxy directly.
Delete
When an object is no longer needed, call delete() to release its external JavaScript reference to the underlying C++ instance. If another VTK object still owns that instance, the C++ object may remain alive. In JavaScript, clear your local reference immediately afterward so the handle is not reused accidentally.
Arrays
VTK provides array classes for typed data exchange. These arrays accept JavaScript TypedArray instances directly, which makes initialization straightforward.
User data
In addition to VTK-managed state, you can attach custom JavaScript properties to store application-specific metadata.
Observers
For interactive workflows, register event handlers with observe(). Remove a handler later by passing its returned tag to object.unObserve().
WebGL2 rendering within HTML canvas
Create a canvas in your HTML and ensure that you assign the id of the canvas to the renderWindow.canvasSelector property so VTK knows where to render, and to the renderWindowInteractor.canvasSelector property so VTK knows which element to receive events from.