Registry / serialization / path2d

path2d

JSON →
library0.3.1jsnpmunverified

This `path2d` package provides a server-side implementation of the standard browser `Path2D` API, along with `CanvasRenderingContext2D.roundRect()`, for use in Node.js environments. It enables developers to utilize modern Canvas Path2D features and methods like `arc()`, `ellipse()`, and `roundRect()` when performing server-side rendering with libraries such as `node-canvas`. The current stable version is `0.3.1`, and updates appear to be released on an as-needed basis for feature enhancements (e.g., `DomPoints` support for `roundRect`) and dependency maintenance. Its primary differentiation is bringing browser-standard canvas path construction to Node.js, ensuring consistent rendering logic across client and server-side contexts for applications that generate images programmatically.

npm install path2d
INSTALL
IMPORT
SIG · PATH2D
P
path2d
serializationjavascriptv0.3.1
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Path2D
import { Path2D } from 'path2d';
const Path2D = require('path2d'); // This is incorrect for named exports if not destructured
Main class for creating Path2D objects. For CommonJS, use `const { Path2D } = require('path2d');`.
applyPath2DToCanvasRenderingContext
import { applyPath2DToCanvasRenderingContext } from 'path2d';
import applyPath2DToCanvasRenderingContext from 'path2d'; // Not a default export
This function extends a CanvasRenderingContext prototype (e.g., from `node-canvas`) with Path2D features.
applyRoundRectToCanvasRenderingContext2D
import { applyRoundRectToCanvasRenderingContext2D } from 'path2d';
This function extends a CanvasRenderingContext2D prototype with the `roundRect` method. Can be used independently or alongside `applyPath2DToCanvasRenderingContext`.
parsePath
import { parsePath } from 'path2d';
Utility function for parsing SVG path strings into canvas commands, useful for custom implementations.

Demonstrates how to apply Path2D and roundRect polyfills to a CanvasRenderingContext2D (from `node-canvas` or a similar implementation) and then use them to draw shapes.

import { createCanvas } from 'canvas'; import { Path2D, applyPath2DToCanvasRenderingContext, applyRoundRectToCanvasRenderingContext2D } from 'path2d'; // Create a mock CanvasRenderingContext2D to apply the polyfills // In a real scenario, this would be `CanvasRenderingContext2D` from 'canvas' class MockCanvasRenderingContext2D { constructor() { this.calls = []; } beginPath() { this.calls.push('beginPath'); } moveTo(x, y) { this.calls.push(`moveTo(${x},${y})`); } lineTo(x, y) { this.calls.push(`lineTo(${x},${y})`); } arc(x, y, r, sa, ea) { this.calls.push(`arc(${x},${y},${r})`); } closePath() { this.calls.push('closePath'); } fill(path) { this.calls.push(`fill(${path instanceof Path2D ? 'Path2D' : 'currentPath'})`); } stroke(path) { this.calls.push(`stroke(${path instanceof Path2D ? 'Path2D' : 'currentPath'})`); } } // Apply Path2D and roundRect polyfills to the mock context applyPath2DToCanvasRenderingContext(MockCanvasRenderingContext2D); applyRoundRectToCanvasRenderingContext2D(MockCanvasRenderingContext2D); // Example using node-canvas (uncomment and install 'canvas' to run) // const { createCanvas, CanvasRenderingContext2D } = require('canvas'); // applyPath2DToCanvasRenderingContext(CanvasRenderingContext2D); // applyRoundRectToCanvasRenderingContext2D(CanvasRenderingContext2D); // const canvas = createCanvas(400, 300); // const ctx = canvas.getContext('2d'); const ctx = new MockCanvasRenderingContext2D(); // Using mock for independent demonstration ctx.fillStyle = 'blue'; // Create a Path2D object from an SVG string const svgPath = new Path2D('M10 10 H100 V100 H10 L10 10'); ctx.fill(svgPath); // Draw a rounded rectangle using the polyfilled method ctx.beginPath(); ctx.roundRect(150, 50, 100, 75, 10); ctx.fillStyle = 'red'; ctx.fill(); console.log('Path2D and roundRect polyfills applied.'); console.log('Context calls:', ctx.calls); // If using node-canvas, you could then save the output: // import { writeFileSync } from 'fs'; // writeFileSync('./output.png', canvas.toBuffer('image/png'));
Debug
Known issues
gotchaThe `Path2D` class and `roundRect` method are not automatically available on a `CanvasRenderingContext2D` instance after installation. You must explicitly apply the polyfills using `applyPath2DToCanvasRenderingContext` and `applyRoundRectToCanvasRenderingContext2D` to your `CanvasRenderingContext2D` prototype.
fix
Call `applyPath2DToCanvasRenderingContext(CanvasRenderingContext2D);` and/or `applyRoundRectToCanvasRenderingContext2D(CanvasRenderingContext2D);` before using Path2D features or `ctx.roundRect`.
affects: >=0.1.0
gotchaThe `path2d` package is the core implementation. There's also `path2d-polyfill`, which is a wrapper around `path2d`. Ensure you're installing and importing from the correct package (`path2d`) if you intend to use the core library directly.
fix
Verify your `package.json` and import statements use `'path2d'` if you want the direct implementation.
affects: >=0.1.0
gotchaWhile the package explicitly states `node` engines `'>=6'` and later removed the `engines` field in `path2d-polyfill@3.1.1` (which depends on `path2d`), compatibility with very old or unmaintained Node.js versions might not be guaranteed for future updates. Users should ideally run on actively supported Node.js versions.
fix
Ensure your Node.js environment is a currently supported LTS version to guarantee long-term compatibility and security.
affects: >=0.1.0
Errors
Common errors & fixes
TypeError: Path2D is not a constructor
The `Path2D` class was not imported or destructured correctly, or the `applyPath2DToCanvasRenderingContext` function was not called on the `CanvasRenderingContext2D` prototype.
fix
Ensure `import { Path2D, applyPath2DToCanvasRenderingContext } from 'path2d';` is used, and `applyPath2DToCanvasRenderingContext(CanvasRenderingContext2D);` has been executed for your context.
TypeError: ctx.roundRect is not a function
The `roundRect` method was not applied to the `CanvasRenderingContext2D` prototype.
fix
Call `applyRoundRectToCanvasRenderingContext2D(CanvasRenderingContext2D);` after importing, and before attempting to use `ctx.roundRect`.
ReferenceError: createCanvas is not defined
This error typically occurs when using the `node-canvas` library without properly importing or installing it.
fix
Install `canvas` via `npm install canvas` and ensure you are importing `createCanvas` from it: `import { createCanvas } from 'canvas';` (ESM) or `const { createCanvas } = require('canvas');` (CJS).
Upgrade
Version history
0.3.1latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
2 hits · last 30 days
node
2
Resources
path2d — npm install path2d · libregistry