Open in a new tab

A software-3D heightmap terrain of 4 608 triangles, projected and drawn as SVG (no canvas, no WebGL). The camera stands at the centre at peak height and orbits slowly; the first press on the terrain stops the orbit, and dragging then turns the camera.

Uses @awacloud/graphic-anim (Apache-2.0). This example's code is Apache-2.0.

When to use it

A lightweight 3D look that must stay vector, accessible and printable: a relief, a landscape backdrop, a data surface. For heavy or interactive 3D scenes, use a WebGL library.

Copy-paste steps

  1. npm install @awacloud/fw @awacloud/graphic-anim
  2. Serve node_modules/@awacloud/fw/src/ at /vendor/fw/ and node_modules/@awacloud/graphic-anim/src/ at /vendor/graphic-anim/ (node serve.js from the examples root does both; any static server works).
  3. Copy index.html, main.js, heightmap.js and style.css next to each other and open index.html through that server. The ../../_style/examples.css link is optional: copy that file along, or delete the link.

What it shows

  • graphic.svg3d is a composed module (the Svg3d class and createTerrain) that is not part of the graphic.anim aggregate. registerAllDeep(graphicModules) registers it with its kernels (vector maths, mesh, projection and the SVG rig builder), and fw.runtime.resolve('graphic.svg3d') returns it.
  • createTerrain(null, { side, heights }, options) meshes a square heightmap. The options set the viewport, the height scale, the number of painter slices (buckets), faceColors (an altitude colour and a shade per face, water at sea level), the camera, the orbit duration and the events module that carries the pointer look.
  • heightmap.js generates the terrain in code from two sine terms, with fixed coefficients: no asset, no fetch and no random number, so the same terrain appears on every load.
  • Each instance takes a fresh prefix for its ids; dispose() stops the orbit, unbinds the pointer look and the page removes the node.
  • Limits: the camera and terrain options are fixed in main.js; the page has no controls beyond the pointer look.

Code

main.js

javascriptApache-2.046 lines

// SPDX-License-Identifier: Apache-2.0
import fw from '@awacloud/fw';
import { events } from '@awacloud/fw/dom/query/events.js';
import { graphicModules } from '@awacloud/graphic-anim';
import { proceduralHeights } from './heightmap.js';

// graphic.svg3d is a composed module: registering the package pulls its kernels (vec3, mesh, project, apply).
fw.runtime.registerAllDeep([events, ...graphicModules]);
const { createTerrain } = fw.runtime.resolve('graphic.svg3d');

/** Quads per side: 2 * 48 * 48 = 4 608 triangles. */
export const CELLS = 48;
const HEIGHT_SCALE = 220; // world height of a sample of 1
const SIZE = { width: 1280, height: 800 }; // the viewport, as svg3d.project's defaults
let instance = 0;

/**
 * Build the terrain into `mount`: the camera stands at the centre at peak height, looking level, and orbits
 * slowly until the first press; then dragging the terrain turns the camera (`events` carries the pointer).
 * @param {SVGElement} mount
 * @returns {{ terrain: any, dispose(): void }}
 */
export function mountTerrain(mount) {
    const side = CELLS + 1;
    const terrain = createTerrain(null, { side, heights: proceduralHeights(side) }, {
        prefix: `svg3d-${instance++}`, // a fresh id scope per instance
        ...SIZE,
        heightScale: HEIGHT_SCALE,
        buckets: 8, // painter slices, each split into face-colour classes
        faceColors: true, // altitude colours with per-face shading; sea-level faces are water
        camera: { y: -HEIGHT_SCALE * (250 / 255), depth: 350, rx: 0, ry: 0 },
        duration: 40000, // one full yaw revolution, ms
        events: fw.runtime.resolve('events'),
    });
    mount.appendChild(terrain.node);
    terrain.play();
    return {
        terrain,
        dispose() {
            terrain.dispose(); // stops the orbit and unbinds the pointer look
            terrain.node.remove();
        },
    };
}

export const view = mountTerrain(/** @type {SVGElement} */ (document.getElementById('mount')));

index.html

htmlApache-2.025 lines

<!doctype html>
<!-- SPDX-License-Identifier: Apache-2.0 -->
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>graphic-anim · A 3D terrain in plain SVG</title>
  <link rel="stylesheet" href="../../_style/examples.css">
  <link rel="stylesheet" href="./style.css">
  <script type="importmap">
  { "imports": {
    "@awacloud/fw": "/vendor/fw/main.js", "@awacloud/fw/": "/vendor/fw/",
    "@awacloud/graphic-anim": "/vendor/graphic-anim/main.js", "@awacloud/graphic-anim/": "/vendor/graphic-anim/" } }
  </script>
  <script type="module" src="./main.js"></script>
</head>
<body>
  <main>
    <h1>A 3D terrain in plain SVG</h1>
    <p>A procedural heightmap of 4 608 triangles, drawn without canvas. The camera orbits slowly until you drag
       the terrain; then the drag turns the camera.</p>
    <svg id="mount" viewBox="0 0 1280 800" role="img" aria-label="A 3D terrain with valleys, peaks and water"></svg>
  </main>
</body>
</html>

heightmap.js

javascriptApache-2.026 lines

// SPDX-License-Identifier: Apache-2.0
// A deterministic procedural heightmap: no asset, no fetch, no random number.

/** Normalised sea level: samples below it become water (height 0). */
export const SEA_LEVEL = 0.3;

/**
 * A low-frequency swell (two out-of-phase sines) layered with a higher-frequency ridge term, fixed coefficients.
 * Samples below `SEA_LEVEL` are 0, the rest are rescaled to [0, 1].
 * @param {number} side samples per side
 * @returns {Float64Array} `side * side` samples, row by row
 */
export function proceduralHeights(side) {
    const heights = new Float64Array(side * side);
    for (let j = 0; j < side; j += 1) {
        const v = j / (side - 1);
        for (let i = 0; i < side; i += 1) {
            const u = i / (side - 1);
            const swell = 0.5 + 0.5 * Math.sin(u * Math.PI * 2.3) * Math.cos(v * Math.PI * 1.6);
            const ridge = Math.abs(Math.sin((u * 3.1 + v * 2.7) * Math.PI));
            const h = (0.55 * swell + 0.45 * ridge - SEA_LEVEL) / (1 - SEA_LEVEL);
            heights[j * side + i] = Math.min(1, Math.max(0, h));
        }
    }
    return heights;
}

style.css

cssApache-2.02 lines

/* SPDX-License-Identifier: Apache-2.0 */
#mount { display: block; width: 100%; height: auto; background: #1a1a1a; border-radius: 8px; touch-action: none; }