![New axobject-query Maintainer Faces Backlash Over Controversial Decision to Support Legacy Node.js Versions](https://cdn.sanity.io/images/cgdhsj6q/production/86e6ebdea652d20da070ebbda20134b839972db7-1024x1024.webp?w=800&fit=max&auto=format)
Security News
New axobject-query Maintainer Faces Backlash Over Controversial Decision to Support Legacy Node.js Versions
A JavaScript library maintainer is under fire after merging a controversial PR to support legacy versions of Node.js.
path2d-polyfill
Advanced tools
Readme
Polyfills Path2D
api and roundRect
for CanvasRenderingContext2D
Add this script tag to your page to enable the feature.
<script lang="javascript" src="https://cdn.jsdelivr.net/npm/path2d-polyfill/dist/path2d-polyfill.min.js"></script>
or install from npm
npm install --save path2d-polyfill
and import with module bundler e.g. webpack before using the feature
import "path2d-polyfill";
This will polyfill the browser's window object with Path2D features and it will also polyfill roundRect if they are missing in both CanvasRenderingContexst and Path2D.
Example of usage
ctx.fill(new Path2D("M 80 80 A 45 45 0 0 0 125 125 L 125 80 Z"));
ctx.stroke(new Path2D("M 80 80 A 45 45 0 0 0 125 125 L 125 80 Z"));
It is possible to use this library in a node environment as well. The package exports a few functions that can be used:
Path2D
- class to create Path2D objects used by the polyfill methodspolyfillPath2D
- function that adds Path2D to a "window like" object and polyfills CanvasRenderingContext2D to use Path2DpolyfillRoundRect
- polyfills roundRect function on Path2D and CanvasRenderingContext2D (missing in firefox)parsePath
- function for parsing an SVG path string into canvas commandsuse any of these functions like:
const { polyfillRoundRect } = require "path2d-polyfill";
const windowlike = { CanvasRenderingContext2D, Path2D };
polyfillRoundRect(windowLike);
// roundRect functions has now been added if they were missing
To get Path2D features with the node-canvas library use the following pattern:
const { createCanvas, CanvasRenderingContext2D } = require("canvas");
const { polyfillPath2D } = require("path2d-polyfill/path2d");
global.CanvasRenderingContext2D = CanvasRenderingContext2D;
polyfillPath2D(global);
// Path2D has now been added to global object
const canvas = createCanvas(200, 200);
const ctx = canvas.getContext("2d");
const p = new Path2D("M10 10 l 20 0 l 0 20 Z");
ctx.fillStyle = "green";
ctx.fill(p);
A working example of a node express server that serves an image drawn with canvas can be seen here
Method | Supported |
---|---|
constructor(SVGPath) | Yes |
addPath() | Yes |
closePath() | Yes |
moveTo() | Yes |
lineTo() | Yes |
bezierCurveTo() | Yes |
quadraticCurveTo() | Yes |
arc() | Yes |
ellipse() | Yes |
rect() | Yes |
roundRect() | Yes |
Clone this repo and run the following
yarn
yarn start
open http://localhost:10001 to see the example page.
Recommended to use vscode with the prettier extension to keep formatting intact.
FAQs
Polyfills Path2D api for canvas rendering
The npm package path2d-polyfill receives a total of 601,645 weekly downloads. As such, path2d-polyfill popularity was classified as popular.
We found that path2d-polyfill demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
A JavaScript library maintainer is under fire after merging a controversial PR to support legacy versions of Node.js.
Security News
Results from the 2023 State of JavaScript Survey highlight key trends, including Vite's dominance, rising TypeScript adoption, and the enduring popularity of React. Discover more insights on developer preferences and technology usage.
Security News
The US Justice Department has penalized two consulting firms $11.3 million for failing to meet cybersecurity requirements on federally funded projects, emphasizing strict enforcement to protect sensitive government data.