Create dynamic, parametric designs that can be easily customized through user-friendly controls.
Parameters allow you to create flexible designs that can be modified without changing code. STL.Show automatically generates user interface controls based on your parameter definitions, making it easy for anyone to customize your designs.
Automatic UI generation for easy customization
See changes instantly as you adjust parameters
Parameter values are saved with your project
Parameters are defined using the getParameterDefinitions() function and accessed in your main() function.
const { cube, sphere } = require('@jscad/modeling').primitives
// Define parameters
function getParameterDefinitions() {
return [
{name: 'size', type: 'number', initial: 10, caption: 'Size'},
{name: 'segments', type: 'number', initial: 16, caption: 'Segments'}
]
}
// Use parameters in main function
function main(params) {
return cube({size: params.size})
}
module.exports = { main, getParameterDefinitions }name - Unique identifier for the parametertype - Data type (number, choice, text, etc.)initial - Default valuecaption - Display label in UImain(params)params.parameterNameJSCAD supports various parameter types, each with different UI controls and validation options.
Numeric values with optional constraints and step increments.
{name: 'size', type: 'number', initial: 10, caption: 'Size'}{
name: 'radius',
type: 'number',
initial: 5,
min: 1,
max: 20,
step: 0.5,
caption: 'Radius'
}Features: Min/max constraints, step increments, real-time validation
Dropdown selection from predefined options.
{
name: 'shape',
type: 'choice',
values: ['cube', 'sphere', 'cylinder'],
captions: ['Cube', 'Sphere', 'Cylinder'],
initial: 'cube',
caption: 'Shape Type'
}{
name: 'quality',
type: 'choice',
values: ['low', 'medium', 'high'],
initial: 'medium',
caption: 'Quality'
}Features: Dropdown selection, custom captions, value mapping
String input for text-based parameters.
{
name: 'label',
type: 'text',
initial: 'My Design',
caption: 'Design Label'
}{
name: 'filename',
type: 'text',
initial: 'design',
maxLength: 20,
caption: 'File Name'
}Features: Text input, length validation, placeholder text
Boolean true/false values for enabling/disabling features.
{
name: 'showHoles',
type: 'checkbox',
checked: true,
caption: 'Show Holes'
}// In main function
if (params.showHoles) {
// Add holes to design
}Features: Boolean values, conditional logic, feature toggles
Color picker for applying colors to geometry.
{
name: 'objectColor',
type: 'color',
initial: '#ff0000',
caption: 'Object Color'
}// In main function
return color(params.objectColor, cube({size: 10}))Features: Color picker, hex values, visual preview
Advanced techniques for creating sophisticated parametric designs.
Organize parameters into logical groups for better UI organization.
function getParameterDefinitions() {
return [
// Basic parameters
{name: 'size', type: 'number', initial: 10, caption: 'Size'},
// Group: Hole settings
{name: 'holeDiameter', type: 'number', initial: 2, caption: 'Hole Diameter'},
{name: 'holeDepth', type: 'number', initial: 5, caption: 'Hole Depth'},
// Group: Material settings
{name: 'material', type: 'choice', values: ['plastic', 'metal'], initial: 'plastic', caption: 'Material'},
{name: 'thickness', type: 'number', initial: 1, caption: 'Wall Thickness'}
]
}Tip: Use comments to visually group related parameters in your code.
Show/hide parameters based on other parameter values.
function getParameterDefinitions() {
return [
{name: 'shape', type: 'choice', values: ['cube', 'sphere'], initial: 'cube', caption: 'Shape'},
{name: 'cubeSize', type: 'number', initial: 10, caption: 'Cube Size'},
{name: 'sphereRadius', type: 'number', initial: 5, caption: 'Sphere Radius'}
]
}function main(params) {
if (params.shape === 'cube') {
return cube({size: params.cubeSize})
} else {
return sphere({radius: params.sphereRadius})
}
}Note: STL.Show will show/hide parameter controls based on conditional logic.
Ensure parameter values are within acceptable ranges and relationships.
function main(params) {
// Validate parameters
if (params.innerRadius >= params.outerRadius) {
throw new Error('Inner radius must be smaller than outer radius')
}
if (params.height <= 0) {
throw new Error('Height must be positive')
}
// Create geometry
return cylinder({
radius: params.outerRadius,
height: params.height
})
}Tip: Use validation to prevent invalid geometry and provide helpful error messages.
A comprehensive example showing various parameter types and advanced features.
const { cube, sphere, cylinder } = require('@jscad/modeling').primitives
const { translate, rotate } = require('@jscad/modeling').transforms
const { union, subtract } = require('@jscad/modeling').booleans
const { color } = require('@jscad/modeling').colors
function getParameterDefinitions() {
return [
// Basic settings
{name: 'objectType', type: 'choice', values: ['cube', 'sphere', 'cylinder'],
captions: ['Cube', 'Sphere', 'Cylinder'], initial: 'cube', caption: 'Object Type'},
// Size parameters
{name: 'size', type: 'number', initial: 10, min: 1, max: 50, step: 1, caption: 'Size'},
{name: 'height', type: 'number', initial: 10, min: 1, max: 50, step: 1, caption: 'Height'},
// Hole settings
{name: 'hasHole', type: 'checkbox', checked: true, caption: 'Add Hole'},
{name: 'holeDiameter', type: 'number', initial: 2, min: 0.5, max: 8, step: 0.5, caption: 'Hole Diameter'},
{name: 'holeDepth', type: 'number', initial: 5, min: 1, max: 20, step: 1, caption: 'Hole Depth'},
// Appearance
{name: 'objectColor', type: 'color', initial: '#4f46e5', caption: 'Object Color'},
{name: 'quality', type: 'choice', values: ['low', 'medium', 'high'],
captions: ['Low (8 segments)', 'Medium (16 segments)', 'High (32 segments)'],
initial: 'medium', caption: 'Quality'},
// Text label
{name: 'label', type: 'text', initial: 'My Design', maxLength: 20, caption: 'Label'}
]
}
function main(params) {
// Determine segments based on quality
const segments = params.quality === 'low' ? 8 : params.quality === 'medium' ? 16 : 32
// Create base object
let baseObject
switch (params.objectType) {
case 'cube':
baseObject = cube({size: params.size})
break
case 'sphere':
baseObject = sphere({radius: params.size / 2, segments})
break
case 'cylinder':
baseObject = cylinder({radius: params.size / 2, height: params.height, segments})
break
}
// Add hole if requested
if (params.hasHole) {
const hole = cylinder({
radius: params.holeDiameter / 2,
height: params.holeDepth,
segments
})
baseObject = subtract(baseObject, hole)
}
// Apply color
const coloredObject = color(params.objectColor, baseObject)
return coloredObject
}
module.exports = { main, getParameterDefinitions }Define parameters with types and constraints
Use parameters in geometry creation
Apply conditional logic and validation
Guidelines for creating effective and user-friendly parametric designs.
Now that you understand parameters, explore these advanced topics: