meta data for this page
  •  

This is an old revision of the document!


Quick Reference

The following sections provide quick reference to the JSCAD application programming interface (API), the heart of making designs.

Primitive Shapes

The 'primitives' are accessed through the modeling API using the following:

const { cube, ellipse, star } = require('@jscad/modeling').primitives

2D Primitives

Primitive Notes
const mypath = arc({center: [2, 2], radius: 2, startAngle: Math.PI, endAngle: Math.PI * 2, segments: 64}) API
const myshape = circle({center: [6.5, 6.5], radius: 3.5, startAngle: Math.PI / 2, endAngle: Math.PI, segments: 64}) User Guide API
const myshape = ellipse({center: [6.5, 6.5], radius: [7, 9], startAngle: Math.PI / 2, endAngle: Math.PI, segments: 64}) API
const mypath = line([ [10, 10], [-10, 10] ]) API
const myshape = polygon({ points: [ [10, 11], [0, 11], [5, 20] ], paths: [0, 1, 2]}) API
const myshape = rectangle({center: [6.5, 6.5], size: [10, 20]}) API
const myshape = roundedRectangle({center: [6.5, 6.5], size: [10, 20], roundRadius: 2, segments: 64}) API
const myshape = square({center: [6.5, 6.5], size: 10}) API
const myshape = star({vertices: 8, outerRadius: 10}) /* star with 8/2 density */ API
const myshape = star({vertices: 12, outerRadius: 40, innerRadius: 20}) /* star with given radius */ API
const myshape = triangle({type: 'AAS', values: [degToRad(62), degToRad(35), 7]}) API

3D Primitives

Primitive Notes
const myshape = cube({center: [6.5, 6.5, 6.5], size: 7}) API
const myshape = cuboid({center: [6.5, 6.5, 6.5], size: [3, 5, 7]}) API
const myshape = cylinder({center: [-5, -5, -5], height: 10, radius: 4, segments: 5}) API
const myshape = cylinderElliptic({center: [-5, -5, -5], height: 10, startRadius: [1, 2], endRadius: [2, 1], startAngle: Math.PI / 2, endAngle: Math.PI * 2 * 0.75, segments: 5}) API
const myshape = ellipsoid({center: [-5, -5, -5], radius: [4, 6, 8], segments: 64}) API
const myshape = geodesicSphere({radius: 15, frequency: 18}) API
const myshape = polyhedron({points: mypoints, faces: myfaces, orientation: 'inward'}) API
const myshape = roundedCuboid({center: [-5, -5, -5], size: [10, 20, 10], roundRadius: 2, segments: 16}) API
const myshape = roundedCylinder({center: [-5, -5, -5], height: 10, radius: 2, roundRadius: 0.5, segments: 16}) API
const myshape = sphere({center: [-5, -5, -5], radius: 5, }) API
const myshape = torus({innerRadius: 10, outerRadius: 100, innerSegments: 32, outerSegments: 8, innerRotation: Math.PI, startAngle: Math.PI}) API

Shape Transformations

The 'transforms' are accessed through the modeling API using the following:

const { translate, scale, rotateX } = require('@jscad/modeling').transforms
Transform Notes
const newshape = align({modes: ['min', 'center', 'none'], relativeTo: [10, null, 10], grouped: true}, oldshape) API
const newshape = center({axes: [true, true, false], center: [15, 10, 0]}, oldshape) API
const newshape = centerX(oldshape) API
const newshape = centerY(oldshape) API
const newshape = centerZ(oldshape) API
const newshape = mirror({origin: [5, 5, 5], normal: [0, 0, 10]}, oldshape)) API
const newshape = mirrorX(oldshape) API
const newshape = mirrorY(oldshape) API
const newshape = mirrorZ(oldshape) API
const newshape = rotate([Math.PI / 4, 0, 0], oldshape) API
const newshape = rotateX(oldshape) API
const newshape = rotateY(oldshape) API
const newshape = rotateZ(oldshape) API
const newshape = scale([5, 0, 10], oldshape) API
const newshape = scaleX(5, oldshape) API
const newshape = scaleY(0.5, oldshape) API
const newshape = scaleZ(5, oldshape) API
const newshape = transform(mat4.rotateX(Math.PI/4), oldshape) API
const newshape = translate([5, 0, 10], oldshape) API
const newshape = translateX(5, oldshape) API
const newshape = translateY(0.5, oldshape) API
const newshape = translateZ(5, oldshape) API

Shape Operations

Boolean Operations

The 'booleans' are accessed through the modeling API using the following:

const { union, intersect, scission, subtract } = require('@jscad/modeling').booleans
Operation Notes
const newshape = intersect(oldshape0, oldshape1) API
const newshape = subtract(oldshape0, oldshape1) API
const newshape = union(oldshape0, oldshape1) API
const newshapes = scission(bigshape) API

