flag-cloth

Configuration

Every constructor option, its exact default, and whether it can change at runtime.

createFlagCloth() accepts a FlagClothOptions object. The React component accepts the same options except container, because it creates that element for you. Nested groups are partial: pass only the values you want to change.

import type { FlagClothOptions } from 'flag-cloth';

Required values

OptionTypeDefaultMeaning
containerHTMLElementrequiredElement that hosts and sizes the canvas. Vanilla API only.
texturestring | TexImageSource | nullrequiredImage URL, ready browser image source, or null to render material.baseColor.

Cloth topology and lifecycle

OptionTypeDefaultMeaning
widthnumber3Cloth width in simulation units, not CSS pixels.
heightnumber2Cloth height in simulation units, not CSS pixels.
segments.xnumber32Horizontal cell count. There are x + 1 particle columns.
segments.ynumber20Vertical cell count. There are y + 1 particle rows.
attachmentAttachment'left'Edge, corner, partial edge, custom grid points, or 'none'.
autoStartbooleantrueMarks the instance as started immediately and schedules its owned animation loop.

Changing width, height, or segments rebuilds the simulation topology but keeps the same canvas, WebGL2 context, renderer instance, observers, and event controllers. Changing attachment only rebuilds the pin mask. See Attachments for every accepted shape.

Wind

PropertyTypeDefaultMeaning
direction[number, number, number][1, 0.08, 0.3]World-space direction, normalized internally. A zero vector disables directional wind.
strengthnumber7Base wind speed.
turbulencenumber0.35Strength of noisy gust and lateral variation.
gustFrequencynumber0.5Rate of time variation in the wind field.
spatialScalenumber0.8Frequency of spatial variation across the cloth.
aerodynamicCoefficientnumber0.35Multiplier that converts triangle pressure into particle force.

Simulation

PropertyTypeDefaultMeaning
dampingnumber0.985Velocity retained over a 1/60-second interval.
gravity[number, number, number][0, 0, 0]World-space acceleration. Gravity is disabled by default for a conventional flag.
fixedTimeStepnumber1 / 60Stable accumulator step in seconds.
maxFrameDeltanumber0.1Maximum display-frame gap accepted by the accumulator, in seconds.
substepsnumber2Integration subdivisions per fixed step. Values are floored and clamped to at least 1.
constraintIterationsnumber6Structural, shear, and bend solve passes per substep.
structuralStiffnessnumber1Horizontal and vertical distance retention.
shearStiffnessnumber0.9Diagonal distance retention.
bendStiffnessnumber0.4Two-cell distance retention.
maxCorrectionnumber0.25Per-constraint correction cap. Set 0 to disable the cap.
massnumber1Total cloth mass, independent of grid density.

Stiffness values are clamped from 0 to 1 by the solver and converted to an iteration-independent correction. See Cloth simulation before raising grid resolution or solver work.

Pointer interaction

PropertyTypeDefaultMeaning
enabledbooleantrueEnables mouse, pen, and touch dragging through Pointer Events.
touchAction'pan-y' | 'pan-x' | 'none' | 'auto''pan-y'Browser touch policy. The default preserves vertical page scrolling and horizontal cloth dragging.
dragRadiusnumber1Grid-space radius around the selected particle.
dragStiffnessnumber0.9Strength of the temporary positional constraint.
maxDragDistancenumber4Maximum movement from the initial grab point. Set 0 for unlimited movement.
allowPinnedbooleanfalseAllows attached particles to be selected temporarily.
releaseImpulse[number, number, number][0, 0, 0]World-space velocity impulse applied on release.

Renderer

The table shows the default quality profile. Selecting performance changes maxPixelRatio to 1, shadows to false, and shadingUpdateInterval to 2. Explicit values passed alongside a preset win.

PropertyTypeDefaultMeaning
preset'quality' | 'performance''quality'Selects a tested rendering profile.
alphabooleantrueRequests an alpha-enabled WebGL2 context. Construction-time only.
antialiasbooleantrueRequests browser multisample antialiasing. Construction-time only.
desynchronizedbooleanfalseRequests a desynchronized context when supported. Construction-time only.
powerPreferenceWebGLPowerPreference'high-performance'GPU preference used during context creation. Construction-time only.
maxPixelRationumber1.5Caps backing-store resolution; values below 0.5 are clamped.
textureFiltering'linear' | 'nearest''linear'Image texture sampling mode.
transparentbooleantrueClears with alpha instead of backgroundColor.
backgroundColorstring'transparent'CSS color used when transparent is false.
shadowsbooleantrueEnables the nine-sample soft silhouette shadow.
shadingUpdateIntervalnumber1Recalculates normals every N rendered frames.

alpha, antialias, desynchronized, and powerPreference are intentionally absent from FlagClothUpdateOptions.renderer; changing a WebGL context attribute requires creating a new instance.

Material

PropertyTypeDefaultMeaning
baseColorstring'#f2f4f3'Cloth color used when texture is null.
ambientnumber0.58Minimum material brightness.
diffusenumber0.55Directional fold-lighting strength.
specularnumber0.16Highlight strength in the quality preset.
shininessnumber18Highlight concentration.
foldContrastnumber0.42Dark/light fold contrast.
shadowColorstring'rgba(0, 0, 0, 0.3)'CSS color for the silhouette shadow.
shadowBlurnumber18Shadow sampling radius in CSS pixels.
shadowOffsetXnumber10Horizontal shadow offset in CSS pixels.
shadowOffsetYnumber12Vertical shadow offset in CSS pixels.

Lighting

PropertyTypeDefaultMeaning
enabledbooleantrueEnables fold lighting.
intensitynumber1Overall directional-light multiplier.
direction[number, number, number][-0.4, 0.7, 1]World-space light direction, normalized internally.

Camera

PropertyTypeDefaultMeaning
fovnumber40Vertical perspective field of view in degrees.
nearnumber0.01Near clipping distance.
farnumber100Far clipping distance.
position[number, number, number] | nullnullCamera position. null automatically fits the cloth.
lookAt[number, number, number][0, 0, 0]World-space camera target.

Visibility and debug

PropertyTypeDefaultMeaning
visibility.pauseWhenOffscreenbooleantruePauses through IntersectionObserver while the container is outside the viewport.
visibility.pauseWhenDocumentHiddenbooleantruePauses while document.visibilityState is hidden.
debugboolean | Partial<DebugOptions>falseEnables or configures the optional overlay.
debug.enabledbooleanfalseCreates the debug overlay.
debug.updateIntervalnumber500Overlay refresh interval in milliseconds, clamped to at least 100.

debug: { updateInterval: 250 } does not enable the panel by itself; include enabled: true or use debug: true.

Advanced integration

PropertyTypeDefaultMeaning
canvasHTMLCanvasElementlibrary-createdSupplies a caller-owned canvas.
contextWebGL2RenderingContextlibrary-createdSupplies a caller-owned context; its canvas is used when canvas is omitted.
externalAnimationLoopbooleanfalseDisables internal requestAnimationFrame scheduling.

advanced and autoStart are construction-time options and are not part of FlagClothUpdateOptions.

Runtime updates

await flag.setOptions({
  attachment: { points: [[0, 0], [0, 10], [4, 4]] },
  wind: { strength: 11 },
  interaction: { enabled: false },
  renderer: { preset: 'performance' },
});

setOptions() is asynchronous because it can load a new texture. Wind, pinning, simulation, interaction, renderer, material, lighting, camera, visibility, and debug changes do not recreate the WebGL2 context.

On this page