# Getting started

Render a rotating cube inside an HTML card.



## Install [#install]

```sh
pnpm add portalgl three
```

Use Three.js r184 or later. PortalGL manages element measurements automatically.

Run the setup below in the browser after your HTML exists; imports themselves
are safe during server rendering.

## Give the view a size [#give-the-view-a-size]

```html
<div id="card" aria-hidden="true"></div>
```

```css
body { margin: 0; }
#card { width: min(100%, 360px); height: 280px; }
```

Use a host with no padding or border. Add decoration to a wrapper instead.
The container holds the WebGL2 fallback's shared canvas; this example uses `document.body`.

## Mount a scene [#mount-a-scene]

Save this as `mount-card.ts`. It creates a view, starts one animation loop, and
returns a function to call when the card is removed.

```ts title="mount-card.ts"
import { ThreeDOM } from 'portalgl/three'
import {
  BoxGeometry,
  Mesh,
  MeshNormalNodeMaterial,
  PerspectiveCamera,
  Scene,
  WebGPURenderer,
} from 'three/webgpu'

export async function mountCard(element: HTMLElement) {
  const renderer = new WebGPURenderer({ alpha: true, antialias: true })
  await renderer.init()

  const dom = new ThreeDOM({ renderer, container: document.body })

  const scene = new Scene()
  const camera = new PerspectiveCamera(45, 1, 0.1, 100)
  camera.position.z = 3

  const geometry = new BoxGeometry()
  const material = new MeshNormalNodeMaterial()
  const cube = new Mesh(geometry, material)
  scene.add(cube)

  dom.addView(element, {
    scene,
    camera,
    onFrame: ({ delta }) => {
      cube.rotation.x += delta * 0.3
      cube.rotation.y += delta * 0.5
    },
  })

  function destroy() {
    renderer.setAnimationLoop(null)
    dom.destroy()
    geometry.dispose()
    material.dispose()
    renderer.dispose()
  }

  try {
    await dom.prepare()
  } catch (error) {
    destroy()
    throw error
  }

  renderer.setAnimationLoop((time) => {
    dom.update(time)
  })

  return destroy
}
```

Call it from your browser entrypoint:

```ts
import { mountCard } from './mount-card'

const card = document.querySelector<HTMLElement>('#card')!
const unmount = await mountCard(card)
// Call unmount() when leaving the page or removing the card.
```

Scroll or resize the page: the view follows its host. `ThreeDOM` creates the
canvas and updates the camera's aspect ratio. `WebGPURenderer` uses native WebGPU
when available and can fall back to WebGL2, where every view shares one canvas
(see [WebGL2 fallback](./reference.md#webgl2-fallback-one-canvas)).

## The three calls to remember [#the-three-calls-to-remember]

| Call                  | When                                                                        |
| --------------------- | --------------------------------------------------------------------------- |
| `await dom.prepare()` | Optional, before starting the loop: prepare the first draw of sized views   |
| `dom.update(time)`    | Each frame: measure, position, animate, and render                          |
| `dom.destroy()`       | On teardown: release views and tracking; dispose your own assets separately |

`delta` in `onFrame` is in seconds. Offscreen views pause their animation and
rendering automatically. Keep the main loop running so they can become active again.

For an existing application, reuse its initialized renderer and loop. Register
additional content on the same `ThreeDOM` instance; each view needs its own camera.

Next: [views and 3D objects](./dom-track.md), [image planes](./projected-quad.md),
or [post-processing](./dom-pipelines.md).
