miglib
v2.0.8
Published
A TypeScript graphics and game development library built on p5.js: rendering, particle systems, vector/color math, sprite animation, tilesets, input, and audio.
Maintainers
Readme
MigLib
MigLib is a TypeScript graphics and game development library built on p5.js, providing utilities for rendering, particle systems, vector math, color manipulation, UI elements, and more.
Features
- Screen-based architecture for organizing game/app content
- Separate update and render phases each frame
- Powerful graphics and particle system
- Vector and color math utilities
- Asset and sound management
- Render-order mouse routing with pointer capture
- Gamepad and input helpers
- Easy p5.js integration
Quick Example
import { Starter, MigScreen, Graphics, Assets, Color } from 'miglib';
class MyScreen implements MigScreen {
onShow(graphics: Graphics) {
// Called when this screen is shown
}
onUpdate(graphics: Graphics) {
// Optional. Game logic for this frame, run before onRender
}
onRender(graphics: Graphics) {
graphics.renderText("Hello World", 100, 100, {
size: 24,
color: Color.col(255, 0, 0)
});
}
}
// Make sure the p5.js script is loaded globally before this runs.
// Starter creates and wires the p5 instance for you — you don't write
// p5's preload/setup/draw callbacks yourself.
const game = new Starter((assets: Assets) => {
assets.loadImage("myImage", "myImage.png"); // resolved relative to the assets/ folder
return new MyScreen();
});
game.start(); // optionally pass a container element: game.start(myDiv)Example: Mouse Claims
Widgets are drawn by you, then claim their rect. The last claim under the cursor wins, so draw order is priority. A press captures that claim until the button comes up.
onRender(graphics: Graphics) {
const rect = { x: 100, y: 100, w: 120, h: 40 };
graphics.sketch.rect(rect.x, rect.y, rect.w, rect.h);
graphics.mouse.claim("my-button", rect, {
// There is no onClick: a click is a release inside, without a drag.
onRelease: (info) => { if (info.inside && !info.wasDrag) this.doThing(); },
onHover: () => graphics.renderText("tooltip", rect.x, rect.y - 6),
});
}- Claim inside the
push()/translate()/scale()block that positions the widget — the rect is in that space and the cursor is mapped through the live canvas transform for you. graphics.mouse.pushClip(rect)/popClip()restrict hits to a region; nested clips intersect.- Per-frame state lives on the same object:
graphics.mouse.justPressed,justReleased,isPressed,wheelDelta,wheelUp,wheelDown. - Query helpers:
captured,hovered,isDragging,capturedBy(id),isHovered(id),isInside(id).Starterdrives the router; screens never callbegin()/end().
Example: Sprite Animation
const animator = new Animator();
animator.addAnimation(
"walk",
"character_spritesheet",
[
{ x: 0, y: 0, w: 32, h: 32, duration: 100 },
{ x: 32, y: 0, w: 32, h: 32, duration: 100 },
{ x: 64, y: 0, w: 32, h: 32, duration: 100 },
{ x: 96, y: 0, w: 32, h: 32, duration: 100 }
]
);
animator.play("walk");
// Advance playback in your screen's onUpdate...
animator.update(graphics.deltaTime);
// ...and draw in onRender. render() never advances playback, so the frame you
// read with getCurrentFrameIndex() is the frame that gets drawn.
animator.render(100, 100, graphics, { w: 64, h: 64 });Example: Playing Sounds
Sound.load("shoot", "sounds/shoot.mp3");
Sound.play("shoot");More Documentation
See the full API and advanced usage in DOCS.md.
