CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-jscad--modeling

Constructive Solid Geometry (CSG) Library for 2D and 3D geometries with boolean operations, transformations, and mathematical utilities

Pending
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

extrusions.mddocs/

Extrusions

All 2D shapes (primitives or the results of operations) can be extruded in various ways to create 3D geometry. In all cases, the functions return new results and never change the original shapes. Extrusions convert 2D geometries (geom2) into 3D geometries (geom3).

Capabilities

Linear Extrusion

Extrude 2D shapes linearly along the Z-axis to create 3D solids.

/**
 * Extrude 2D geometry linearly along the Z axis
 * @param {Object} options - Extrusion options
 * @param {Number} [options.height=1] - Height of the extrusion
 * @param {Number} [options.twistAngle=0] - Twist angle in radians along the height
 * @param {Number} [options.twistSteps=1] - Number of steps for twist (more steps = smoother twist)
 * @param {...Object} objects - 2D objects to extrude (geom2)
 * @returns {geom3|Array} Extruded 3D geometry
 */
function extrudeLinear(options?: {
  height?: number,
  twistAngle?: number,
  twistSteps?: number
}, ...objects: geom2[]): geom3 | geom3[];

Usage Examples:

const { circle, rectangle, extrudeLinear } = require('@jscad/modeling');

// Simple linear extrusion
const circle2D = circle({ radius: 5 });
const cylinder = extrudeLinear({ height: 10 }, circle2D);

// Extrusion with twist
const square2D = rectangle({ size: [6, 6] });
const twistedExtrusion = extrudeLinear({
  height: 20,
  twistAngle: Math.PI,
  twistSteps: 20
}, square2D);

Rotational Extrusion

Extrude 2D shapes by rotating them around the Y-axis to create rotationally symmetric 3D shapes.

/**
 * Extrude 2D geometry by rotating around the Y axis
 * @param {Object} options - Rotation extrusion options
 * @param {Number} [options.angle=Math.PI*2] - Angle of rotation in radians (default: full rotation)
 * @param {Number} [options.startAngle=0] - Starting angle in radians
 * @param {Number} [options.segments=12] - Number of segments for the rotation
 * @param {...Object} objects - 2D objects to extrude (geom2)
 * @returns {geom3|Array} Rotationally extruded 3D geometry
 */
function extrudeRotate(options?: {
  angle?: number,
  startAngle?: number,
  segments?: number
}, ...objects: geom2[]): geom3 | geom3[];

Usage Examples:

const { polygon, extrudeRotate } = require('@jscad/modeling');

// Create a bowl by rotating a profile
const profile = polygon({
  points: [[0, 0], [10, 0], [12, 2], [10, 8], [8, 10], [0, 10]]
});
const bowl = extrudeRotate({ segments: 32 }, profile);

// Create partial rotation (e.g., 3/4 of a circle)
const partialRotation = extrudeRotate({
  angle: Math.PI * 1.5,
  segments: 24
}, profile);

Rectangular Extrusion

Extrude 2D shapes in a rectangular pattern, creating multiple extruded copies arranged in a grid.

/**
 * Extrude 2D geometry in a rectangular pattern
 * @param {Object} options - Rectangular extrusion options
 * @param {Number} [options.height=1] - Height of each extrusion
 * @param {Array} [options.size=[1,1]] - Number of copies in [X,Y] directions
 * @param {Array} [options.spacing=[1,1]] - Spacing between copies in [X,Y] directions
 * @param {...Object} objects - 2D objects to extrude (geom2)
 * @returns {geom3|Array} Rectangular pattern of extruded geometries
 */
function extrudeRectangular(options?: {
  height?: number,
  size?: [number, number],
  spacing?: [number, number]
}, ...objects: geom2[]): geom3 | geom3[];

Usage Examples:

const { circle, extrudeRectangular } = require('@jscad/modeling');

// Create a grid of extruded circles
const smallCircle = circle({ radius: 2 });
const pillarsGrid = extrudeRectangular({
  height: 15,
  size: [4, 3],    // 4x3 grid
  spacing: [8, 8]   // 8 units apart
}, smallCircle);

Helical Extrusion

Extrude 2D shapes along a helical (spiral) path to create twisted, coiled geometries.

/**
 * Extrude 2D geometry along a helical path
 * @param {Object} options - Helical extrusion options
 * @param {Number} options.angle - Total rotation angle in radians
 * @param {Number} options.pitch - Vertical distance per rotation
 * @param {Number} [options.height] - Total height (alternative to pitch*rotations)
 * @param {Number} [options.segments=32] - Number of segments for the helix
 * @param {...Object} objects - 2D objects to extrude (geom2)
 * @returns {geom3|Array} Helically extruded 3D geometry
 */
function extrudeHelical(options: {
  angle: number,
  pitch: number,
  height?: number,
  segments?: number
}, ...objects: geom2[]): geom3 | geom3[];

Usage Examples:

const { rectangle, extrudeHelical } = require('@jscad/modeling');

// Create a helical spring profile
const springProfile = rectangle({ size: [1, 0.5] });
const spring = extrudeHelical({
  angle: Math.PI * 10,  // 5 full rotations
  pitch: 2,             // 2 units vertical per rotation
  segments: 100
}, springProfile);

