pixfx
Pixel-art effects written once in Rust. Render them to sprite sheets, or play the same effects live in the browser, Phaser and gpui. Every spell in the duel above is one of them.
The duel above is drawn live by the engine, every frame.
Where it runs
Pick the layer that fits your project. All of them share one engine and one effect registry.
Write a sprite sheet (composite, colour and light PNGs plus beanfx-layout JSON), or a looping GIF.
cargo run -p pixfx-export --example sprite_sheet -- fire/campfire --out out
cargo run -p pixfx-export --example gif -- fire/campfire --scale 4 --out out
Effects are painted on worker threads and drawn on a Canvas 2D context, upscaled by a whole number with no smoothing, among your own drawing.
import { createPixfx, loadPixfx } from "pixfx-canvas2d";
await loadPixfx();
const pixfx = createPixfx(canvas.getContext("2d"), { scale: 4 });
pixfx.play("fire/campfire", 64, 112, { seed: 3 });
// each frame: pixfx.update(dt), then pixfx.draw()
A scene plugin. Effects show up as ordinary Phaser images, a back pair and a front pair, so sprites can stand inside the fire.
import { PixfxPlugin, loadPixfx } from "pixfx-phaser";
await loadPixfx();
// plugins: { scene: [{ key: "pixfx", plugin: PixfxPlugin, mapping: "pixfx" }] }
const fire = this.pixfx.play("fire/campfire", 150, 170);
fire.follow(player, 0, -4);
Drive an effect yourself and read its pixels: RGBA bytes, or the colour and light layers separately.
import init, { Fx } from "./pkg/pixfx_wasm.js";
await init();
const fx = new Fx("fire/candle", { seed: 1 });
fx.update(1 / 60);
fx.draw("both");
ctx.putImageData(new ImageData(fx.rgba(), fx.width(), fx.height()), 0, 0);
Render frames in Rust, hash them, composite them or export them.
use pixfx_core::{Options, render_frames};
let fx = pixfx_effects::effect("fire/brazier").ok_or("not registered")?;
let rendered = render_frames(fx, &Options::default(), 0);
pixfx_export::write_gif(Path::new("brazier.gif"), &rendered, 4)?;
Paint effects inside a gpui window, light blended additively on the GPU with nearest sampling.
let mut view = FxView::new(campfire, Options::default());
view.tick(dt);
paint_fx(&mut view, &mut frames, Layer::Both, origin, 4, window)?;
frames.end_frame(window)?;
Try it in a minute
-
Clone the repository.
rust-toolchain.tomlpins the nightly and the wasm target, so rustup fetches them on first build. -
Render the campfire to a GIF:
cargo run -p pixfx-export --example gif -- fire/campfire --out out -
Or play it in the browser through the Canvas 2D demo:
cd packages/pixfx-web && bun install && bun run build:wasm cd ../pixfx-canvas2d && bun install && bunx vite demo