Overview
SWCross is a four-armed plus-sign (crosshair) shape that
extends SWWheel. It hard-wires three design
constraints — always 4 spokes, no visible rim, no visible hub —
and adds an initialRotation property that pre-tilts the arms
before animation begins.
All of SWWheel’s animation and drawing machinery
(drawOnGrid(), breathe(), all setters) is
inherited unchanged. SWCross only overrides
rotate() and reset() to bake in the
initial-rotation offset.
SWCross defaults to SQUARE (flat-tip) cap style,
overriding p5.js’s global default of ROUND.
Flat tips make the crosshair geometry precise and clean.
See the Cap Styles section for a full explanation.
Inheritance
SWCross uses single inheritance via
the extends keyword. The constructor calls
super() with three locked values, then adds its own
initialRotation state.
| Category | Item | How SWCross handles it |
|---|---|---|
| Inherited (unchanged) | drawOnGrid(grid), breathe(sinusoid, t) |
Used exactly as defined in SWWheel |
| Inherited (unchanged) | All setters: setSpokeThickness(),
setSpokeColor(),
setSpokeCapStyle(), etc. |
Available on every SWCross instance |
| Overridden | rotate(degPerSec, t) |
Sets rotationAngle = initialRotation + degPerSec × t
instead of just degPerSec × t
|
| Overridden | reset() |
Returns arms to initialRotation
(not 0°) and restores originalOuterRadius
|
| New | setInitialRotation(deg) |
Live setter that immediately repositions arms to the new base angle |
| Locked (not exposed) | numSpokes, rimThickness, hub colors |
Hard-wired in super(); not constructor options |
Dependencies
All six must be loaded in your HTML before
SWCross.js:
| Class / Library | File | Role |
|---|---|---|
p5.js |
CDN | Canvas and rendering engine |
SWColor |
shapeClasses/swColor.js |
HSB color objects for arm stroke color |
SWPoint |
shapeClasses/swPoint.js |
Center point and computed arm endpoints |
SWLine |
shapeClasses/swLine.js |
Each individual arm (spoke) |
SWDisk |
shapeClasses/swDisk.js |
Hub and rim components (invisible in SWCross) |
SWGrid |
shapeClasses/swGrid.js |
Required only for drawOnGrid() |
SWWheel |
shapeClasses/swWheel.js |
Parent class — must load immediately before SWCross.js |
Constructor
All options are passed as a single destructured object.
numSpokes, rimThickness, and hub colors
are not accepted as options — they are locked:
| Option | Type | Default | Description |
|---|---|---|---|
center |
SWPoint | SWPoint(0, 0) | Shape center in user/grid coordinates |
outerRadius |
number | 100 | Arm length in user units |
hubRadius |
number | 6 | Hub size in user units (hub is invisible; value kept for internal mechanics) |
spokeThickness |
number | 25 | Arm stroke weight in pixels |
spokeColor |
SWColor | undefined | Arm stroke color (SWColor instance) |
initialRotation |
number | 0 | Pre-tilt angle in degrees. 0° = plus ✚, 45° = times ✕ |
| Locked value | Forced to | Why |
|---|---|---|
numSpokes |
4 | Defines the plus-sign shape; other counts produce different shapes |
rimThickness |
0 | Arms only — no enclosing ring |
hubFillColor |
null | Hub fill invisible |
rimColor |
null | Hub outline invisible |
| arm cap style | 'SQUARE' | Flat tips; set via setSpokeCapStyle() after super() |
Public Properties
All properties listed for SWWheel
are present. The following are specific to SWCross:
| Property | Type | Notes |
|---|---|---|
initialRotation |
number | Pre-tilt offset in degrees. Arms rest here at t = 0. Set via setInitialRotation(). |
rotationAngle stores the current computed angle
(initialRotation + degPerSec × t).
originalOuterRadius is what reset() restores
currentRadius to.
Methods
Overridden Methods
| Signature | Parameters | Behavior change from SWWheel |
|---|---|---|
rotate(degPerSec, t) |
degPerSec: numbert: number (s)
|
Sets rotationAngle = initialRotation + degPerSec × t.
At t = 0, arms rest at initialRotation, not 0°.
|
reset() |
— |
Restores rotationAngle to initialRotation
(not 0) and currentRadius to originalOuterRadius.
|
New Setters
| Method | Parameter | Notes |
|---|---|---|
setInitialRotation(deg) |
deg: number |
Sets this.initialRotation = deg,
this.rotationAngle = deg, and immediately
repositions the arms via _repositionSpokes().
Always zero tElapsed after calling this
so the animation clock restarts from the new pre-tilt baseline.
|
Key Inherited Methods (from SWWheel)
| Method | Notes |
|---|---|
drawOnGrid(grid) |
Draw in user coordinates. Preferred over draw(). |
breathe(sinusoid, t) |
Oscillate arm length using an SWSinusoid. Call every frame. |
setSpokeThickness(w) |
Arm stroke weight in pixels. |
setSpokeColor(swColor) |
Arm color (each arm gets a defensive copy). |
setSpokeCapStyle(cap) |
Set cap style: 'ROUND', 'SQUARE',
or 'PROJECT'. Propagates to all four arms.
See Cap Styles for details.
|
setOuterRadius(r) |
Arm length in user units. Also updates originalOuterRadius. |
Line Cap Styles — A p5.js Global State Lesson
strokeCap() is a global setting in p5.js.
If any shape changes it during a frame, every subsequent shape drawn that
frame inherits the new setting — unless something resets it.
SWLine now brackets every line() call with
explicit set & restore, so it never leaks its cap style to other shapes.
By default, p5.js draws all line endpoints with round caps.
This is fine for general lines, but a geometric crosshair looks better with
flat tips. The cap style is controlled per SWLine via
this.capStyle ('ROUND' | 'SQUARE'
| 'PROJECT').
The pattern used in SWLine.draw() and
SWLine.drawOnGrid():
window[this.capStyle] looks up the p5 constant by name at
draw time — so the string 'SQUARE' becomes the integer
constant SQUARE without needing p5’s globals to be
available when the class is first defined.
The Three Options
| Value | p5 Constant | Visual Result | Default for |
|---|---|---|---|
'ROUND' |
ROUND |
Semicircular end cap; line length equals exact nominal length | SWLine (p5 default) |
'SQUARE' |
SQUARE |
Flat, blunt end; line length equals exact nominal length | SWCross — cleanest crosshair |
'PROJECT' |
PROJECT |
Flat end that extends beyond the endpoint by ½ the stroke weight | None |
ROUND and SQUARE is very obvious.
PROJECT visually lengthens the arms.
Usage Examples
Minimal plus sign at the grid origin
Diagonal cross (45° pre-tilt) with red arms
Animated rotation with initialRotation offset
Changing initialRotation at runtime
Breathing plus sign
Switching cap style at runtime
Source — shapeClasses/SWCross.js
Loaded live from the file so this view always reflects the current version. Use the button to copy the full source to the clipboard.
Loading source…