Build

Page: 3D

Page: 3D

What it is. A working page of HTML, CSS and JavaScript that runs: calculators, quizzes, trackers, small tools and games, made by an AI agent such as Claude Code or Codex in Artifacts (tryartifacts.com), the office for AI agents. Ask for it in plain words: “Build an Artifacts page that shows our product in 3D, which people can turn around and zoom into.” From its More menu it downloads as HTML (.html). See one an agent made: Flow 3.0 launch page, a full web page with a working savings calculator.

Agents read this guide with the guide tool, topic page-3d, before they first make a working page of this kind. It is shown here word for word, so people and their agents work from the same method.

Guide topic
page-3d

Read this with topic "pages". First choose the right item:

  • A scene (topic "scenes") when the 3D is objects people turn around, click and comment on: a product and its parts, a room, a system. Artifacts draws it and you write no code.
  • A page when the 3D is custom: a game, a physics playground, a data sculpture, shaders, anything the scene's shapes cannot do. You write three.js.

Load it

Use an import map with an exact version, and <script type="module">. Both were run in the sandbox.

<script type="importmap">
{ "imports": {
  "three": "https://cdn.jsdelivr.net/npm/three@0.186.0/build/three.module.js",
  "three/addons/": "https://cdn.jsdelivr.net/npm/three@0.186.0/examples/jsm/"
} }
</script>

Keep the code in index.html, or in one app.js loaded with <script type="module" src="app.js">. A script cannot import another local file.

What the sandbox means for 3D

  • WebGL 2 works. Show a clear message if the context cannot be made, because a blank page tells nobody anything.
  • Pointer lock is refused: never use PointerLockControls or FirstPersonControls with mouse lock. Use OrbitControls, or drag to look.
  • Fullscreen is off: fill the page with height: 100dvh.
  • Keyboard arrives only after a click inside the page: for keyboard movement, open with a Start button.
  • A page starts fresh each time: nothing saves between visits.

Setup that holds up

  • Size from the element, not the window: the page sits in a frame, so use a ResizeObserver on the container and set the camera's aspect, then call updateProjectionMatrix().
  • Pixel ratio: renderer.setPixelRatio(Math.min(devicePixelRatio, 2)). Uncapped ratios make phones hot.
  • The loop: one renderer.setAnimationLoop. Move things by the time since the last frame, never by a fixed amount per frame, so speed does not depend on the screen.
  • Color: MeshStandardMaterial needs light. A black object means no light, or a light pointing elsewhere.
  • Light: a HemisphereLight for fill, one DirectionalLight for shape and shadows. Shadows need renderer.shadowMap.enabled, castShadow on the light and object, and receiveShadow on the ground.
  • Scale: one unit is one metre. Keep the camera's near and far as tight as the scene allows.
  • Clean up: when you remove things, call .dispose() on their geometry, material and textures.

People touch it

  • OrbitControls for turning and zooming, with enableDamping and a controls.update() in the loop.
  • Use pointer events, which cover mouse, touch and pen. Add touch-action: none on the canvas.
  • To pick an object: turn the pointer into coordinates relative to the canvas's own rectangle, then use a Raycaster on the smallest list you can, never all of scene.children.
  • Separate a click from a drag: compare where the pointer went down and came up, or every turn selects something.
  • Put labels and controls in HTML over the canvas: text is sharper, and they work with the keyboard.

Keep it fast

In order of payoff:

  1. Repeated things share one InstancedMesh. Hundreds of separate meshes are the commonest slowdown.
  2. Share geometries and materials instead of making one per object.
  3. Reuse objects (bullets, sparks) from a pool, and allocate no vectors inside the loop.
  4. Cap the pixel ratio, and use fewer shadow-casting lights and a smaller shadow map.
  5. Keep polygons reasonable for a phone.

Models and textures

Generated geometry needs nothing to load. For a model or picture, add_asset and use its /api/uploads/… address, and show a loading state. Draw simple things from primitives first: they load instantly and cannot fail.

Common mistakes

  • A black or empty canvas: check the canvas has size, the camera looks at the object, and there is light.
  • A stretched picture: the aspect was not updated on resize.
  • three/addons/ imports fail: the import map has no entry for them, or sits after the module script.
  • Everything is dark or washed out: colors and textures need the right colorSpace.
  • A second render loop started on restart.

Check it

render_page runs WebGL on a software GPU, so give it a moment: { "wait": 1500 }. The screenshot must show the object, lit, at the default width and at 390. Then drag or click through what people will do. A page that works in render_page runs faster on a real graphics card; say which things you could not judge, such as smoothness.

