Character Sprite
Most characters are one picture per sprite. A layered character is drawn from several pictures stacked on top of each other: a body, a face, an accessory. Each layer can be changed or hidden on its own, so a few pictures make many combinations. When show character shows a layered sprite, the engine draws it with a <character-sprite>.

| Tag | <character-sprite> |
| Where it lives | Created by show character in the scene, for layered sprites |
| Built on | Component |
| Source | src/components/character-sprite/ |
WARNING
Layered characters are experimental in Monogatari 2.9.0: they work only with 'ExperimentalFeatures': true in js/options.js, and they may change. Without that setting, show character with a layered sprite shows nothing, with no error.
How to show one
A layered character declares its layers, bottom first, the pictures of each layer, and sprites that combine them. The pictures here are Yui’s, by Queen BeeBee, from monogatari-characters-v2.9.0.zip (copy its yui folder to assets/characters/yui/, and read their terms: free games only, never with AI, always with credit to Queen BeeBee):
// js/script.js
monogatari.characters({
'y': {
name: 'Yui',
color: '#c28cf0',
directory: 'yui',
layers: ['base', 'mouth', 'eyes', 'eyebrows', 'ribbon'],
layer_assets: {
base: {
'pose-1': 'layers/base/pose-1.png',
},
mouth: {
smile: 'layers/mouth/small-smile.png',
grin: 'layers/mouth/grinning.png',
},
eyes: {
open: 'layers/eyes/default.png',
closed: 'layers/eyes/closed-up.png',
},
eyebrows: {
normal: 'layers/eyebrows/normal.png',
},
ribbon: {
on: 'layers/ribbon/on.png',
},
},
sprites: {
happy: 'happy.png',
cheerful: {
base: 'pose-1',
mouth: 'smile',
eyes: 'open',
eyebrows: 'normal',
ribbon: 'on',
},
},
},
});'show character y cheerful at right',
'y A sprite made of layers.',
'show character y:mouth grin',
'show character y:eyes closed',
'hide character y:ribbon'A sprite whose value is an object (cheerful) is layered: each key is a layer and each value one of that layer’s pictures. show character y:mouth grin changes one layer, and hide character y:ribbon removes one. Character layers covers those statements. Every picture of a character should be the same size, so the layers line up.
NOTE
Before 2.9.0, a layered sprite showed only its right half: the game screen's rule that centers sprites also moved each layer half its width to the left. Games made for those versions added character-sprite [data-layer] { translate: none; } to style/main.css. 2.9.0 does the same in its own stylesheet, so the rule can stay or go.
Markup
<character-sprite data-component="character-sprite" class="animated right" data-character="y" data-sprite="cheerful" data-position="right" directory="assets/characters/yui/" width="427" height="1350" ratio="0.316" role="img" aria-label="Yui, cheerful">
<div data-content="wrapper" style="height: 1350px; width: 182.187px;">
<img src="assets/characters/yui/layers/base/pose-1.png" data-layer="base" data-sprite="pose-1" style="z-index: 0;">
<img src="assets/characters/yui/layers/mouth/small-smile.png" data-layer="mouth" data-sprite="smile" style="z-index: 1;">
<img src="assets/characters/yui/layers/eyes/default.png" data-layer="eyes" data-sprite="open" style="z-index: 2;">
<img src="assets/characters/yui/layers/eyebrows/normal.png" data-layer="eyebrows" data-sprite="normal" style="z-index: 3;">
<img src="assets/characters/yui/layers/ribbon/on.png" data-layer="ribbon" data-sprite="on" style="z-index: 4;">
</div>
</character-sprite>| Hook | What it is |
|---|---|
[data-character="…"] |
The character, by id, like any sprite. Its position classes (center, left, right) and focus work as on a plain sprite. |
[data-content="wrapper"] |
The stack of layers. |
[data-layer="…"] |
One layer’s image, by layer name; its data-sprite is the picture it shows. Layers lower in the layers list have a lower z-index. |
Properties and attributes
| Name | Type | What it is |
|---|---|---|
character |
object |
The character’s declaration, with layers and layer_assets. |
directory |
string |
The folder of its pictures. |
width, height, ratio |
number |
The size of its largest picture, which the component measures to scale the layers. |
layers (state) |
object |
The picture and classes of each layer shown. Changing it updates the layers that changed: it adds, changes or removes their images, and leaves a layer playing its exit animation until the animation ends. |
The component scales itself again when the window changes size.
Events
None of its own.
Style it
The sprite stands on the bottom edge of the scene, 80svh tall (80% of the screen’s height), as wide as its pictures at that height, and hides whatever overflows it. Style a layer of one character in your game’s style/main.css:
character-sprite[data-character="y"] [data-layer="ribbon"] {
filter: drop-shadow(0 0 4px #ffd23f);
}Replace or extend it
The ShowCharacter and ShowCharacterLayer actions create the element with document.createElement('character-sprite'), set its character and directory, and change its layers state. Because the feature is experimental, prefer CSS over a subclass.
Related pages
- Character layers: showing and hiding layers.
- Characters: declaring characters and sprites.
- Game Screen: where sprites are placed.