Wireframe Box Animation

This post demonstrates a 3D wireframe cube animation using JavaScript-generated SVG with normalized coordinates and matrix transformations.

Animated Wireframes:

Technical Features

3D Mathematics

  • Matrix transformations: Full 4x4 matrix operations for 3D transformations
  • Perspective projection: Realistic 3D-to-2D conversion with depth
  • Rotation matrices: Separate X, Y, Z rotation calculations
  • Vertex transformation: Homogeneous coordinate system

Wireframe Rendering

  • Cube definition: 8 vertices, 12 edges in normalized space
  • Edge rendering: Dynamic line drawing with SVG
  • Vertex highlighting: Color-coded corner points
  • Depth sorting: Simple back-face culling

Animation Types

  1. Single Box: Classic rotating wireframe cube
  2. Multiple Boxes: Several cubes with different scales and positions
  3. Tunnel Effect: Infinite tunnel of rotating wireframes

Coordinate System

// 3D cube vertices in normalized coordinates (-1 to 1)
const cubeVertices = [
    [-1, -1,  1], [ 1, -1,  1], [ 1,  1,  1], [-1,  1,  1], // Front
    [-1, -1, -1], [ 1, -1, -1], [ 1,  1, -1], [-1,  1, -1]  // Back
];

// Transform to screen space
function transformPoint(matrix, x, y, z) {
    const w = matrix[12] * x + matrix[13] * y + matrix[14] * z + matrix[15];
    return {
        x: (matrix[0] * x + matrix[1] * y + matrix[2] * z + matrix[3]) / w,
        y: (matrix[4] * x + matrix[5] * y + matrix[6] * z + matrix[7]) / w
    };
}

Performance Optimizations

  • Efficient matrix math: Minimal allocations in animation loop
  • SVG reuse: Complete regeneration for smooth animation
  • Culling: Skip rendering of vertices/edges behind camera
  • Color cycling: HSL color space for smooth transitions

This wireframe system demonstrates how to build a complete 3D rendering pipeline using just SVG and JavaScript, with normalized coordinates providing a clean mathematical foundation!

Tags: