Rive 101 logoRive 101
β„Ή AboutπŸ“‹ ChangelogδΈ­
0%
Progress0/88
8.9

Control Properties in Javascript

9m 36s
How To Control View-Model Properties in Javascript

This course covers how to utilize the Rive JavaScript runtime (WebJS Runtime) to dynamically manipulate decoupled View-Model properties. Through data binding mechanisms, developers can directly modify numbers, strings, booleans, triggers, colors, and enums within the animation model using JS code. This eliminates complex hardcoded state machine logic during design handoff, creating a perfect closed loop between the UI elements configured by designers in Rive and real-time production data streams.

  • Variable Declaration Best Practices: In JavaScript, if a property reference or instance does not need to be reassigned later in the application lifecycle, it is highly recommended to use const instead of let for better execution safety and V8 engine optimization; let was used here purely for absolute flexibility in a demo environment.
  • API Syntax Variations per Data Type:
    • Number, String, and Enum properties manipulate data through the .value = newValue assignment.
    • Triggers do not possess a .value property; they must execute the .trigger() method directly to fire.
    • Colors use dedicated setter methods like .rgb(R, G, B) to correctly map red, green, and blue channel integer values to Rive components.
  • Hardcoded Constraints on Enums: When modifying an Enum via JavaScript, the string passed to .value must exactly match the case-sensitive option names configured within the Rive editor's View-Model panel; otherwise, the underlying state machine transitions will fail silently.
  • Pre-requisites for Auto Bind: Setting autobind: true in code relies entirely on checking Export Instance when building the runtime asset. Forgetting to check this box results in an undefined viewModelInstance at runtime.