A canvas object is drawing code of your own: a starry sky, a mini-game, a custom transition. When your script shows one with show canvas, the engine creates a <canvas-container> with one <canvas> per layer your object asks for, places it on the game screen according to its mode, and calls your object’s start function with those canvases.

The game screen covered by a dark blue night sky with a diagonal band of stars, Yui's line "Look at the sky." in the text box
A canvas object with two layers, shown as a displayable

Tag <canvas-container>
Where it lives Created by the Canvas action on the game screen
Built on Component
Source src/components/canvas-container/

How to show one

Declare the object in js/script.js and show it from your script (Canvas has every detail):

// js/script.js
monogatari.action('canvas').objects({
  stars: {
    layers: ['sky', 'stars'],
    props: {},
    state: {},
    start: ({ sky, stars }, props, state, container) => {
      const width = container.offsetWidth;
      const height = container.offsetHeight;

      for (const layer of [sky, stars]) {
        layer.width = width;
        layer.height = height;
      }

      const skyContext = sky.getContext('2d');
      skyContext.fillStyle = '#0b1d3a';
      skyContext.fillRect(0, 0, width, height);

      const starsContext = stars.getContext('2d');
      starsContext.fillStyle = '#fff8d6';

      for (let i = 0; i < 120; i++) {
        starsContext.beginPath();
        starsContext.arc((i * 97) % width, (i * 53) % height, (i % 3) + 1, 0, Math.PI * 2);
        starsContext.fill();
      }
    },
    stop: () => {},
  },
});
'show canvas stars displayable',
'y Look at the sky.'

start receives the layers as an object of canvases by name, then the object’s props and a copy of its state, then the <canvas-container> itself; this is the engine. stop gets the same arguments when the canvas is hidden (hide canvas stars), when the player goes back past it, and when the game ends. An object with no layers gets one, named base.

Words after with become classes on the container: 'show canvas stars displayable with fadeIn' adds fadeIn.

Never write <canvas-container> in index.html.

Modes

Mode Where it goes Does the story wait?
displayable (the default) In the scene ([data-content="visuals"]), filling it, behind the text box. No: the next statement runs at once.
background Inside the scene’s background element. Its resize function runs when the window changes size. No.
character In the scene, standing on the bottom edge like a character sprite, with data-character set to the object’s name. No.
modal In the scene, filling it. Yes, until it’s hidden.
immersive Over the whole game screen, text box included, on a black background. Its resize function runs when the window changes size. Yes, until it’s hidden.

While a modal or immersive canvas is shown, the player’s clicks don’t move the story. Your object ends it itself, for example from a button or a timer in its code, by running this.run('hide canvas stars'): the canvas is hidden and the story moves on.

Markup

<canvas-container data-component="canvas-container" class="animated" mode="displayable" canvas="stars">
  <div data-content="wrapper">
    <canvas data-layer="sky" width="1280" height="720"></canvas>
    <canvas data-layer="stars" width="1280" height="720"></canvas>
  </div>
</canvas-container>

The layers are stacked on top of each other in the order of layers, all in the same grid cell.

Hook What it is
[canvas="…"] The container of one canvas object, by its name.
[mode="…"] Its mode.
[data-layer="…"] One layer’s <canvas>.
.left, .right In character mode, push it to an edge (from with left or with right).

Properties and attributes

Name Type What it is
mode string The mode.
canvas string The object’s name.
object object The object’s layers, props, state, start, stop and resize.
classes string[] The classes from the statement.

The <canvas> elements are also on the element as layers, an object by name: document.querySelector('[canvas="stars"]').layers.sky.

Events

None of its own.

Style it

The container fills its place (width and height 100%), and the canvases are centered in it. A canvas keeps the size your code gives it (layer.width, layer.height), so set it from the container’s size in start, as above. In immersive mode the container covers the game screen with a black background (z-index: 20); in modal mode it’s above the stage’s particles (z-index: 2), so the player can click it; in character mode it’s at most 80vh tall and as wide as its canvases.

/* style/main.css */
canvas-container[canvas="stars"] {
  opacity: 0.85;
}

Replace or extend it

The Canvas action creates the element with document.createElement('canvas-container'), finds it again with [data-component="canvas-container"][canvas="…"][mode="…"], and reads its props.object and layers. Change what’s drawn in your object’s functions rather than in the component.