Documentation

Get started

Add a drivable car to your page, then choose which elements it can interact with. Start with a script or npm, and mark existing elements with data-jr-* attributes.

Installation

Choose CDN, JavaScript, or React. All three accept the same authored configuration and expose the same ride controller.

IntegrationEntryReact runtimeStartup
CDNembed.jsIncludedA script with data-jr-mount starts automatically.
JavaScript@joy-ride/gameIncludedCall mount() explicitly.
React@joy-ride/game/reactThe application's React 19 and React DOM 19Render JoyRide; the component owns cleanup.

Choose one integration per page. The root import, @joy-ride/game, bundles React and starts only when you call mount(). The /embed entry adds globals and declarative script startup; /react shares the application's React runtime. React and React DOM are optional package peers; the JavaScript and CDN entries do not need an installed React runtime.

CDN

Paste this into an HTML page:

HTML
<h1
  data-jr-breakable
>
  Drive through this headline
</h1>

<script
  type="module"
  src="https://unpkg.com/@joy-ride/game@latest/embed.js"
  data-jr-mount
></script>

data-jr-mount starts Joy Ride when the document is ready. Drive with WASD or the arrow keys; click the car to change vehicles. See Script tag options for presets, saved preferences, routing, and arrival targets.

npm, pnpm, or Yarn

Terminal
npm install @joy-ride/game

Use pnpm add or yarn add instead if those manage your project. Mount after the page elements exist. This entry includes React and React DOM internally; no framework installation is needed.

TypeScript
import { mount } from '@joy-ride/game';

const ride = mount();

await ride.ready;

// Call ride.dispose() when the host view is removed.

The package import does not mount automatically. It is safe to import during server rendering, but call mount() only in the browser after page content exists.

React

In an application using React 19 and React DOM 19, import the React entry so Joy Ride shares the application's runtime. It does not include another copy of React.

TSX
'use client';

import { JoyRide } from '@joy-ride/game/react';

export const JoyRideExperience = () => (
  <JoyRide
    initialOptions={{
      player: { vehicle: 'porsche-911-997' },
      game: { quality: 'high', masterVolume: 0 },
    }}
  />
);

JoyRide mounts after the component reaches the browser and disposes the ride on unmount. It renders its controls through the host React tree, supports Strict Mode, and does not create a separate React root. The initial options accept the same MountOptions or saved PlaygroundConfiguration as mount().

PropMeaning
presetNamed defaults, currently "sfw". Applied once at mount; explicit initial options override them.
initialOptionsInitial authored configuration. Read when mounting; use a new component key to replace it.
settingsLive RuntimeOptionsPatch for game and dialog, applied without remounting.
refA React ref to RideController or null; commands that need the world still await ride.ready.
onReadyReceives the initialized RideController.
onErrorReceives an initialization, presentation, or live settings error.

The entry also exports the same imperative API and types for integrations that need to call mount() in their own lifecycle. A default import remains the imperative API; the JoyRide component is a named export. Use /react rather than wrapping the root JavaScript API in a React component, which would load an additional bundled React.

Lazy loading React with a preset

The component resolves its preset after it loads, so the host only imports the React entry inside lazy():

TSX
'use client';

import { lazy, Suspense } from 'react';

const JoyRideGame = lazy(() =>
  import('@joy-ride/game/react').then(({ JoyRide }) => ({ default: JoyRide })),
);

export const JoyRideExperience = () => (
  <Suspense fallback={null}>
    <JoyRideGame
      preset="sfw"
      initialOptions={{ player: { vehicle: 'porsche-911-997' } }}
    />
  </Suspense>
);

preset and initialOptions are read once when mounting. The preset supplies game and dialog defaults; explicit initial options win within each group. Saved playground configurations remain supported and their authored settings win. Use a new component key to apply a different preset or initial configuration; settings continues to apply live updates without remounting. Unknown preset names report an initialization error through onError.

A playground in HTML

HTML
<main>
  <h1
    data-jr-breakable
    data-jr-burnable
  >
    Make a mess.
  </h1>
  <div style="background: #fafbfc">Your own layout</div>
  <div
    data-jr-liquid="paint"
    data-jr-liquid-color="#5271ff"
  >
    Blue paint
  </div>
  <button
    data-jr-trigger="click"
  >
    Drive over me
  </button>
  <output
    data-jr-hud="speed"
  >
    0
  </output> mph
</main>

The script from Installation starts the ride. The module import needs an explicit JoyRide.mount() call.

An empty capability attribute or true enables it; false disables it. Values use the units in their names, such as seconds, metres, or CSS pixels.

A playground in JavaScript

TypeScript
import JoyRide from '@joy-ride/game';

const ride = JoyRide.mount({
  game: { masterVolume: 0 },
});

const titles = ride.bind('.playful-title', {
  breakable: { health: 80 },
  burnable: { burnSeconds: 12 },
});

await ride.ready;
ride.player?.update({ appearance: 'auto' });

Selectors, attributes, and exact-element bindings share the same schema and validation. A selector binding includes future matches. An exact-element binding overrides that element's attributes.

Joy Ride finds authored objects throughout the page. Add a behavior to any existing element; no playground wrapper is needed.

Lifecycle and readiness

JoyRide.mount() returns a controller immediately. You can register bindings and listeners while it loads. Await ride.ready before world commands such as spawning vehicles or pouring liquid. Readiness includes loading and decoding the selected vehicle image. Image failures reject ride.ready; no placeholder vehicle is drawn. ride.player and ride.state are null until ready; tours wait automatically.

Only one ride can be mounted on a page. JoyRide.active returns its controller, or undefined. Call ride.dispose() before mounting another; repeated disposal is safe. Disposal restores the page and releases listeners and resources. Initialization errors reject ride.ready and clean up. Commands fail after disposal; ride.player and ride.state return null.

Sound starts muted until a browser user gesture permits it. ride.startAudio() explicitly requests audio startup; browser autoplay rules still apply.

Coordinate conventions

Browser placement, vehicle snapshots, and public event positions use CSS document pixels, X right and Y down. angleDeg is clockwise. Vehicle speed is speedMps. A supplied element places a vehicle or pour at its centre. Vehicle state is deeply readonly.

Canvas level definitions and low-level simulation positions use metres, X right and Y up. Do coordinate conversion at the browser/canvas boundary.