Learn to create multi-user shared animations and interactions with bouncing balls simulation
MyModel
and BallModel
. Both classes must be registered with Multisynq for proper synchronization.
Multisynq.Constants
to ensure all users share the same configuration values:
Multisynq.Constants
is recursively frozen once a session starts to prevent accidental modification.MyModel
is the root model passed to Multisynq.Session.join()
. It creates and stores the BallModel
objects in the MyModel.children
array.
BallModel
represents a shaped, colored, bouncing ball. The model stores only the data needed for synchronization:
'circle'
or 'roundRect'
)BallModel
subscribes to the 'touch-me'
event using its own ID as scope. This ensures that only the specific ball responds to touch events intended for it.
BallModel
schedules its first step()
method invocation. This creates a continuous simulation loop:
moveBounce()
method updates ball position and handles wall collisions. When a ball hits a wall, it gets a new random speed vector.
MyView
and BallView
.
MyView
is called when a session instance starts, receiving the MyModel
object as an argument. It builds the visual representation and container for all balls.
MyView
accesses the model’s children collection and creates a BallView
for each BallModel
.
MyView
listens for browser “resize” events and adjusts the view scale accordingly. This ensures all users see the same scene regardless of their window size.
BallView
tracks its associated BallModel
and handles visual representation and user interaction.
BallView
subscribes to 'pos-changed'
events from its specific model. The "oncePerFrame"
handling ensures efficient rendering even with multiple position updates per frame.
BallView
sets up touch/click handlers that publish 'touch-me'
events. The corresponding BallModel
subscribes to these events and toggles ball motion on and off.
Model-View Separation
Deterministic Simulation
Efficient Event Handling
Interactive Synchronization
Multisynq.Constants
"oncePerFrame"
handling for frequent updatesdetach()
methods