Expand description
This crate adds PixiJS 2D canvas stages to Autumn apps. It works with Maud and htmx.
Add PixiPlugin to the app. Put pixi_stylesheet() and
pixi_script() in the page <head>. Then write a Stage:
use autumn_plugin_pixijs::{
Color, PixiPlugin, Shape, Sprite, Stage, Text, pixi_script, pixi_stylesheet,
};
use autumn_web::prelude::*;
#[get("/")]
async fn index() -> Markup {
html! {
html {
head { (pixi_stylesheet()) (pixi_script()) }
body {
(Stage::new()
.id("game")
.label("A coin game")
.background(Color::hex(0x0f172a))
.add(Text::new("Tap the coin").fill(Color::WHITE).position([400.0, 60.0]))
.add(Shape::star(5, 120.0, 50.0).fill(Color::hex(0xff7a18)).spin(30.0))
.add(Sprite::new("/static/img/coin.png")
.label("Gold coin")
.tap_post("/collect")
.tap_target("#game")
.tap_swap("outerHTML")))
}
}
}
}
autumn_web::app()
.plugin(PixiPlugin::new())
.routes(routes![index])
.run()
.await;§How it works
- The crate contains PixiJS 8.22.0 (MIT) and its
unsafe-evalpackage. It does not use npm or a bundler.PIXI_ASSETSserves them under/static/_plugins/pixi/. SRI covers every tag. - The builder renders
data-pixi-*attributes.init.jsreads them and builds the stage. You can also write the attributes by hand. init.jsscans on load, onhtmx:afterSwap, and on each DOM insertion. It builds a stage again when its declarations change. It frees a stage when its element leaves the document.- A tappable object sends a
pixi:tapevent from its declaration. An htmx trigger can use it. - The
pixi:readyevent gives your own JavaScript the application, the root container, and the globalPIXI.
§Limits
- The plugin uses WebGL or Canvas 2D. It does not use the WebGPU renderer.
- The declarative layer has no filters, masks, particles, or physics.
Use the
pixi:readyevent for custom code. - Each WebGL stage has its own context. Browsers keep about 16 contexts.
Renderer::Canvasuses no WebGL context. - Do not let user content keep
data-pixi-*orhx-*attributes. User markup could then start stages, load image URLs, and send requests.
Structs§
- Animated
Sprite - A frame animation from a PixiJS sprite sheet (
.jsonwith its image). - Color
- An sRGB color.
- Pixi
Plugin - Installs the PixiJS assets in an Autumn app.
- Shape
- A vector shape with a fill and an optional stroke.
- Sprite
- An image sprite.
- Stage
- A 2D stage. Renders one
<div data-pixi="stage">element. - Text
- A text object. The text is the content of the declaration element, so an htmx swap can change it.
- Tiling
Sprite - A repeated image that fills a rectangle. It can scroll.
- Vec2
- A 2D vector: position, scale, anchor, size, or scroll speed.
- Vendored
File - Provenance of one vendored upstream file.
Enums§
- Align
- Text alignment of a multi-line
Text. - Geometry
- The geometry of a
Shape. Sizes use logical pixels. - Renderer
- The renderer of a
Stage. - Stage
Object - One display object of a
Stage.
Constants§
- ASSETS_
NAMESPACE - URL namespace of the bundle. Files are served under
/static/_plugins/pixi/. - DEFAULT_
STAGE_ SIZE - Default logical stage size:
800 × 450. - MAX_
POLYGON_ POINTS - Maximum number of polygon corners. The builder drops the extra corners.
- MAX_
STAGE_ SIDE - Largest stage side, in logical pixels.
- PIXI_
VERSION - Pinned PixiJS version.
- PLUGIN_
NAME - The plugin name in Autumn diagnostics.
- VENDORED
- The vendored upstream files and their pins.
Statics§
- PIXI_
ASSETS - The plugin asset bundle.
Functions§
- pixi_
script - Renders the tags that load PixiJS and the plugin runtime.
- pixi_
stylesheet - Renders the
<link>tag for the plugin stylesheet.