Skip to content

JSCAD Tutorial: Parametric 3D Design with Code

March 17, 2026 • Tom Silas Helmke • Updated

This JSCAD tutorial builds one complete parametric part, a plate with two holes, and explains each line. The script runs in CADFaber's code mode, where its parameters become sliders, and it was also run with the @jscad/modeling library in Node.js to check the geometry.

Work along in the CADFaber Editor and use the complete guide as your reference while applying the steps below.

How code mode works

A CADFaber script is a JavaScript module that loads the library with require('@jscad/modeling'), exports a main function that returns geometry, and can export getParameterDefinitions to describe sliders. Parameter types are number, int, choice, checkbox and text.

The code runs in a Web Worker with a 10-second time limit, so an endless loop cannot freeze the editor. Code that comes from an imported project file needs your explicit permission before it runs.

The part: a plate with two holes

The plate is a cuboid with two cylinders subtracted. All five dimensions are parameters. The script clamps the hole spacing so the holes can never leave the plate, which a slider range alone cannot guarantee.

Parametric plate with two holes (paste into code mode)
const jscad = require('@jscad/modeling')
const { cuboid, cylinder } = jscad.primitives
const { subtract } = jscad.booleans
const { translate } = jscad.transforms

const getParameterDefinitions = () => [
  { name: 'width', type: 'number', initial: 60, min: 30, max: 120, step: 1, caption: 'Width (mm)' },
  { name: 'height', type: 'number', initial: 30, min: 20, max: 80, step: 1, caption: 'Height (mm)' },
  { name: 'thickness', type: 'number', initial: 4, min: 2, max: 10, step: 0.5, caption: 'Thickness (mm)' },
  { name: 'holeDiameter', type: 'number', initial: 5, min: 2, max: 12, step: 0.5, caption: 'Hole diameter (mm)' },
  { name: 'holeSpacing', type: 'number', initial: 40, min: 10, max: 100, step: 1, caption: 'Hole spacing (mm)' },
]

const main = (p) => {
  // keep at least 4 mm of material between a hole and the plate edge
  const spacing = Math.min(p.holeSpacing, p.width - p.holeDiameter - 8)
  const plate = cuboid({ size: [p.width, p.height, p.thickness] })
  const hole = (x) =>
    translate([x, 0, 0], cylinder({ radius: p.holeDiameter / 2, height: p.thickness + 2, segments: 48 }))
  return subtract(plate, hole(-spacing / 2), hole(spacing / 2))
}

module.exports = { main, getParameterDefinitions }

What the geometry measures

With the default values, the library reports a bounding box of 60 x 30 x 4 mm and a volume of about 7,043 mm3. That matches the hand calculation: 60 x 30 x 4 is 7,200 mm3, and two cylinders of 5 mm diameter and 4 mm height remove about 157 mm3. Checking a number like this is the fastest way to know a script does what you think.

Mistakes that cost time

These are the ones that show up most often in first scripts:

  • Primitives are centered on the origin. A cuboid of size [60, 30, 4] spans -2 to +2 on the thickness axis, not 0 to 4.
  • A cylinder's axis is Z by default. Make its height larger than the plate thickness so the cut goes all the way through.
  • Round holes need enough segments. 48 segments gives a smooth 5 mm hole; a low segment count looks faceted on small radii.
  • Parameters that depend on each other need code, not slider limits. Without the clamp in the script above, a hole spacing of 70 mm on a 60 mm plate would put both holes outside the plate, and the result would be a plate with no holes at all (a volume of 7,200 mm3).
  • Expose only values someone will really change. Five sliders are easier to use than twenty.

Export and verify

Export STL (Binary) and open it in your slicer to confirm 60 x 30 x 4 mm and a hole that is really 5 mm wide. Then change one slider at a time and check the result again, instead of changing several values and guessing which one broke the print.

One limit to know about: the triangles that JSCAD produces after a boolean operation can meet at T-junctions, where a vertex of one triangle lies on the edge of its neighbor. A strict check that every edge belongs to exactly two triangles counts such edges as unmatched (124 for this plate) even though the solid is closed: the exported mesh encloses the same 7,043 mm3 that JSCAD reports. Slicers usually repair this silently, but if yours warns about holes or non-manifold edges, that is the likely reason, and the preview will show whether the part looks right.

Where to go next

Start from the template library: the Parametric Box with Hole, Gear, Cable Clip and Desk Organizer templates show different techniques, such as extrusions, loops and nested subtractions. Read one, change a value, and compare the result before writing your own.


Related