7. Views
A view turns the model's state into something the player sees or hears. It builds its output once, and then, every frame, its
refresh()reads the state again and writes the output to match. A view doesn't keep any domain state or run a clock of its own, so you can delete it, replace it, or run the game without any views, and the game plays the same.
Related: Models · The Tick · Bindings · Presentation State · Writing Views in JSX · MVT on One Page
Here is a view of a bullet that looks reasonable at first. It reads the bullet's position once, and then moves the sprite up a little on every frame:
// Don't do this: the view runs its own animation
function BulletView(bindings: BulletViewBindings): Container {
const view = new Container();
view.position.set(bindings.x(), bindings.y());
// ...draw the bullet...
function refresh(): void {
view.y -= BULLET_SPEED_PER_FRAME;
}
setRefresh(view, refresh);
return view;
}It works on the first playtest. Then the problems start:
- On a 120 Hz screen,
refresh()runs twice as often, and the bullet flies twice as fast as the model's bullet. - When the game pauses, the model's bullet stops. If anything keeps refreshing the view, such as a pause menu drawn over it, this bullet flies on.
- When the model's bullet hits an enemy, the view's bullet may be somewhere else. The two drifted apart, and the code didn't detect it.
- In a test, refreshing the view twice moves it twice. The picture depends on how many times you looked, not on the state of the game.
The model decides the bullet's position. The view's job is to show the bullet at the model's position, every time it refreshes:
function refresh(): void {
view.position.set(bindings.x(), bindings.y());
}Now all four problems go away. The view can't drift, because it doesn't keep any state that could drift. This chapter is about that one idea, and what follows from it.
Experiment 7A puts both views side by side, showing one bullet from one model. The buttons on the right set how much time the model receives each frame. The example starts in slow motion, so the model's bullet moves at a quarter of its speed. The left view follows it. The right view counts frames, not time, so its bullet flies on at full speed, and the two are soon far apart. Press Paused, and the left bullet stops while the right one carries on. Press Normal, and on a 60 Hz screen the two roughly agree. On a faster screen, the right bullet races ahead even then.

7.1 The Role of a View
A view is a window onto the simulation. Each frame, it reads the current state of the models and updates its output to match. The output is often a picture, such as a Pixi container or some DOM elements, but MVT doesn't restrict what it is. It could be a sound (Chapter 20), a debug panel, or a log in a test.
You can open several windows onto the same model, and they all stay in step, because each one reads the same state. You can close every window, and the simulation runs on unchanged. A test, a headless replay or a server can run the models without any views.
In Experiment 7B, a lift is shown through three windows at once: its shaft, a dial and a panel. Press 1, 2 or 3, or click a window's button, to close that window or open it again. Close all three, wait a moment, and open one. The lift carried on without them, and the new view shows where it is on its first refresh.

