Learn the fundamental concepts and syntax for creating 3D models with JSCAD.
Every JSCAD file follows a simple structure with a main function that returns geometry.
// Import JSCAD functions
const { cube, sphere, cylinder } = require('@jscad/modeling').primitives
const { translate, rotate, scale } = require('@jscad/modeling').transforms
const { union, subtract, intersect } = require('@jscad/modeling').booleans
// Main function - this is what gets executed
function main() {
// Your geometry code goes here
return cube({size: 10})
}
// Export the main function
module.exports = { main }Import the functions you need from JSCAD modules
Define your geometry in the main() function
Export the main function for JSCAD to use
Primitives are the basic building blocks of 3D geometry. JSCAD provides several types of primitives.
Creates a rectangular box with specified dimensions.
// Simple cube
const cube1 = cube({size: 10})
// Cube with specific dimensions
const cube2 = cube({
size: [10, 20, 5]
})
// Cube centered at origin
const cube3 = cube({
size: 10,
center: [0, 0, 0]
})size - Number or array [x, y, z]center - Boolean or array [x, y, z]Tip: Use size: [x, y, z] for non-uniform dimensions.
Creates a perfect sphere with specified radius and resolution.
// Simple sphere
const sphere1 = sphere({radius: 5})
// High-resolution sphere
const sphere2 = sphere({
radius: 5,
segments: 32
})
// Sphere with custom center
const sphere3 = sphere({
radius: 5,
center: [10, 0, 0]
})radius - Number (default: 1)segments - Number (default: 16)center - Array [x, y, z] (default: [0, 0, 0])Note: Higher segment counts create smoother spheres but increase file size.
Creates a circular cylinder with specified radius, height, and resolution.
// Simple cylinder
const cylinder1 = cylinder({radius: 5, height: 10})
// Cylinder with segments
const cylinder2 = cylinder({
radius: 5,
height: 10,
segments: 32
})
// Cylinder with different top/bottom radius
const cylinder3 = cylinder({
radius: [5, 3], // [bottom, top]
height: 10
})radius - Number or array [bottom, top]height - Numbersegments - Number (default: 16)center - Array [x, y, z] (default: [0, 0, 0])Tip: Use different top/bottom radii to create cones or tapered cylinders.
Creates a donut-shaped object with specified outer radius, inner radius, and resolution.
// Simple torus
const torus1 = torus({outerRadius: 5, innerRadius: 1})
// High-resolution torus
const torus2 = torus({
outerRadius: 5,
innerRadius: 1,
outerSegments: 32,
innerSegments: 16
})
// Torus with custom center
const torus3 = torus({
outerRadius: 5,
innerRadius: 1,
center: [10, 0, 0]
})outerRadius - Number (distance from center)innerRadius - Number (thickness of ring)outerSegments - Number (default: 16)innerSegments - Number (default: 8)center - Array [x, y, z] (default: [0, 0, 0])Note: innerRadius must be smaller than outerRadius.
Transformations modify the position, orientation, and size of geometric objects.
Moves an object to a new position in 3D space.
// Move along X axis
const moved1 = translate([10, 0, 0], cube({size: 5}))
// Move along multiple axes
const moved2 = translate([5, 10, 2], sphere({radius: 3}))
// Move relative to current position
const moved3 = translate([0, 0, 5], cylinder({radius: 2, height: 10}))offset - Array [x, y, z] (translation vector)object - Geometry to translateTip: Use negative values to move in the opposite direction.
Rotates an object around specified axes by given angles.
// Rotate around Z axis (90 degrees)
const rotated1 = rotate([0, 0, Math.PI/2], cube({size: 5}))
// Rotate around multiple axes
const rotated2 = rotate([Math.PI/4, 0, Math.PI/6], sphere({radius: 3}))
// Rotate around custom center
const rotated3 = rotateX(Math.PI/2, cylinder({radius: 2, height: 10}))angles - Array [x, y, z] (radians)object - Geometry to rotateNote: Angles are in radians. Use Math.PI/2 for 90 degrees.
Resizes an object by scaling factors along each axis.
// Uniform scaling
const scaled1 = scale(2, cube({size: 5}))
// Non-uniform scaling
const scaled2 = scale([1, 2, 0.5], sphere({radius: 3}))
// Scale from custom center
const scaled3 = scale([2, 2, 2], cylinder({radius: 2, height: 10}))factors - Number or array [x, y, z]object - Geometry to scaleTip: Use values less than 1 to shrink, greater than 1 to enlarge.
Boolean operations combine multiple objects using mathematical set operations.
Combines multiple objects into a single object, removing overlaps.
// Combine two objects
const combined = union(
cube({size: 10}),
sphere({radius: 5})
)
// Combine multiple objects
const complex = union(
cube({size: 10}),
translate([5, 0, 0], sphere({radius: 3})),
translate([0, 5, 0], cylinder({radius: 2, height: 8}))
)Tip: Union is the most common Boolean operation for combining parts.
Removes the volume of one object from another object.
// Create a hole
const withHole = subtract(
cube({size: 10}),
cylinder({radius: 2, height: 12})
)
// Create multiple holes
const withHoles = subtract(
cube({size: 10}),
union(
translate([-2, -2, 0], cylinder({radius: 1, height: 12})),
translate([2, 2, 0], cylinder({radius: 1, height: 12}))
)
)Note: The first object is the base, the second is what gets removed.
Keeps only the volume where objects overlap.
// Keep only overlap
const overlap = intersect(
cube({size: 10}),
sphere({radius: 6})
)
// Complex intersection
const complex = intersect(
cube({size: 10}),
translate([5, 0, 0], sphere({radius: 8})),
translate([0, 5, 0], cylinder({radius: 4, height: 12}))
)Tip: Intersect is useful for creating precise geometric constraints.
Here's a complete example that demonstrates all the basic concepts:
const { cube, sphere, cylinder } = require('@jscad/modeling').primitives
const { translate, rotate, scale } = require('@jscad/modeling').transforms
const { union, subtract } = require('@jscad/modeling').booleans
function main() {
// Create base plate
const base = cube({size: [20, 20, 2]})
// Create central cylinder
const centerCylinder = translate([0, 0, 1],
cylinder({radius: 3, height: 8})
)
// Create corner spheres
const corner1 = translate([8, 8, 1], sphere({radius: 2}))
const corner2 = translate([-8, 8, 1], sphere({radius: 2}))
const corner3 = translate([8, -8, 1], sphere({radius: 2}))
const corner4 = translate([-8, -8, 1], sphere({radius: 2}))
// Create holes in base
const holes = union(
translate([8, 8, 0], cylinder({radius: 1, height: 3})),
translate([-8, 8, 0], cylinder({radius: 1, height: 3})),
translate([8, -8, 0], cylinder({radius: 1, height: 3})),
translate([-8, -8, 0], cylinder({radius: 1, height: 3}))
)
// Combine everything
const result = union(
subtract(base, holes),
centerCylinder,
corner1, corner2, corner3, corner4
)
return result
}
module.exports = { main }Create base plate with holes
Add central cylinder
Add corner spheres
Now that you understand the basics, explore these advanced topics: