Skip to content

Spatial transformation operations for 2D and 3D geometry: translation, rotation (with axis-specific shortcuts), uniform and non-uniform scaling, mirroring across a plane, centering on axes, and multi-geometry alignment. All operations return new geometry without modifying the inputs.

See also:

function translate(vector: Vec3, geometries: AnyGeom | AnyGeom[]): AnyGeom | AnyGeom[]

Move geometry by an offset vector along the X, Y, and Z axes.

Parameters:

Name Type Description
vector — [dx, dy, dz] translation offset.
geometries — One geometry or an array of geometries to move.

Returns: The translated geometry or array.

const { cube } = jscad.primitives
const { translate } = jscad.transforms
async function main() {
return translate([10, 0, 5], cube({ size: 4 }))
}

See also:

function translateX(offset: number, geometries: AnyGeom | AnyGeom[]): AnyGeom | AnyGeom[]

Move geometry along the X axis only.

Parameters:

Name Type Description
offset — Distance to move along X.
geometries — One geometry or an array of geometries to move.

Returns: The translated geometry or array.

const { sphere } = jscad.primitives
const { translateX } = jscad.transforms
async function main() {
return translateX(5, sphere({ radius: 2 }))
}

See also:

function translateY(offset: number, geometries: AnyGeom | AnyGeom[]): AnyGeom | AnyGeom[]

Move geometry along the Y axis only.

Parameters:

Name Type Description
offset — Distance to move along Y.
geometries — One geometry or an array of geometries to move.

Returns: The translated geometry or array.

See also:

function translateZ(offset: number, geometries: AnyGeom | AnyGeom[]): AnyGeom | AnyGeom[]

Move geometry along the Z axis only.

Parameters:

Name Type Description
offset — Distance to move along Z.
geometries — One geometry or an array of geometries to move.

Returns: The translated geometry or array.

See also:

function rotate(angles: Vec3, geometries: AnyGeom | AnyGeom[]): AnyGeom | AnyGeom[]

Rotate geometry around the X, Y, and Z axes by the given angles.

Rotations are applied in X → Y → Z order. All angles are in radians. Use the utils.degToRad() helper to convert from degrees.

Parameters:

Name Type Description
angles — [rx, ry, rz] rotation angles in radians.
geometries — One geometry or an array of geometries to rotate.

Returns: The rotated geometry or array.

const { cylinder } = jscad.primitives
const { rotate } = jscad.transforms
const { degToRad } = jscad.utils
async function main() {
return rotate([degToRad(90), 0, 0], cylinder({ height: 8, radius: 2 }))
}

See also:

function rotateX(angle: number, geometries: AnyGeom | AnyGeom[]): AnyGeom | AnyGeom[]

Rotate geometry around the X axis by the given angle in radians.

Parameters:

Name Type Description
angle — Rotation angle in radians.
geometries — One geometry or an array of geometries to rotate.

Returns: The rotated geometry or array.

See also:

function rotateY(angle: number, geometries: AnyGeom | AnyGeom[]): AnyGeom | AnyGeom[]

Rotate geometry around the Y axis by the given angle in radians.

Parameters:

Name Type Description
angle — Rotation angle in radians.
geometries — One geometry or an array of geometries to rotate.

Returns: The rotated geometry or array.

See also:

function rotateZ(angle: number, geometries: AnyGeom | AnyGeom[]): AnyGeom | AnyGeom[]

Rotate geometry around the Z axis by the given angle in radians.

Parameters:

Name Type Description
angle — Rotation angle in radians.
geometries — One geometry or an array of geometries to rotate.

Returns: The rotated geometry or array.

See also:

function scale(factors: Vec3, geometries: AnyGeom | AnyGeom[]): AnyGeom | AnyGeom[]

Scale geometry along the X, Y, and Z axes.

Parameters:

Name Type Description
factors — [sx, sy, sz] scale factors. Values > 1 enlarge; values < 1 shrink.
geometries — One geometry or an array of geometries to scale.

Returns: The scaled geometry or array.

const { sphere } = jscad.primitives
const { scale } = jscad.transforms
async function main() {
return scale([2, 1, 0.5], sphere({ radius: 3 }))
}

See also:

function mirror( options: { origin?: Vec3; normal?: Vec3 }, geometries: AnyGeom | AnyGeom[] ): AnyGeom | AnyGeom[]

Mirror (reflect) geometry across a plane defined by an origin and normal vector.

Parameters:

Name Type Description
options — Mirror options.
options.origin — A point on the mirror plane. Default [0, 0, 0].
options.normal — Normal vector of the mirror plane. Default [0, 0, 1].
geometries — One geometry or an array of geometries to mirror.

Returns: The mirrored geometry or array.

const { cylinder } = jscad.primitives
const { mirror, translate } = jscad.transforms
async function main() {
const half = translate([5, 0, 0], cylinder({ height: 10, radius: 2 }))
return [half, mirror({ normal: [1, 0, 0] }, half)]
}

See also:

function center( options: { axes?: [boolean, boolean, boolean]; relativeTo?: AnyGeom }, geometries: AnyGeom | AnyGeom[] ): AnyGeom | AnyGeom[]

Move geometry so that its bounding box is centred on the specified axes.

Parameters:

Name Type Description
options — Center options.
options.axes — Which axes to center on [x, y, z]. Default [true, true, true].
options.relativeTo — Reference geometry; if given, centers relative to its bounding box.
geometries — One geometry or an array of geometries to center.

Returns: The recentred geometry or array.

const { cuboid } = jscad.primitives
const { center } = jscad.transforms
async function main() {
return center({ axes: [true, true, false] }, cuboid({ size: [10, 6, 2] }))
}

See also:

function align( options: { modes?: Array<'min' | 'center' | 'max' | 'none'>; relativeTo?: AnyGeom[]; grouped?: boolean; }, ...geometries: AnyGeom[] ): AnyGeom[]

Align multiple geometries relative to each other or to an absolute position.

Each axis independently snaps to the minimum, centre, or maximum of the combined bounding box.

Parameters:

Name Type Description
options — Align options.
options.modes — Per-axis mode: ‘min’, ‘center’, ‘max’, or ‘none’. Default [‘none’, ‘none’, ‘none’].
options.relativeTo — Align relative to these geometries’ bounding box instead of the inputs.
options.grouped — Move all inputs as a rigid group. Default false.
geometries — Two or more geometries to align.

Returns: The aligned geometries as an array.

const { cube, cylinder } = jscad.primitives
const { align } = jscad.transforms
async function main() {
return align({ modes: ['min', 'center', 'min'] }, cube({ size: 8 }), cylinder({ height: 12, radius: 2 }))
}

See also: