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

primitives.mddocs/

Primitive Shapes

Primitives provide the building blocks for complex parts. Each primitive is a geometrical object that can be described mathematically, and therefore precise. Primitives can be logically combined, transformed, extruded, etc.

Capabilities

2D Primitives

Arc

Creates a 2D arc segment defined by radius, start angle, and end angle.

/**
 * Create a 2D arc segment
 * @param {Object} options - Arc configuration
 * @param {Array} [options.center=[0,0]] - Center point of the arc
 * @param {Number} [options.radius=1] - Radius of the arc
 * @param {Number} [options.startAngle=0] - Starting angle in radians
 * @param {Number} [options.endAngle=TAU] - Ending angle in radians (default: 2π)
 * @param {Number} [options.segments=32] - Number of segments for arc approximation
 * @param {Boolean} [options.makeTangent=false] - Make arc tangent at start/end
 * @returns {path2} 2D path representing the arc
 */
function arc(options?: {
  center?: [number, number],
  radius?: number,
  startAngle?: number,
  endAngle?: number,
  segments?: number,
  makeTangent?: boolean
}): path2;

Circle

Creates a 2D circle with specified center and radius.

/**
 * Create a 2D circle
 * @param {Object} options - Circle configuration  
 * @param {Array} [options.center=[0,0]] - Center point of the circle
 * @param {Number} [options.radius=1] - Radius of the circle
 * @param {Number} [options.segments=32] - Number of segments for circle approximation
 * @param {Number} [options.startAngle=0] - Starting angle in radians
 * @param {Number} [options.endAngle=TAU] - Ending angle in radians (default: 2π)
 * @returns {geom2} 2D geometry representing the circle
 */
function circle(options?: {
  center?: [number, number],
  radius?: number,
  segments?: number,
  startAngle?: number,
  endAngle?: number
}): geom2;

Ellipse

Creates a 2D ellipse with configurable radii and rotation.

/**
 * Create a 2D ellipse
 * @param {Object} options - Ellipse configuration
 * @param {Array} [options.center=[0,0]] - Center point of the ellipse
 * @param {Array} [options.radius=[1,1]] - X and Y radii of the ellipse  
 * @param {Number} [options.segments=32] - Number of segments for ellipse approximation
 * @param {Number} [options.startAngle=0] - Starting angle in radians
 * @param {Number} [options.endAngle=Math.PI*2] - Ending angle in radians
 * @returns {geom2} 2D geometry representing the ellipse
 */
function ellipse(options?: {
  center?: [number, number],
  radius?: [number, number],
  segments?: number,
  startAngle?: number,
  endAngle?: number
}): geom2;

Rectangle

Creates a 2D rectangle with specified dimensions and center.

/**
 * Create a 2D rectangle
 * @param {Object} options - Rectangle configuration
 * @param {Array} [options.center=[0,0]] - Center point of the rectangle
 * @param {Array} [options.size=[2,2]] - Width and height of the rectangle
 * @returns {geom2} 2D geometry representing the rectangle
 */
function rectangle(options?: {
  center?: [number, number],
  size?: [number, number]
}): geom2;

Square

Creates a 2D square with specified size and center.

/**
 * Create a 2D square
 * @param {Object} options - Square configuration
 * @param {Array} [options.center=[0,0]] - Center point of the square
 * @param {Number} [options.size=2] - Side length of the square
 * @returns {geom2} 2D geometry representing the square
 */
function square(options?: {
  center?: [number, number],
  size?: number
}): geom2;

Rounded Rectangle

Creates a 2D rectangle with rounded corners.

/**
 * Create a 2D rounded rectangle
 * @param {Object} options - Rounded rectangle configuration
 * @param {Array} [options.center=[0,0]] - Center point of the rectangle
 * @param {Array} [options.size=[2,2]] - Width and height of the rectangle
 * @param {Number} [options.roundRadius=0.2] - Radius of the corner rounds
 * @param {Number} [options.segments=16] - Number of segments for rounded corners
 * @returns {geom2} 2D geometry representing the rounded rectangle
 */
function roundedRectangle(options?: {
  center?: [number, number],
  size?: [number, number],
  roundRadius?: number,
  segments?: number
}): geom2;

Polygons and Lines

Polygon

Creates a 2D polygon from an array of points.

/**
 * Create a 2D polygon from points with optional paths and orientation
 * @param {Object} options - Polygon configuration
 * @param {Array} options.points - Array of [x,y] points or nested arrays defining the polygon
 * @param {Array} [options.paths=[]] - Array of point indices or nested arrays of point indices
 * @param {String} [options.orientation='counterclockwise'] - Orientation of points ('counterclockwise' or 'clockwise')
 * @returns {geom2} 2D geometry representing the polygon
 */
function polygon(options: {
  points: Array<[number, number]> | Array<Array<[number, number]>>,
  paths?: Array<number> | Array<Array<number>>,
  orientation?: string
}): geom2;

Star

Creates a 2D star shape with configurable inner and outer radii.

/**
 * Create a 2D star shape with configurable density
 * @param {Object} options - Star configuration
 * @param {Array} [options.center=[0,0]] - Center point of the star
 * @param {Number} [options.vertices=5] - Number of star vertices
 * @param {Number} [options.density=2] - Density (Q) of star for calculating inner radius
 * @param {Number} [options.outerRadius=1] - Outer radius of the star
 * @param {Number} [options.innerRadius=0] - Inner radius of the star (0 to calculate from density)
 * @param {Number} [options.startAngle=0] - Starting angle rotation in radians
 * @returns {geom2} 2D geometry representing the star
 */
function star(options?: {
  center?: [number, number],
  vertices?: number,
  density?: number,
  outerRadius?: number,
  innerRadius?: number,
  startAngle?: number
}): geom2;

Triangle

Creates a 2D triangle from three points.

/**
 * Construct a triangle in two dimensional space using mathematical relationships
 * @param {Object} [options] - Triangle configuration
 * @param {String} [options.type='SSS'] - Type of triangle to construct (AAA, AAS, ASA, SAS, SSA, SSS)
 *   - A = angle (in radians), S = side length
 * @param {Array} [options.values=[1,1,1]] - Array of 3 values (angles in radians or side lengths)
 * @returns {geom2} 2D geometry representing the triangle
 */
function triangle(options?: {
  type?: string,
  values?: [number, number, number]
}): geom2;

Line

Creates a 2D line (path) from an array of points.

/**
 * Create a 2D line from points
 * @param {Array} points - Array of [x,y] points defining the line path
 * @returns {path2} 2D path geometry representing the line
 */
function line(points: Array<[number, number]>): path2;

3D Primitives

Cube

Creates a 3D cube with equal sides.

/**
 * Create a 3D cube with equal sides
 * @param {Object} options - Cube configuration
 * @param {Array} [options.center=[0,0,0]] - Center point of the cube
 * @param {Number} [options.size=2] - Side length of the cube
 * @returns {geom3} 3D geometry representing the cube
 */
function cube(options?: {
  center?: [number, number, number],
  size?: number
}): geom3;

Cuboid

Creates a 3D rectangular box with configurable dimensions.

/**
 * Create a 3D rectangular box
 * @param {Object} options - Cuboid configuration
 * @param {Array} [options.center=[0,0,0]] - Center point of the cuboid
 * @param {Array} [options.size=[2,2,2]] - Width, depth, and height dimensions
 * @returns {geom3} 3D geometry representing the cuboid
 */
function cuboid(options?: {
  center?: [number, number, number],
  size?: [number, number, number]
}): geom3;

Rounded Cuboid

Creates a 3D rectangular box with rounded edges.

/**
 * Create a 3D rounded rectangular box
 * @param {Object} options - Rounded cuboid configuration
 * @param {Array} [options.center=[0,0,0]] - Center point of the cuboid
 * @param {Array} [options.size=[2,2,2]] - Width, depth, and height dimensions
 * @param {Number} [options.roundRadius=0.2] - Radius of the edge rounds
 * @param {Number} [options.segments=16] - Number of segments for rounded edges
 * @returns {geom3} 3D geometry representing the rounded cuboid
 */
function roundedCuboid(options?: {
  center?: [number, number, number],
  size?: [number, number, number],
  roundRadius?: number,
  segments?: number
}): geom3;

Sphere

Creates a 3D sphere with specified radius and center.

/**
 * Create a 3D sphere
 * @param {Object} options - Sphere configuration
 * @param {Array} [options.center=[0,0,0]] - Center point of the sphere
 * @param {Number} [options.radius=1] - Radius of the sphere
 * @param {Number} [options.segments=32] - Number of segments for sphere tessellation
 * @returns {geom3} 3D geometry representing the sphere
 */
function sphere(options?: {
  center?: [number, number, number],
  radius?: number,
  segments?: number
}): geom3;

Ellipsoid

Creates a 3D ellipsoid with configurable radii for each axis.

/**
 * Create a 3D ellipsoid
 * @param {Object} options - Ellipsoid configuration
 * @param {Array} [options.center=[0,0,0]] - Center point of the ellipsoid
 * @param {Array} [options.radius=[1,1,1]] - X, Y, and Z radii of the ellipsoid
 * @param {Number} [options.segments=32] - Number of segments for ellipsoid tessellation
 * @returns {geom3} 3D geometry representing the ellipsoid
 */
function ellipsoid(options?: {
  center?: [number, number, number],
  radius?: [number, number, number],
  segments?: number
}): geom3;

Cylinder

Creates a 3D cylinder with specified height and radius.

/**
 * Create a 3D cylinder
 * @param {Object} options - Cylinder configuration
 * @param {Array} [options.center=[0,0,0]] - Center point of the cylinder
 * @param {Number} [options.height=2] - Height of the cylinder
 * @param {Number} [options.radius=1] - Radius of the cylinder
 * @param {Number} [options.segments=32] - Number of segments for cylinder circumference
 * @returns {geom3} 3D geometry representing the cylinder
 */
function cylinder(options?: {
  center?: [number, number, number],
  height?: number,
  radius?: number,
  segments?: number
}): geom3;

Elliptic Cylinder

Creates a 3D cylinder with elliptical cross-section.

/**
 * Create a 3D elliptic cylinder
 * @param {Object} options - Elliptic cylinder configuration
 * @param {Array} [options.center=[0,0,0]] - Center point of the cylinder
 * @param {Number} [options.height=2] - Height of the cylinder
 * @param {Array} [options.radius=[1,1]] - X and Y radii of the elliptical base
 * @param {Number} [options.segments=32] - Number of segments for cylinder circumference
 * @returns {geom3} 3D geometry representing the elliptic cylinder
 */
function cylinderElliptic(options?: {
  center?: [number, number, number],
  height?: number,
  radius?: [number, number],
  segments?: number
}): geom3;

Rounded Cylinder

Creates a 3D cylinder with rounded top and bottom edges.

/**
 * Create a 3D rounded cylinder
 * @param {Object} options - Rounded cylinder configuration
 * @param {Array} [options.center=[0,0,0]] - Center point of the cylinder
 * @param {Number} [options.height=2] - Height of the cylinder
 * @param {Number} [options.radius=1] - Radius of the cylinder
 * @param {Number} [options.roundRadius=0.2] - Radius of the edge rounds
 * @param {Number} [options.segments=32] - Number of segments for cylinder circumference
 * @returns {geom3} 3D geometry representing the rounded cylinder
 */
function roundedCylinder(options?: {
  center?: [number, number, number],
  height?: number,
  radius?: number,
  roundRadius?: number,
  segments?: number
}): geom3;

Torus

Creates a 3D torus (donut shape) with major and minor radii.

/**
 * Create a 3D torus
 * @param {Object} options - Torus configuration
 * @param {Array} [options.center=[0,0,0]] - Center point of the torus
 * @param {Number} [options.innerRadius=1] - Inner radius (minor radius)
 * @param {Number} [options.outerRadius=4] - Outer radius (major radius)
 * @param {Number} [options.innerSegments=32] - Number of segments for the inner circumference
 * @param {Number} [options.outerSegments=32] - Number of segments for the outer circumference
 * @param {Number} [options.innerRotation=0] - Rotation of the inner circumference
 * @param {Number} [options.startAngle=0] - Starting angle for partial torus
 * @param {Number} [options.outerRotation=Math.PI*2] - Ending angle for partial torus
 * @returns {geom3} 3D geometry representing the torus
 */
function torus(options?: {
  center?: [number, number, number],
  innerRadius?: number,
  outerRadius?: number,
  innerSegments?: number,
  outerSegments?: number,
  innerRotation?: number,
  startAngle?: number,
  outerRotation?: number
}): geom3;

Advanced 3D Primitives

Geodesic Sphere

Creates a geodesic sphere with triangular faces for more uniform tessellation.

/**
 * Create a geodesic sphere with triangular tessellation
 * @param {Object} options - Geodesic sphere configuration
 * @param {Array} [options.center=[0,0,0]] - Center point of the sphere
 * @param {Number} [options.radius=1] - Radius of the sphere
 * @param {Number} [options.frequency=6] - Frequency of subdivision (higher = more detailed)
 * @returns {geom3} 3D geometry representing the geodesic sphere
 */
function geodesicSphere(options?: {
  center?: [number, number, number],
  radius?: number,
  frequency?: number
}): geom3;

Polyhedron

Creates a custom 3D polyhedron from face definitions.

/**
 * Create a custom 3D polyhedron from vertices and faces
 * @param {Object} options - Polyhedron configuration
 * @param {Array} options.points - Array of [x,y,z] vertex coordinates
 * @param {Array} options.faces - Array of face definitions (indices into points array)
 * @param {Array} [options.colors] - Optional array of RGBA colors per face
 * @returns {geom3} 3D geometry representing the polyhedron
 */
function polyhedron(options: {
  points: Array<[number, number, number]>,
  faces: Array<number[]>,
  colors?: Array<[number, number, number, number]>
}): geom3;

Usage Examples

Creating basic shapes:

const { arc, circle, cube, sphere } = require('@jscad/modeling').primitives;

// 2D shapes
const myCircle = circle({ radius: 5, segments: 64 });
const myArc = arc({ radius: 10, startAngle: 0, endAngle: Math.PI/2 });

// 3D shapes  
const myCube = cube({ size: 10, center: [0, 0, 5] });
const mySphere = sphere({ radius: 8, center: [10, 0, 0] });

Creating complex shapes:

const { star, torus, geodesicSphere } = require('@jscad/modeling').primitives;

// Star with 8 points
const star8 = star({ 
  vertices: 8, 
  outerRadius: 10, 
  innerRadius: 5 
});

// Detailed torus
const detailedTorus = torus({
  innerRadius: 2,
  outerRadius: 8,
  innerSegments: 64,
  outerSegments: 128
});

// High-detail geodesic sphere
const geodesic = geodesicSphere({ 
  radius: 5, 
  frequency: 12 
});

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