Security News
The Push to Ban Ransom Payments Is Gaining Momentum
Ransomware costs victims an estimated $30 billion per year and has gotten so out of control that global support for banning payments is gaining momentum.
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://unpkg.com/path2d-polyfill"></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
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
Ransomware costs victims an estimated $30 billion per year and has gotten so out of control that global support for banning payments is gaining momentum.
Application Security
New SEC disclosure rules aim to enforce timely cyber incident reporting, but fear of job loss and inadequate resources lead to significant underreporting.
Security News
The Python Software Foundation has secured a 5-year sponsorship from Fastly that supports PSF's activities and events, most notably the security and reliability of the Python Package Index (PyPI).