Pi.js v2 delivers a major modernization with a complete architectural refactor, WebGL2-only rendering for GPU-accelerated performance, and a powerful new plugin system. This guide covers all the new features, improvements, and minimal breaking changes, ensuring a smooth transition to a faster, more extensible Pi.js.
INTRODUCTION
Pi.js v2 represents a major modernization of the library, featuring a complete architectural refactor, WebGL2-only rendering, and significant performance improvements. This guide covers all the new features, improvements, and breaking changes from v1 to v2.
MAJOR ARCHITECTURAL CHANGES
-
WEBGL2-ONLY RENDERING
- Complete migration from Canvas2D ImageData rendering to WebGL2
- GPU-accelerated rendering for significantly better performance
- Automatic batch rendering system for optimal draw call efficiency
- Framebuffer Object (FBO) architecture for off-screen rendering
- No Canvas2D fallback – WebGL2 is required (all modern browsers support it)
-
MODULAR ARCHITECTURE
- Complete refactor from monolithic codebase to modular ES6 structure
- Clean separation of concerns with dedicated modules for each feature
- Renderer layer separated from API layer for better maintainability
- Organized directory structure:
core/,renderer/,api/,text/
-
MODERN BUILD SYSTEM
- Migrated from legacy build tools to esbuild (100x faster builds)
- Multiple output formats: ESM (ES Modules), IIFE (browser)
- Source maps for debugging
- Tree-shaking support for smaller bundles
- Zero runtime dependencies
-
MODERN JAVASCRIPT
- Full ES2020+ support (
const/let, arrow functions, template literals, etc.) - Comprehensive JSDoc documentation throughout
- Consistent coding conventions
- Better error handling with typed errors
- Full ES2020+ support (
NEW FEATURES
-
PLUGIN SYSTEM
Official plugin registration system for extending Pi.js functionality. Register plugins using
pi.registerPlugin()with an initialization function. The plugin initialization function receives apluginApiobject with methods:pluginApi.addCommand()– Register new commandspluginApi.addScreenDataItem()– Add custom screen data propertiespluginApi.addScreenDataItemGetter()– Add computed screen data propertiespluginApi.addScreenInitFunction()– Run code when screens are createdpluginApi.addScreenCleanupFunction()– Run code when screens are destroyedpluginApi.getScreenData()– Access screen datapluginApi.getAllScreensData()– Access all screens datapluginApi.getApi()– Access the main Pi.js APIpluginApi.utils– Access utility functionspluginApi.wait()– Wait for async operationspluginApi.done()– Signal async operation completionpluginApi.registerClearEvents()– Register event clearing handlers
Plugins also support dependencies, and you can list registered plugins with
pi.getPlugins()and clear events withpi.clearEvents(). -
MULTIPLE OUTPUT FORMATS
- ESM format:
pi.esm.min.jsfor modern import statements - IIFE format:
pi.jsandpi.min.jsfor traditional<script>tags - Both formats available in minified and unminified versions
- ESM format:
-
BATCH RENDERING SYSTEM
- Automatic batching of draw operations for optimal GPU performance
- Multiple batch types:
POINTS_BATCH,IMAGE_BATCH,GEOMETRY_BATCH,LINES_BATCH - Automatic batch flushing and capacity management
- GPU-first rendering approach using vertices and shapes
-
ENHANCED TEXTURE MANAGEMENT
- Improved texture caching system
- Better memory management with automatic cleanup
- Support for
Imageelements,Canvaselements, and URL strings
PERFORMANCE IMPROVEMENTS
-
GPU-ACCELERATED RENDERING
- All drawing operations now use WebGL2 for hardware acceleration
- Significant performance improvements for complex scenes
- Better frame rates for games and animations
-
BATCH RENDERING
- Multiple draw operations batched into single GPU calls
- Reduced draw call overhead
- Optimal GPU utilization
-
OPTIMIZED COMMAND SYSTEM
- Pre-specialized functions that close over screen data
- Reduced branching in hot paths
- Better JIT optimization opportunities
- Monomorphic call sites for improved performance
-
FASTER BUILDS
- esbuild provides faster build times compared to legacy tools
- Quicker development iteration cycles
BREAKING CHANGES
-
REMOVED: Canvas2D Renderer
- WebGL2 is now the only rendering backend
- No fallback to Canvas2D
- All modern browsers support WebGL2
-
REMOVED:
render()Command- Rendering is now always automatic
- No manual render control needed
- Removed
setAutoRender()– always auto-renders
-
REMOVED: Non-Pixel Mode
- Pixel mode is now the only mode
- Removed
setPixelMode()command - Always uses pixel-perfect rendering
-
REMOVED: Canvas Font Support
- Only bitmap fonts are supported
- Removed canvas-based font rendering
- Uses bitmap/put fonts exclusively
-
REMOVED: Pen System
- Simplified to direct pixel writes
- Removed pen mode configuration
- Direct drawing operations only
-
REMOVED: Some Blend Modes
- Simplified blend mode system
- Some advanced blend modes removed
- GPU-based blending only
-
CHANGED: Plugin System
- New official plugin registration system via
pi.registerPlugin() - Plugins receive a
pluginApiobject with controlled access to internal APIs - More structured and secure than accessing internal APIs directly
- Supports plugin dependencies and proper initialization order
- New official plugin registration system via
API COMPATIBILITY
The public API remains largely compatible with v1.2.4. All major commands work mostly the same way:
- Graphics commands:
pset(),line(),rect(),circle(),ellipse(),arc(),bezier() - Image commands:
loadImage(),drawImage() - Color commands:
setPalColor(),findColor(), etc. - Screen commands:
screen(),cls(), etc. - Text commands:
print(),setFont(), etc. - Input commands:
key(),mouse(),touch(),gamepad(), etc.
The main differences are:
- Removed commands:
render(),setPixelMode(),setAutoRender() - Automatic rendering (no need to call
render()) - Always pixel mode (no need to set it)
- Some of the parameters have changed for commands; see the reference page for details.
MIGRATION GUIDE
-
REMOVE DEPRECATED CALLS
- Remove any
render()calls – rendering is now automatic - Remove
setPixelMode()calls – always pixel mode - Remove
setAutoRender()calls – always auto-renders
- Remove any
-
TEST YOUR CODE
- Most code should work without changes
- Test all graphics operations
- Verify performance improvements
- Check for any visual differences (should be minimal)
TECHNICAL DETAILS
BUILD SYSTEM
- Build tool: esbuild (replaces legacy uglify-js)
- Build time: ~100x faster
- Output formats: ESM, IIFE
- Source maps: Included for debugging
- Tree-shaking: Supported for ESM format
DEPENDENCIES
- Runtime: Zero dependencies (no external libraries required)
- Development: 4 dev dependencies (down from 6 in v1.2.4)
- esbuild – Fast bundler
- @playwright/test – Testing framework
- @iarna/toml – TOML parser
- pngjs – PNG image comparison
MODULE STRUCTURE
core/– Core system modules (commands, screen-manager, utils, plugins)renderer/– WebGL2 rendering system (renderer, batches, shaders, textures, draw/)api/– User-facing API commands (graphics, images, colors, pixels, paint, blends)text/– Text rendering modules (fonts, print)
RENDERING ARCHITECTURE
- WebGL2 context creation and management
- Framebuffer Object (FBO) for off-screen rendering
- Shader-based rendering with GLSL shaders
- Batch system for efficient GPU utilization
- Automatic batch flushing and display to canvas
WHAT’S NEXT
Future improvements planned for v2.x:
- Custom post image processing effects that apply affects to the screen before rendering to canvas
- Create a video plugin that supports playing of videos on a pi.js screen
- Create a pens plugin that can draw shapes instead of just pixels
- More comprehensive documentation
- Additional examples and tutorials
SUMMARY
- ✅ WebGL2-only rendering for better performance
- ✅ Modern build system with multiple output formats
- ✅ Modular architecture for better maintainability
- ✅ Plugin system for extensibility
- ✅ Improved gamepad and input handling
- ✅ Zero runtime dependencies
- ✅ Full ES2020+ JavaScript support
Breaking changes are minimal and mostly involve removing deprecated features. The public API remains largely compatible, making migration straightforward for most projects.
For detailed API documentation, visit: https://pijs.org/api
END OF UPGRADE GUIDE