// Create a twisted tower
const { polygon } = require('@jscad/modeling').primitives;
const towerProfile = polygon({
  points: [[0, 0], [2, 0], [1.5, 3], [0.5, 3]]
});
const twistedTower = extrudeHelical({
  angle: Math.PI * 4,
  pitch: 5,
  segments: 64
}, towerProfile);

Slice-Based Extrusion

Extrude using a series of 2D slices to create complex shapes with varying cross-sections.

/**
 * Extrude from a series of 2D slices
 * @param {Object} options - Slice extrusion options
 * @param {Function} [options.callback] - Function to transform each slice
 * @param {Number} [options.numberOfSlices=2] - Number of slices to create
 * @param {Boolean} [options.capStart=true] - Whether to cap the start
 * @param {Boolean} [options.capEnd=true] - Whether to cap the end
 * @param {Array} slices - Array of 2D geometries representing each slice
 * @returns {geom3} 3D geometry created from slices
 */
function extrudeFromSlices(options?: {
  callback?: (progress: number, index: number, base: geom2) => geom2,
  numberOfSlices?: number,
  capStart?: boolean,
  capEnd?: boolean
}, slices: geom2[]): geom3;

Usage Examples:

const { circle, rectangle, extrudeFromSlices, scale, translate } = require('@jscad/modeling');

// Create a shape that transitions from circle to rectangle
const bottomSlice = circle({ radius: 5 });
const topSlice = rectangle({ size: [8, 8] });

// Create intermediate slices
const slices = [];
for (let i = 0; i <= 10; i++) {
  const t = i / 10;
  const slice = translate([0, 0, i * 2], 
    scale([1 - t * 0.3, 1 - t * 0.3], bottomSlice)
  );
  slices.push(slice);
}
slices.push(translate([0, 0, 22], topSlice));

const morphedShape = extrudeFromSlices({}, slices);

Projection

Project 3D geometries onto 2D planes, essentially the reverse of extrusion.

/**
 * Project 3D geometry onto a 2D plane
 * @param {Object} options - Projection options
 * @param {Array} [options.axis=[0,0,1]] - Axis to project along
 * @param {Number} [options.origin=0] - Origin point on the axis
 * @param {...Object} objects - 3D objects to project
 * @returns {geom2|Array} Projected 2D geometry
 */
function project(options?: {
  axis?: [number, number, number],
  origin?: number
}, ...objects: geom3[]): geom2 | geom2[];

Usage Examples:

const { cube, sphere, project, union } = require('@jscad/modeling');

// Project a 3D shape to get its 2D footprint
const complex3D = union(
  cube({ size: 10 }),
  sphere({ radius: 8, center: [5, 5, 5] })
);
const footprint = project({}, complex3D);

// Project along different axis
const sideView = project({ axis: [1, 0, 0] }, complex3D);

Advanced Extrusion Techniques

Multi-Step Extrusions

Combine multiple extrusion operations for complex shapes:

const { circle, extrudeLinear, extrudeRotate } = require('@jscad/modeling');

// Create a complex bottle shape
const baseProfile = circle({ radius: 3 });
const cylinder1 = extrudeLinear({ height: 10 }, baseProfile);

const neckProfile = circle({ radius: 1 });
const neck = translate([0, 0, 10], extrudeLinear({ height: 5 }, neckProfile));

const bottle = union(cylinder1, neck);

Custom Slice Functions

Create complex shapes using mathematical functions for slice generation:

const { circle, extrudeFromSlices, scale, translate } = require('@jscad/modeling');

const createSlices = (baseShape, height, steps) => {
  const slices = [];
  
  for (let i = 0; i <= steps; i++) {
    const t = i / steps;
    const z = t * height;
    
    // Create scaling that varies with height
    const scaleX = 1 + 0.5 * Math.sin(t * Math.PI * 4);
    const scaleY = 1 + 0.3 * Math.cos(t * Math.PI * 6);
    
    const slice = translate([0, 0, z],
      scale([scaleX, scaleY], baseShape)
    );
    slices.push(slice);
  }
  
  return slices;
};

const base = circle({ radius: 3 });
const complexShape = extrudeFromSlices({}, createSlices(base, 20, 30));

Performance Considerations

  • Higher segment counts create smoother curves but increase computation time
  • Twist operations with many steps can be CPU-intensive
  • Consider the final application when choosing segment counts
  • For 3D printing, moderate segment counts often provide the best balance

Common Extrusion Patterns

const { rectangle, circle, extrudeLinear, extrudeRotate } = require('@jscad/modeling');

// Architectural column
const columnBase = circle({ radius: 5 });
const column = extrudeLinear({ height: 30 }, columnBase);

// Gear tooth profile
const toothProfile = polygon({
  points: [[0, 0], [1, 0], [1.2, 0.3], [1, 0.6], [0, 0.6]]
});
const gear = extrudeRotate({ segments: 24 }, toothProfile);

// Threaded rod
const threadProfile = polygon({
  points: [[2.8, -0.1], [3.2, -0.1], [3.2, 0.1], [2.8, 0.1]]
});
const threadedRod = extrudeHelical({
  angle: Math.PI * 20,
  pitch: 0.5,
  segments: 200
}, threadProfile);

docs

booleans.md

colors.md

curves.md

expansions-modifiers.md

extrusions.md

geometries.md

hulls.md

index.md

maths.md

measurements.md

primitives.md

text-utils.md

transforms.md

tile.json