@emeraldproductions/fwb
v0.1.3
Published
The **Emerald Whiteboard Protocol** library is a WebAssembly-powered toolkit designed for high-performance manipulation of pixel grids, drawing primitives, and managing a color shader. Ideal for collaborative whiteboard applications or pixel-based graphic
Readme
@emeraldproductions/fwb
The Emerald Whiteboard Protocol library is a WebAssembly-powered toolkit designed for high-performance manipulation of pixel grids, drawing primitives, and managing a color shader. Ideal for collaborative whiteboard applications or pixel-based graphics rendering.
Features
- PixelBox: Manage 2D grids of pixels with flexible operations.
- Color Shader: Up to 16 colors, customizable for any use case.
- Drawing Functions: Draw dots, lines, and apply transformations.
- Efficient Encoding: Convert PixelBox to/from raw buffers.
- WebAssembly: Cross-platform performance with easy JavaScript integration.
Installation
Install via NPM:
npm install @emeraldproductions/fwbUsage
Import the Library
import { PixelBox, RGBA, ProtocolMessage } from "@emeraldproductions/fwb";API Documentation
PixelBox
The core structure for managing 2D pixel grids.
Constructor
const pixelBox = new PixelBox(width: number, height: number);width: Width of the pixel grid (32-bit integer).height: Height of the pixel grid (32-bit integer).
Methods
fromRGBA(rgba: Uint8Array): void- Converts an RGBA buffer to a
PixelBox. - Example:
pixelBox.fromRGBA(new Uint8Array([255, 0, 0, 255, ...])); // Red pixels
- Converts an RGBA buffer to a
toRGBA(): Uint8Array- Converts the
PixelBoxback to an RGBA buffer.
- Converts the
setPixel(x: number, y: number, color: number): void- Sets a single pixel at
(x, y)to a color from the shader palette.
- Sets a single pixel at
getPixel(x: number, y: number): number- Retrieves the color index of the pixel at
(x, y).
- Retrieves the color index of the pixel at
setShader(shader: Uint8Array): boolean- Updates the color shader palette. Must be exactly 48 bytes (16 colors × 3 RGB bytes each).
Example:
const shader = new Uint8Array([ 0, 0, 0, // Black 255, 255, 255, // White ... // Other colors ]); pixelBox.setShader(shader);clear(): void- Clears the entire pixel grid.
drawDot(x: number, y: number, radius: number, color: number): void- Draws a filled circle (dot) at
(x, y)with a givenradiusandcolor.
Example:
pixelBox.drawDot(50, 50, 5, 3); // A dot at (50, 50) with radius 5, color index 3- Draws a filled circle (dot) at
drawLine(x1: number, y1: number, x2: number, y2: number, radius: number, color: number): void- Draws a line from
(x1, y1)to(x2, y2)with optional thickness (radius).
- Draws a line from
apply(pb: PixelBox, x: number, y: number): void- Overlays another
PixelBoxonto the current one starting at(x, y).
- Overlays another
ProtocolMessage
Manages protocol-based communication.
Constructor
const message = new ProtocolMessage(instruction: number, payload_type: number);instruction: An 8-bit instruction for the message.payload_type: Defines the payload type (0for raw,1for vectors,2for PixelBox).
Methods
addBuffer(payload: Uint8Array): void- Appends raw data to the message payload.
buffer (getter)- Retrieves the message as a byte array.
fromBuffer(bytes: Uint8Array): ProtocolMessage- Parses a message from a byte array.
RGBA
A utility class for working with RGBA colors.
Constructor
const rgba = new RGBA(r: number, g: number, b: number, a: number);r,g,b,a: Red, Green, Blue, and Alpha values (0–255).
Methods
to_rgb(): RGB- Converts the RGBA value to an RGB object (ignoring alpha).
Vector
Represents a vector-based drawing primitive.
Constructor
const vector = new Vector(layer, tool, color, modifier, start_x, start_y, end_x, end_y);layer: The drawing layer.tool: The drawing tool identifier.color: Color index from the shader palette.modifier: Tool-specific modifier.start_x,start_y: Starting coordinates.end_x,end_y: Ending coordinates.
Examples
Drawing a Circle
const pixelBox = new PixelBox(100, 100);
pixelBox.drawDot(50, 50, 10, 2); // Draw a circle at (50, 50) with radius 10, color 2Setting a Custom Shader
const shader = new Uint8Array([
0, 0, 0, // Black
255, 255, 255, // White
128, 128, 128, // Gray
... // 13 more colors
]);
pixelBox.setShader(shader);License
This project is licensed under the MIT License.
Let me know if you need further adjustments!