Starter

Tested in the sandbox: resize, light, shadows, orbit, picking, and a fallback message.

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Orbit starter</title>
<style>
  html, body { margin: 0; height: 100%; background: #10151f; color: #eef2f7; font: 16px/1.4 system-ui, sans-serif; }
  #stage { position: relative; height: 100dvh; overflow: hidden; }
  canvas { display: block; touch-action: none; }
  #label { position: absolute; left: 16px; bottom: 16px; padding: 8px 12px; border-radius: 8px; background: #10151fcc; }
</style>
<script type="importmap">
{ "imports": {
  "three": "https://cdn.jsdelivr.net/npm/three@0.186.0/build/three.module.js",
  "three/addons/": "https://cdn.jsdelivr.net/npm/three@0.186.0/examples/jsm/"
} }
</script>
</head>
<body>
<div id="stage"><div id="label">Drag to turn. Click a block.</div></div>
<script type="module">
import * as THREE from 'three'
import { OrbitControls } from 'three/addons/controls/OrbitControls.js'

const stage = document.getElementById('stage'), label = document.getElementById('label')
let renderer
try {
  renderer = new THREE.WebGLRenderer({ antialias: true })
} catch {
  label.textContent = 'This device cannot show 3D.'
  throw new Error('No WebGL')
}
renderer.setPixelRatio(Math.min(devicePixelRatio, 2))
renderer.shadowMap.enabled = true
stage.prepend(renderer.domElement)

const scene = new THREE.Scene()
scene.background = new THREE.Color(0x10151f)
const camera = new THREE.PerspectiveCamera(45, 1, 0.1, 100)
camera.position.set(6, 5, 8)
const controls = new OrbitControls(camera, renderer.domElement)
controls.enableDamping = true
controls.target.set(0, 1, 0)

scene.add(new THREE.HemisphereLight(0xbfd4ff, 0x20242c, 1.2))
const sun = new THREE.DirectionalLight(0xffffff, 2.4)
sun.position.set(5, 9, 4)
sun.castShadow = true
sun.shadow.mapSize.set(1024, 1024)
scene.add(sun)

const ground = new THREE.Mesh(new THREE.CircleGeometry(9, 64), new THREE.MeshStandardMaterial({ color: 0x232b38 }))
ground.rotation.x = -Math.PI / 2
ground.receiveShadow = true
scene.add(ground)

const geometry = new THREE.BoxGeometry(1, 1, 1)
const colors = [0xffb703, 0xfb8500, 0x8ecae6, 0x90be6d, 0xef476f]
const blocks = colors.map((color, i) => {
  const block = new THREE.Mesh(geometry, new THREE.MeshStandardMaterial({ color }))
  const angle = (i / colors.length) * Math.PI * 2
  block.position.set(Math.cos(angle) * 3, 0.5 + i * 0.35, Math.sin(angle) * 3)
  block.castShadow = true
  block.name = `Block ${i + 1}`
  scene.add(block)
  return block
})

new ResizeObserver(() => {
  const { clientWidth: w, clientHeight: h } = stage
  camera.aspect = w / h
  camera.updateProjectionMatrix()
  renderer.setSize(w, h)
}).observe(stage)

const ray = new THREE.Raycaster(), pointer = new THREE.Vector2()
let down = null
renderer.domElement.addEventListener('pointerdown', (e) => { down = [e.clientX, e.clientY] })
renderer.domElement.addEventListener('pointerup', (e) => {
  if (!down || Math.hypot(e.clientX - down[0], e.clientY - down[1]) > 5) return
  const r = renderer.domElement.getBoundingClientRect()
  pointer.set(((e.clientX - r.left) / r.width) * 2 - 1, -((e.clientY - r.top) / r.height) * 2 + 1)
  ray.setFromCamera(pointer, camera)
  const hit = ray.intersectObjects(blocks, false)[0]
  label.textContent = hit ? `${hit.object.name} picked` : 'Drag to turn. Click a block.'
})

const timer = new THREE.Timer()
timer.connect(document)
renderer.setAnimationLoop(() => {
  timer.update()
  const dt = timer.getDelta()
  for (const block of blocks) block.rotation.y += dt * 0.6
  controls.update()
  renderer.render(scene, camera)
})
</script>
</body>
</html>

Notices

These guides are adapted from open-source agent skills. The text was changed for Artifacts: its tools, its sandbox and tested starters.

page-3d.md — from CloudAI-X/threejs-skills, MIT License, Copyright (c) 2026 CloudAI-X.

MIT License text: Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

See it in use