Core Libraries
Monogatari 2.9.0 is built on two libraries of the Aegis Framework, by the same author:
- Artemis 0.6 for the page and the browser: finding and changing elements, storage, requests, the device.
- Pandora 0.5 for components: the custom elements every piece of interface is made of.
Everything Artemis provides is on the Monogatari global, so your game can use it with no extra file. Pandora isn’t on it directly: you use it through Monogatari’s component classes.
Artemis: the page and the browser
On Monogatari |
What it’s for |
|---|---|
$_(selector) |
Finds elements and wraps them in a DOM object with methods to change them. See Change the page with Artemis. |
$_ready(callback) |
Runs callback once the page has loaded. js/main.js starts the game with it. |
$_create('div', { class: 'note' }) |
Creates an element with those attributes, wrapped in a DOM object. |
Platform |
Facts about the device and the browser. See Know the device. |
Debug, DebugLevel |
The console, with levels. The engine’s monogatari.debug is Debug. See The console. |
Text |
Text helpers: Text.capitalize(), Text.truncate(text, length), Text.friendly() ('Rose Garden' becomes 'rose-garden'), Text.isBlank(). |
Util |
Util.uuid(), Util.debounce(function, delay), Util.throttle(function, limit), and Util.callAsync(function, context, …arguments), which calls a function and always returns a promise. |
Space, SpaceAdapter |
Key-value storage on top of localStorage, sessionStorage, IndexedDB or a server. The engine keeps saves and preferences in one: monogatari.Storage. |
Request |
fetch() with helpers: Request.json(url), Request.text(url), Request.post(url, data). |
Preload |
Loading files ahead of time: Preload.image(url), Preload.file(url), Preload.font(url). |
FileSystem |
Files the player picks or downloads: FileSystem.read(file) reads a file the player chose, FileSystem.create(name, content) makes one and FileSystem.download(file) saves it, FileSystem.humanSize(bytes) writes a size like “1.5 MB”. |
Form |
Helpers for a <form data-form="name">: Form.values('name'), Form.fill('name', values), Form.reset('name'). |
Change the page with Artemis
Monogatari.$_('selector') finds every element matching a CSS selector. monogatari.element() returns the game’s <visual-novel> element the same way, and .find() searches inside it, which is how the examples in this section reach the game screen:
const gameScreen = this.engine.element().find('[data-screen="game"]');
gameScreen.style('filter', 'brightness(3)');The DOM object’s methods change every element it holds and return the object, so calls can follow one another. The ones extensions use most:
| Method | What it does |
|---|---|
find(selector), closest(selector), parent(), children() |
Moves to other elements. |
get(0), first(), last(), each(callback), exists() |
Reaches the elements it holds. get(0) is the first one as a plain element. |
append(content), prepend(content), remove(), empty() |
Adds HTML or an element inside, or takes elements off the page. |
text(value), html(value) |
Sets the text or the HTML. Without an argument, returns the first element’s. |
addClass(name), removeClass(name), toggleClass(name), hasClass(name) |
Changes classes. |
attribute(name, value), data(name, value), style(property, value) |
Sets an attribute, a data- attribute or a style. Without a value, returns it. |
show(), hide(), isVisible(), fadeIn(), fadeOut() |
Shows and hides. |
on(events, selector, callback), off(events, callback) |
Listens to events, from the elements themselves or, with a selector, from elements inside them. |
Know the device with Platform
Monogatari.Platform answers questions about where the game runs. Most are properties; mobile() and desktop() are methods that take an optional system name:
if (Monogatari.Platform.mobile()) {
// A phone or a tablet
}
if (Monogatari.Platform.mobile('iOS')) {
// An iPhone
}| Property or method | true when |
|---|---|
mobile(), mobile('Android'), mobile('iOS'), mobile('iPadOS') |
The browser runs on a phone or a tablet, or on that system. |
desktop(), desktop('Windows'), desktop('macOS'), desktop('Linux') |
The browser runs on a computer, or on that system. |
touch |
The screen takes touch. |
orientation |
'portrait' or 'landscape', rather than true. |
darkMode, reducedMotion |
The player asked their system for a dark theme, or for less motion. |
electron, electrobun, desktopApp |
The game runs as a desktop app. |
standalone |
The game was installed and opened like an app. |
The console with Debug
monogatari.debug writes to the browser’s console only at or above the level monogatari.debug.level() set, which js/main.js sets to 5 in the starter game:
| Method | Writes at level |
|---|---|
monogatari.debug.error() |
1 and above |
monogatari.debug.warn() |
2 and above |
monogatari.debug.info() |
3 and above, with the debug module loaded |
monogatari.debug.log(), monogatari.debug.debug() |
4 and above, with the debug module loaded |
Without engine/debug/debug.js, as in a released game, only error() and warn() print: the rest of the methods (info(), log(), debug(), the groups, tables and timers) print nothing. Set the level to 0 as well and the console stays quiet. The engine’s error dialogs need a level above 0 too, as well as engine/debug/debug.js, which starts the level at 1.
Pandora: components
Pandora gives every component its life cycle, its props and state, its rendering and its templates; Monogatari’s Component, ShadowComponent, ScreenComponent and MenuComponent add the engine to it. Component Life Cycle and Component Reference document what Pandora 0.5 provides, as Monogatari 2.9.0 uses it.
The rest of Pandora isn’t on the Monogatari global: its Registry, which the engine uses to define the components, and the html template function, so a component’s render() returns a string.
Since 2.9.0 the engine bundles Pandora 0.5.1, built with lit-html’s production build. Earlier versions bundled the development build, so the console showed “Lit is in dev mode. Not recommended for production!” in every game.
Kayros: the CSS
Kayros 0.5, the Aegis Framework’s CSS library, is built into engine/core/monogatari.css: its layout classes and its look for buttons, inputs and modals. Style and Design covers what your game can use from it.
Other libraries the engine bundles
On Monogatari |
What it is |
|---|---|
Luxon |
Luxon 3, for dates and times: Monogatari.Luxon.DateTime.now(). The save slots use it. |
RandomJS |
random-js, the random numbers behind monogatari.random(min, max). |
tsParticles, loadSlim |
tsParticles 3, behind the particles statement. |
AudioPlayer |
The engine’s player for music, sound and voice, on the Web Audio API. |
FancyError |
The error dialogs: see Reporting problems. |
Font Awesome 7 (the free icons) is in the engine too. Its script replaces every element with icon classes, like <span class="fas fa-arrow-left"></span>, with the icon’s SVG, so you can use any of its free icons in your own markup.
Related
- Components: writing a component on Monogatari’s base classes.
- Engine API: the
monogatariobject. - Style and Design: the CSS.