MVT has two requirements for a view:
- It refreshes. Each frame, it reads the current state again and updates its output to match.
- It doesn't hold domain state or domain logic. Domain state is the state that decides what happens in the game: positions, scores, lives, phases. The view doesn't own any of it, enforce any rules, or decide what happens next. The models do that.
Everything else is your choice. Views can be functions or classes. They can be Pixi containers, DOM elements or three.js objects. The ticker can find their refresh methods by walking a tree, or call each one by hand.
When this book says a view is "stateless", it always means it doesn't hold any domain state. A view does hold things. It holds its display objects, which last from frame to frame. Some views also hold a little state of their own for a fade or a smooth count-up, which the model doesn't need to track. That is called presentation state, and Chapter 11 is all about it.
7.2 A Minimal View
Here is the bullet view in full:
import { Container, Graphics } from 'pixi.js';
import { setRefresh } from '@mvtjs/pixi';
interface BulletViewBindings {
x: () => number;
y: () => number;
isVisible: () => boolean;
}
function BulletView(bindings: BulletViewBindings): Container {
const view = new Container();
const gfx = new Graphics();
gfx.circle(0, 0, 4).fill(0xffffff);
view.addChild(gfx);
function refresh(): void {
view.visible = bindings.isVisible();
view.position.set(bindings.x(), bindings.y());
}
setRefresh(view, refresh);
return view;
}The view does its work in two places:
- When it's made, it builds its display objects and draws what never changes, here a white circle.
- In
refresh(), it reads the state it shows and writes it onto those objects: whether the bullet is visible, and where it is.
Everything the view reads comes from its bindings, a plain object that's passed to it when it's made. Each binding here is a function that returns a current value, such as the bullet's x. The view doesn't depend on where the values come from. They might come from a model, or from a test that returns fixed numbers. Bindings also work in the other direction: a view can receive functions to call when the player does something, such as onTapped. Chapter 8 covers both kinds.
In this book
A view is a function named for what it shows, such as BulletView. It takes one bindings object and returns a Pixi Container. The name starts with a capital letter so that the function can be used as a JSX tag (<BulletView ... />) as well as called (BulletView({ ... })).
setRefresh, from @mvtjs/pixi, sets the view's refresh step on its container. The ticker calls refreshView on the stage once per frame, which finds every refresh method in the tree, wherever it is, and calls it. So a parent never passes the call on to its children, for the reasons in Chapter 5.
7.3 The refresh() Contract
The ticker calls each view's refresh() once per frame, after every model has updated (see Chapter 5). So a view always reads state that has finished changing for this frame. In return, refresh() must follow five rules.
It's reactive. Every value that may change between frames is read again in refresh(). It's never read once, when the view is made, and kept. The first bullet view broke this rule: it read the position once, and from then on it used its own copy. The one exception is a value whose bindings type says the view reads it once, which Chapter 8 explains.
It's idempotent. Calling refresh() twice with the same state produces the same output as calling it once. This is why a test can refresh a view and look at it, without worrying about how many refreshes came before. A += in refresh() is the usual way this rule breaks. A refresh step may still remember what it drew, so that it can skip slow work when nothing has changed. That's allowed, because remembering doesn't change the output.
It's free of side effects. refresh() reads state and writes the presentation output. It doesn't change a model, call a relay binding, send an event, or start a transition. If it did, the picture on the screen would depend on when the views happened to run, and one view could change what the next one reads in the same frame.
It works from state, with no time involved. refresh() doesn't receive deltaMs. It works out the output from the state as it is now, so a paused game refreshes in the same way as a running one.
It never reads back what it wrote. Keep a view's own state in variables, work each output value out from that state, and assign it. Writing sprite.alpha -= 0.1 reads the output back, which makes the display object hold the state. Rounding, another effect or a rebuild can then change that state without any warning. A measurement that the renderer works out, such as the width of a text, is different. It's input, and Chapter 21 shows how to read one.
These five are rules V-reactive, V-idempotent, V-readonly, V-refresh and V-one-way in Appendix A.
7.4 Immediate-Mode Data Flow, Retained-Mode Output
Graphics code is often described as one of two kinds:
- Immediate mode. Each frame, the program draws everything from scratch, straight from its current data. The program doesn't keep anything from the last frame. Canvas 2D drawing and Dear ImGui work this way.
- Retained mode. A library keeps a tree of objects, such as Pixi's scene graph or a browser's DOM, and draws it each frame. The program builds the tree once, and changes the objects' properties as things move.
An MVT view combines the two. Its data flow is immediate mode: refresh() reads all the state it shows, every frame, as if it were drawing from scratch. The view doesn't subscribe to anything, and the model doesn't send change notifications or partial updates. Its output is retained mode: the view builds its display objects once, and refresh() sets their properties. It doesn't tear them down and build new ones.
From immediate mode, the view gets correctness. After every refresh(), the output matches the current state. The output can't be stale, because the view didn't keep any old state, and the view can't miss an update, because the model never sends one. From retained mode, it gets efficiency. The view doesn't make display objects each frame, so the engine doesn't have any garbage to collect. The objects last, and only their properties are written.
The retained output is, in effect, a cache of the last refresh(). Like any cache, it's only as good as the code that fills it. If you add a field to the model and refresh() doesn't read it, the output keeps showing whatever was there before. The reactive and idempotent rules guard against that, because they require refresh() to read everything it shows, every time.
Reading everything every frame sounds slow, but each read is a function call, and at game scale, all of them together take a small part of a frame. Chapter 10 explains why MVT polls rather than pushes changes to views, and measures the performance cost.
7.5 Doing Less Work in refresh()
Most of what refresh() does is fast: copy a number onto a property, set a flag. Read those values directly, every frame. Some work takes longer, such as making new text, or rebuilding a grid of tiles. When the value behind it changes only now and then, doing that work on every frame wastes time.
The answer is to compare the value with the one you read last frame, and do the work only when it differs. That's called change detection. Chapter 9 teaches it in full. Here is one of its tools, a computed value, which keeps a score label from making a new string on every frame:
import { createComputed } from '@mvtjs/utils';
// Made once: it makes the text again only when the score changes
const scoreText = createComputed({ watch: bindings.score, compute: String });
function refresh(): void {
label.text = scoreText();
}Another way to do less work is to skip what can't be seen. A view's refresh() may hide its own container and stop:
function refresh(): void {
const isVisible = bindings.isVisible();
view.visible = isVisible;
if (!isVisible) return;
view.position.set(bindings.x(), bindings.y());
}Hiding a view doesn't stop its refresh method running, so it can show itself again next frame. A plain return skips only the rest of this view's own refresh, though. Its child views are still refreshed. To skip a whole subtree, such as a hidden panel, return SKIP_DESCENDANTS, from @mvtjs/pixi, instead. Then the views below it aren't refreshed on that call.
7.6 Concerns That Belong in a View
A view describes how things look, or sound. It turns domain state into presentation:
- a position in the domain's units, such as tiles, into pixel coordinates
- a named state, such as
'dying', into a texture, a colour or an animation frame - a count, such as lives left, into a row of icons
- a change in state, such as a phase beginning or a count rising, into a sound
And these concerns don't belong in a view:
| Doesn't belong in a view | Why |
|---|---|
| Domain state | It belongs to a model, so that there is one answer to every question about the game |
| Domain logic | Rules belong in models, where a test can check them without drawing anything |
Wall-clock timers, such as setTimeout or Date.now() | Time reaches a view only through the ticker, so pausing, slowing down and tests all work |
| Animations that run by themselves | Anything that moves follows state read in refresh(), as the bullet now does |
Writes to a model in refresh() | A view reads in refresh(). It reports the player's input through relay bindings (Chapter 8) |
Some views do need state of their own, such as the progress of a 300 ms fade that the model doesn't track. That's presentation state, and the view advances it in an update(deltaMs) step of its own, with the deltaMs that the ticker passes in. Chapter 11 shows how, and describes a simple test for telling it apart from domain state: would the game still play correctly with the view deleted?
Views also compose into trees, and a view tree doesn't need to mirror the model tree. Chapter 13 covers that.
7.7 Writing the Body in JSX
You can also write a view's body in JSX, a syntax for building a tree of objects from tags. Here is the bullet view again, with a JSX body:
/** @jsxImportSource @mvtjs/pixi */
import type { Container } from 'pixi.js';
function BulletView(bindings: BulletViewBindings): Container {
return (
<graphics
ref={(g) => g.circle(0, 0, 4).fill(0xffffff)}
visible={bindings.isVisible}
x={bindings.x}
y={bindings.y}
/>
);
}Each attribute you give a function, such as x={bindings.x}, is read again every frame, just as refresh() would read it, and that's all you need to read the JSX samples in the next few chapters. Chapter 12 teaches the rest, and when to choose JSX over plain TypeScript.
7.8 Releasing a View's Resources
Most views don't hold anything that outlives them. Their display objects are destroyed along with them, and since views poll rather than subscribe, they don't have any subscriptions to undo. A few views hold something that isn't one of their display objects, and so isn't destroyed with them:
- a listener on
windowordocument, such as a keyboard handler - a resource that several of its display objects share, such as one shape that many
Graphicsobjects draw from - a texture the view made itself
- a GSAP timeline
Release each one where you acquire it, so the two can't drift apart.
In this book
A view releases these on Pixi's 'destroyed' event, set up right where it acquires them:
window.addEventListener('keydown', onKeyDown);
view.on('destroyed', () => {
window.removeEventListener('keydown', onKeyDown);
});In a JSX body, the onDestroyed attribute does the same for the element it's on:
const boidShape = new GraphicsContext()
.poly([8, 0, -5, 4, -5, -4])
.fill(0x44ccff);
return (
<container onDestroyed={() => boidShape.destroy()}>
{/* ...Graphics that all draw boidShape... */}
</container>
);Prefer the event to replacing the view's destroy method. Any number of listeners can be added, each next to what it releases, and the listeners don't need to call the original.
Views make textures and shapes so often that the JSX runtimes have an attribute for them, owns. List what the view made in owns on any element. When that element is destroyed, the runtime frees each one, so you don't need to write the listener yourself. @mvtjs/pixi frees a resource by calling its destroy method, and @mvtjs/three by calling its dispose method. Here's the same shape again, listed in owns:
const boidShape = new GraphicsContext()
.poly([8, 0, -5, 4, -5, -4])
.fill(0x44ccff);
return (
<container owns={[boidShape]}>
{/* ...Graphics that all draw boidShape... */}
</container>
);A resource that several views share belongs to the view that made it, and only that view lists it in owns. Make each resource in a variable beside the JSX, never inside the attribute that uses it. A texture made in <sprite texture={new Texture({ source })} /> doesn't have a name, so you can't list it, and it's never freed.
Destroy with { children: true }
Pixi's destroy() on its own detaches a container's children without destroying them, so the containers below it never receive 'destroyed'. Whatever code ends a view's life should call destroy({ children: true }). To catch a missed one, in development @mvtjs/pixi logs a warning when a container is destroyed without { children: true } while a container below it has a 'destroyed' listener, since that listener would never run.
7.9 Updating and Refreshing Views with the @mvtjs Libraries
The ticker ticks only the root view, and a function walks the view tree to reach the rest, as Chapter 5 explained. Walking the tree has one more advantage. A view deep in the tree takes part just by having its steps, and the views above it don't need to reference it. When each parent forwards the tick by hand, adding a view with an update step means changing every parent above it, and a parent that doesn't forward the tick leaves the view frozen. Either way, a tick looks the same from the ticker, however deep the trees go.
MVT requires the order, not a particular way of reaching every view. The @mvtjs libraries walk the view tree for you. A view sets its two steps on its own container, and the loop updates, then refreshes, the whole stage.
| Function | Purpose | Called on |
|---|---|---|
setUpdate(view, fn) | Sets the view's update(deltaMs), which moves its presentation state on | Views that have presentation state (Chapter 11) |
setRefresh(view, fn) | Sets the view's refresh(), which reads state and writes output | Every view that shows state |
updateView(view, deltaMs) | Calls every update step in view, each once, parents before children | The stage, once per frame, after the models |
refreshView(view) | Calls every refresh step in view, each once, parents before children | The stage, once per frame, after updateView |
All four come from the renderer package you use: @mvtjs/pixi, @mvtjs/three or @mvtjs/html. They work the same way for every renderer.
- A view includes its child views. A view anywhere in the tree takes part just by setting its steps. Its parents don't need to reference it, or pass anything on. A view never calls its children's steps itself. If it did, they would run twice.
- Children added during a refresh are refreshed too. If a refresh step attaches a new child view,
refreshViewrefreshes it before it returns, so a view is never drawn before its first refresh. - A subtree can be skipped. Either step may return
SKIP_DESCENDANTSto skip its container's descendants for that call, such as a panel that is hidden. Making a container invisible doesn't skip anything by itself. - The walk caches the steps it finds. Each function keeps a list of the steps in the tree, and finds them again only when a view is added or removed. So hiding a subtree is faster than taking it out of the tree and putting it back.
- Updates and refreshes are separate calls. A test can call
updateViewmany times andrefreshViewonce. The code that runs the loop can pause a game by leaving its container out ofupdateView, and still refresh it, as the pause in 5.6 Time Control does.
7.9.1 Why not Pixi's onRender?
Pixi's onRender runs once per draw, not once per tick, so a loop that ticks several times per draw gets the wrong number of calls, and a test without a renderer doesn't get any. The @mvtjs/pixi notes explain the differences in full.
7.10 Common Mistakes
7.10.1 A sprite that runs ahead of the model
A sprite runs too fast on a fast screen, keeps moving while the game is paused, or ends up somewhere other than the model's position, as in the first example. It happens because view.x += speed in refresh() feels like the obvious way to animate. Put the position, or the animation's progress, in the model, and set it in refresh(). If the animation is purely cosmetic, it's presentation state instead (Chapter 11).
7.10.2 A game that depends on which views exist
The game behaves differently depending on which views exist, or on their order. A view is "helping" in refresh(), for example by clamping a value it reads and writing it back to the model. Move the rule into the model, and have the view only read.
7.10.3 Slow work on every frame
A view makes new text, or redraws a whole grid, every frame, for a value that changes once a minute. It happens because rebuilding everything is the simplest correct code. Do the work only when the value changes, with change detection (Chapter 9).
7.11 Summary
A view builds its output once and, every frame, reads the current state and writes it onto that output. Its refresh() is reactive, idempotent and free of side effects, so the output always matches the models, however often you look. The data flow is immediate mode and the output is retained mode, which gives you correctness without making new objects every frame. A view doesn't own any domain state or run a clock of its own. When slow work should run only on a change, change detection (Chapter 9) does it. When a view needs a cosmetic state of its own, presentation state (Chapter 11) does it. A view that holds a resource other than its display objects, such as a window listener, releases it when the view is destroyed.
7.12 Further Reading
- Casey Muratori, "Immediate-Mode Graphical User Interfaces" (2005). The talk that named and popularised immediate-mode UI: drawing the interface from the program's current data on every frame, rather than keeping a tree of widgets in step with it.