Skip to content

ParametricSpringNode

ParametricSpringNode (from scenery-phet) draws a coiled spring using a parametric curve (a variation on a prolate cycloid), rendered as two separate front/back Paths so the coil reads with a pseudo-3D over/under weave. Every shape parameter — loop count, radius, aspect ratio, phase, line width — is exposed as its own NumberProperty, so you can animate the spring (e.g. stretching it as a mass drags down on it) just by setting those Properties; the Node recomputes its shape automatically.

The origin (0, 0) of a ParametricSpringNode is at its left-center, not its bounds' top-left — useful to know when positioning it next to an anchor point.

ts
import { ParametricSpringNode } from 'scenerystack/scenery-phet';

A minimal example

ts
const springNode = new ParametricSpringNode( {
  loops: 8,
  radius: 15,
  aspectRatio: 3,
  frontColor: 'lightBlue',
  middleColor: 'blue',
  backColor: 'darkblue',
  left: 100,
  centerY: 200
} );

// Stretch the spring by changing the loop radius live:
springNode.radiusProperty.value = 20;

Constructor

ts
new ParametricSpringNode( providedOptions?: ParametricSpringNodeOptions )

Options

OptionDefaultEffect
loops10Number of coil loops (seeds loopsProperty)
pointsPerLoop40Points used to approximate one loop (seeds pointsPerLoopProperty); higher is smoother but costlier
radius10Radius of a loop at aspect ratio 1:1 (seeds radiusProperty)
aspectRatio4y:x aspect ratio of each loop, i.e. how "squashed" the coil looks (seeds aspectRatioProperty)
phaseMath.PIPhase angle where the loop starts, in (0, 2π) (seeds phaseProperty)
deltaPhaseMath.PI / 2Controls how much the coil "leans," a Lissajous-style parameter (seeds deltaPhaseProperty)
xScale2.5Multiplier stretching the coil horizontally as it progresses (seeds xScaleProperty)
lineWidth3Stroke width of both the front and back paths (seeds lineWidthProperty)
leftEndLength / rightEndLength15 / 25Length of the straight horizontal segments added at the left/right ends of the coil
frontColor / middleColor / backColor'lightGray' / 'gray' / 'black'Colors used to build the front and back paths' gradients (middleColor is the dominant color)
boundsMethod'accurate'Forwarded to the internal Paths; set to 'none' for a large performance win if you only rely on x/y (not bounds-based layout) for positioning

Public API

MemberDescription
loopsProperty, radiusProperty, aspectRatioProperty, pointsPerLoopProperty, lineWidthProperty, phaseProperty, deltaPhaseProperty, xScalePropertyPublic NumberProperty for each shape parameter above; set any of them to reshape the spring live
reset()Resets every one of the Properties above to its initial value

Changing loops, pointsPerLoop, aspectRatio, phase, or deltaPhase rebuilds the point array; changing radius/xScale alone is cheaper

The first group of Properties changes how many points exist or how they're allocated to the front/back paths, so ParametricSpringNode regenerates the coil's Vector2 points from scratch. radiusProperty and xScaleProperty alone only need to mutate the existing points in place — a meaningfully cheaper path internally. If you're animating a spring every frame, prefer driving radiusProperty (e.g. to simulate stretch) over the other parameters.