Hull Operations

The 'hulls' are accessed through the modeling API using the following:

const { hull, hullChain } = require('@jscad/modeling').hulls
Operation Notes
const newshape = hull(oldshape0, oldshape1) API
const newshape = hullChain(oldshape0, oldshape1) API

Extrusions

The 'extrusions' are accessed through the modeling API using the following:

const { extrudeLinear, extrudeRegtangular, extrudeRotate, project  } = require('@jscad/modeling').extrusions
Extrusion of 2D Shapes Notes
const newshape = extrudeLinear({height: 20, twistAngle: Math.PI, twistSteps: 20}, oldshape)) API
const newshape = extrudeRectangular({size: 3, height: 15}, oldshape) API
const newshape = extrudeRotate({startAngle: Math.PI, angle: Math.PI / 2, overflow: 'cap', segments: 64}, oldshape) API
const newshape = project({axis: [0, 0, 1], origin: [0, 0, 0]}, oldshape) API

OpenSCAD-like Functions

These functions ease the transition from the OpenSCAD (proprietary script) to OpenJSCAD (JavaScript and objects).

2D Solid
var 2Dshape = circle(radius);
var 2Dshape = circle(d); /* diameter */
var 2Dshape = square(size,center);
var 2Dshape = square([width,height],center);
var 2Dshape = polygon([points]);
var 2Dshape = polygon([points],[paths]);

Note: The OpenSCAD text() function is not supported.

3D Solid
var 3Dshape = torus(); /* ri = 1, ro = 4, fni: 16, fno: 32, roti: 0 */
var 3Dshape = torus({ ri: 1.5, ro: 3, fni: 4 });
var 3Dshape = torus({ ri: 1.5, ro: 3, fni:4, fno:4, roti:45 });

Text

var path = vector_char(10,-10, 'A'); /* X axis, Y axis, character */
var array_of_paths = vector_text(10,-10, 'Letters'); /* X axis, Y axis, string of characters */

Design Parameters

A design can have interactive parameters by declaring a special function; getParameterDefinitions().

In applications and browsers, these parameters are presented to users, allowing users to change designs.

Usage

This function must return an array of parameter definitions, as show below.

const getParameterDefinitions = () => {
    return [
        { name: 'length', type: 'int', initial: 150, caption: 'Length?' }, 
        { name: 'width', type: 'int', initial: 100, caption: 'Width?' },
    ]
}

The parameters are evaluated and values are passed into the main function. Be sure to declare the main function properly.

const main = (params) => {
  var l = params.length
  var w = params.width
...
}

Parameter Types

The parameters are defined as input fields on a single HTML5 form, i.e. the list of parameters. For more information on HTML5 input fields, see some examples at W3 Schools.

Note: Browsers are NOT the same and will treat unsupported parameter types as TEXT.

Type Example Returned Value
checkbox {name: 'bigorsmall', type: 'checkbox', checked: true, caption: 'Big?'} if checked true, else false
checkbox {name: 'bigorsmall', type: 'checkbox', checked: true, initial: 20, caption: 'Big?'} if checked 20, else false
color { name: 'color', type: 'color', initial: '#FFB431', caption: 'Color?' } “#rrggbb”, use html2rgb() to convert
date {name: 'birthday', type: 'date', caption: 'Birthday?'} “YYYY-MM-DD”
email {name: 'address', type: 'email', caption: 'Email Address?'} string value
float {name: 'angle', type: 'number', initial: 2.5, step: 0.5, caption: 'Angle?'} float value
int {name: 'age', type: 'int', initial: 20, caption: 'Age?'} integer value
number {name: 'angle', type: 'number', initial: 2.5, step: 0.5, caption: 'Angle?'} float value
password {name: 'password', type: 'password', caption: 'Secret?'} string value
slider {name: 'count', type: 'slider', min: 2, max: 10, caption: 'How many?'} float value
text {name: 'name', type: 'text', caption: 'Name?'} string value
url {name: 'webpage', type: 'url', caption: 'Web page URL?'} string value
group { name: 'balloon', type: 'group', caption: 'Balloons' } none, only displayed

The parameters accept additional restrictions and assistance. These include 'initial', 'max', 'maxLength', 'min', 'pattern', 'placeholder', 'size', and 'step'.

There is one more special parameter type called 'choice', which defines a list of choices for selection.

const getParameterDefinitions = () => {
  return [
    { name: 'rounded', type: 'choice', caption: 'Rounded edges', values: [0, 1], captions: ['No', 'Yes (slow!)'], initial: 0 }
  ]
}

The list of captions are those shown as a pull down list. The list of values define a value for each caption. And, the chosen value is passed into the main function.