JSCAD for LiaScript: Parametric 3D CAD Modeling in the Browser

JSCAD for LiaScript: Parametric 3D CAD Modeling in the Browser

Parametric 3D modeling is a core skill in mechanical engineering, product design, and TVET manufacturing programs. Teaching it usually requires CAD software installations, license management, and significant setup time.

The JSCAD template brings JSCAD — a JavaScript-based parametric 3D modeling library — to LiaScript. Students write JavaScript code that defines 3D geometry using the JSCAD API; the template compresses it and renders it in a fullscreen 3D viewer embedded in the course.


Quick Start

<!--
import: https://raw.githubusercontent.com/liaTemplates/jscad/master/README.md
-->

Two macros: @JSCAD in the fence opener for a static full-view model, and @JSCAD.eval for an editable version.


Macro 1: @JSCAD — Render a 3D Model

Place @JSCAD in the header of a JavaScript code block. The model is rendered in a fullscreen 3D viewer with a fullscreen button. The viewer supports mouse rotation, zoom, and pan.

Every JSCAD model requires the same structure:

  1. Import the primitives or operations you need
  2. Define a main() function that returns the geometry
  3. Export it with module.exports = { main }
```javascript @JSCAD
const { cube, sphere, cylinder } = require('@jscad/modeling').primitives
const { union, subtract } = require('@jscad/modeling').booleans
const { translate } = require('@jscad/modeling').transforms

const main = () => {
  const box = cube({ size: 30 })
  const hole = cylinder({ radius: 10, height: 40 })
  return subtract(box, hole)
}

module.exports = { main }
```

Try it live — rotate, zoom, and pan the 3D model in the viewer:


Macro 2: @JSCAD.eval — Editable Model

@JSCAD.eval is attached at the end of a JavaScript code block. The block is visible and editable; clicking run re-renders the model. The 3D viewer is smaller (inline height) to fit next to the code.

```javascript
const { cube } = require('@jscad/modeling').primitives
const { translate } = require('@jscad/modeling').transforms

const main = () => {
  return cube()
}

module.exports = { main }
```
@JSCAD.eval

JSCAD API Overview

JSCAD models are built by composing primitives and applying transformations and boolean operations:

Primitives (@jscad/modeling/primitives)

FunctionDescription
cube({ size })Cube with given side length
cuboid({ size: [x, y, z] })Box with different dimensions
sphere({ radius, segments })Sphere
cylinder({ radius, height })Cylinder
torus({ innerRadius, outerRadius })Torus
circle({ radius })2D circle (for extrusion)
rectangle({ size: [w, h] })2D rectangle
polygon({ points })2D polygon from point array
star({ vertices, outerRadius })2D star

Boolean Operations (@jscad/modeling/booleans)

FunctionDescription
union(a, b)Combine two solids
subtract(a, b)Subtract b from a
intersect(a, b)Keep only the overlap

Transforms (@jscad/modeling/transforms)

FunctionDescription
translate([x, y, z], obj)Move object
rotate([rx, ry, rz], obj)Rotate by angles (radians)
scale([sx, sy, sz], obj)Scale object

Extrusions (@jscad/modeling/extrusions)

FunctionDescription
extrudeLinear({ height }, shape)Linear extrusion of a 2D shape
extrudeRotate({ segments }, shape)Lathe extrusion around Z axis

Example: Threaded-Hole Part

```javascript @JSCAD
const jscad = require('@jscad/modeling')
const { cuboid, cylinder, cylinderElliptic } = jscad.primitives
const { subtract, union } = jscad.booleans
const { translate } = jscad.transforms

const main = () => {
  // Base block
  const base = cuboid({ size: [60, 40, 10] })

  // Four mounting holes
  const hole = cylinder({ radius: 3, height: 12 })
  const holes = [
    translate([-22,  14, 0], hole),
    translate([ 22,  14, 0], hole),
    translate([-22, -14, 0], hole),
    translate([ 22, -14, 0], hole),
  ]

  // Central boss
  const boss = cylinder({ radius: 10, height: 20 })
  const bossHole = cylinder({ radius: 6, height: 22 })
  const bossWithHole = subtract(boss, bossHole)

  return subtract(
    union(base, bossWithHole),
    ...holes
  )
}

module.exports = { main }
```

Try it live — inspect the mounting plate with boss and four holes from every angle:


Full Template Demo


Use Cases

Mechanical engineering courses — Teach part design concepts — extrusion, boolean operations, parametric dimensions — with live 3D models. Students modify a parameter (e.g., wall thickness) and see the updated part immediately.

TVET manufacturing training — Embed component drawings as 3D models in training materials. Students rotate and inspect the part from any angle, understanding spatial relationships before working with physical material.

Product design — Sketch 3D concepts in code. The parametric approach forces explicit design intent — every dimension is a variable, every constraint is code.

3D printing preparation — JSCAD models can be exported as STL from jscad.app. Teach students to design printable parts directly in the course.

Computer graphics education — The JSCAD pipeline (primitive → transform → boolean → render) mirrors graphics pipeline concepts from OpenGL and game engines.


Technical Facts

Runs in browserYes — via jscad.app iframe
Server requiredNo (local code); jscad.app loads the viewer
3D interactionYes — rotate, zoom, pan
Editable versionYes — @JSCAD.eval
ExportSTL available in the jscad.app viewer
Based onJSCAD / jscad.app by Davor Hrg
JSCAD version@jscad/modeling
LicenseMIT
MaintainedStable (version 0.0.1)

Try It

Try in LiaScript Open in LiveEditor View on GitHub
  • VTK — 3D scientific visualization with VTK.js and WebGL
  • AVR8js — simulate Arduino hardware in the browser
  • p5js — 2D canvas-based creative coding
  • mec2 — 2D mechanism and physics simulation

Related Posts

Chat-Simulation for LiaScript: Bring Dialogues to Life in Your Course

Use the Chat-Simulation template to embed animated, multi-participant chat conversations directly in your LiaScript course — ideal for dialogues, case studies, and collaborative learning scenarios.

Read More

DigiSim for LiaScript: Interactive Digital Circuit Simulation

Simulate digital logic circuits — from simple gates to D-latches and priority encoders — directly in LiaScript using the DigiSim template. Define circuits in JSON or JavaScript DSL.

Read More

WebSerial for LiaScript: Connect Your Code Editor to Real Hardware

Use the WebSerial template to send Python/MicroPython code directly from a LiaScript code block to a connected microcontroller (ESP32, Raspberry Pi Pico) via Chrome's Web Serial API.

Read More