@theresav/graphics
v0.2.6
Published
Unified high-performance 2D graphics engine in Rust for Node.js and Bun
Maintainers
Readme
TheresaV Graphics (@theresav/graphics)
Unified, ultra-high-performance, security-hardened 2D graphics engine written in pure Rust for Node.js and Bun.
TheresaV Graphics unifies the capabilities of Jimp (fluent, intuitive image manipulation), Sharp (high-throughput parallel image processing), and HTML5 Canvas 2D (software vector rasterization & typography) into one cohesive Rust core and one public package.
Key Features
- ⚡ High Throughput: Multi-core row parallelization powered by Rayon, SIMD-aligned contiguous memory streaming, and Link-Time Optimization (LTO).
- 🛡️ Enterprise Security: Decompression bomb mitigation, RAII deterministic memory quotas, magic byte validation, and a strict SVG sandbox rejecting scripts and external entities.
- 🎨 Full Canvas 2D Rendering: Complete software Canvas 2D API (Paths, Bézier curves, arcs, linear & radial gradients, alpha compositing, software anti-aliasing scanline rasterizer, and TTF/OTF typography).
- 🖼️ Fluent Image Pipeline: Resize (Bilinear, Nearest, Lanczos3), Crop, Rotate, Flip, Flop, Blur (Gaussian), Sharpen (Unsharp Mask), Color Adjustments, Tint, Invert, Posterize, and Pixelate.
- 📦 Unified Codecs: Native support for PNG, JPEG, WebP, BMP, TIFF, GIF, and SVG rendering.
- 🎬 Animation Engine: Multi-frame animated GIF decoding and encoding with frame quota enforcement.
- 🌐 Node.js & Bun Native: Zero C++ dependencies, 100% safe Rust where possible, cross-runtime compatibility across Node.js and Bun.
Installation
# Using Bun
bun add @theresav/graphics
# Using npm
npm install @theresav/graphics
# Using pnpm or yarn
pnpm add @theresav/graphics
yarn add @theresav/graphicsQuick Start & Code Examples
1. Fluent Image Processing Pipeline
import { Graphics } from "@theresav/graphics";
// Open an image from file or Buffer
const image = await Graphics.open("./input.png");
// Or from memory: const image = await Graphics.fromBuffer(buffer);
// Fluent processing pipeline
const outputBuffer = await image
.resize(1200, 630)
.grayscale()
.brightness(1.1)
.contrast(1.2)
.sharpen(1.0, 1.5)
.toBuffer("webp", { quality: 85 });
// Save directly to file
await image.toFile("./output.webp", "webp", { quality: 85 });2. Canvas 2D Drawing & Typography
import { Graphics } from "@theresav/graphics";
import fs from "fs";
// Create an 800 x 600 Canvas
const canvas = Graphics.createCanvas(800, 600);
const ctx = canvas.getContext("2d");
// Draw solid background
ctx.fillStyle = "#1e1e2e";
ctx.fillRect(0, 0, 800, 600);
// Create Linear Gradient
const gradient = ctx.createLinearGradient(100, 100, 400, 400);
gradient.addColorStop(0, "#ff5555");
gradient.addColorStop(1, "#50fa7b");
// Draw closed vector paths & Bézier curves
ctx.beginPath();
ctx.moveTo(100, 100);
ctx.bezierCurveTo(200, 50, 300, 250, 400, 150);
ctx.lineTo(400, 400);
ctx.closePath();
ctx.fillStyle = gradient;
ctx.fill();
// Draw circle (Arc)
ctx.beginPath();
ctx.arc(600, 300, 80, 0, Math.PI * 2);
ctx.fillStyle = "#8be9fd";
ctx.fill();
// Draw text with TrueType/OpenType typography
ctx.fillStyle = "#ffffff";
ctx.fillText("TheresaV Graphics Engine", 50, 520);
// Export to buffer
const pngBuffer = await canvas.toBuffer("png");
fs.writeFileSync("./canvas_output.png", pngBuffer);3. Image Compositing & Watermarking
import { Graphics } from "@theresav/graphics";
const baseImage = await Graphics.open("./background.jpg");
const watermark = await Graphics.open("./logo.png");
// Resize watermark and composite onto bottom right corner
watermark.resize(150, 150);
const posX = baseImage.width - watermark.width - 20;
const posY = baseImage.height - watermark.height - 20;
baseImage.composite(watermark, posX, posY, 0.85); // 85% opacity
await baseImage.toFile("./watermarked.jpg", "jpeg", { quality: 90 });4. Secure SVG Rendering
import { Graphics } from "@theresav/graphics";
const svgString = `
<svg width="400" height="400" xmlns="http://www.w3.org/2000/svg">
<rect width="100%" height="100%" fill="#282a36"/>
<circle cx="200" cy="200" r="100" fill="#bd93f9"/>
</svg>
`;
// Strict mode: external entity resolution and scripts are rejected
const renderedBuffer = await Graphics.renderSvg(svgString);5. Animated GIF Creation
import { Graphics } from "@theresav/graphics";
// Prepare frame buffers
const frame1 = await Graphics.open("./frame1.png").then(img => img.toBuffer("png"));
const frame2 = await Graphics.open("./frame2.png").then(img => img.toBuffer("png"));
const gifBuffer = await Graphics.createAnimatedGif(
[frame1, frame2],
200, // delay between frames in milliseconds (200ms)
500, // width
500 // height
);6. Security Profiles & Memory Quotas
TheresaV Graphics provides three built-in kernel-level security profiles:
import { Graphics } from "@theresav/graphics";
// 1. Standard: For local trusted applications
const standardEngine = new Graphics({ security: "standard" });
// 2. Strict: For public APIs and user uploads
const strictEngine = new Graphics({
security: "strict",
memoryLimit: 128 * 1024 * 1024 // 128MB maximum allocation quota
});
// 3. Hardened: For untrusted files and multi-tenant environments
const hardenedEngine = new Graphics({
security: "hardened",
memoryLimit: 64 * 1024 * 1024 // 64MB tight limit
});
const safeImage = await strictEngine.fromBuffer(untrustedUploadBuffer);Performance Benchmarks
Benchmarked on Linux x86_64, Node.js v26.13.0, 500x500 RGBA8 test image, 800x600 Canvas (50 iterations).
| Operation | TheresaV Graphics (Rust) | Sharp (libvips C/C++) | Jimp (Pure JavaScript) | TheresaV Advantage | |:---|:---|:---|:---|:---| | Canvas 2D Rendering | 1,076.4 op/s (0.93 ms) | N/A | N/A | 14.6x Faster than @napi-rs/canvas (73.5 op/s) | | Gaussian Blur | 183.3 op/s (5.46 ms) | 152.7 op/s (6.55 ms) | 15.7 op/s (63.89 ms) | 1.2x Faster than Sharp (11.7x faster than Jimp) | | Sharpen | 180.5 op/s (5.54 ms) | 95.1 op/s (10.52 ms) | N/A | 1.9x Faster than Sharp | | Resize | 468.9 op/s (2.13 ms) | 186.9 op/s (5.35 ms) | 151.0 op/s (6.62 ms) | 2.5x Faster than Sharp (3.1x faster than Jimp) | | Crop | 2,524.5 op/s (0.40 ms) | 710.8 op/s (1.41 ms) | 466.6 op/s (2.14 ms) | 3.5x Faster than Sharp (5.4x faster than Jimp) | | PNG Codec Pipeline | 1,110.8 op/s (0.90 ms) | 460.4 op/s (2.17 ms) | 218.8 op/s (4.57 ms) | 2.4x Faster than Sharp (5.1x faster than Jimp) |
License
Distributed under the MIT OR Apache-2.0 license.
