New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

canvas-confetti

Package Overview
Dependencies
Maintainers
1
Versions
37
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

canvas-confetti - npm Package Compare versions

Comparing version

to
1.8.0

98

dist/confetti.browser.js

@@ -1,2 +0,2 @@

// canvas-confetti v1.7.0 built on 2023-10-02T13:33:46.734Z
// canvas-confetti v1.8.0 built on 2023-10-03T21:26:00.980Z
!(function (window, module) {

@@ -16,2 +16,4 @@ // source content

var canUsePaths = typeof Path2D === 'function' && typeof DOMMatrix === 'function';
function noop() {}

@@ -348,5 +350,16 @@

context.fillStyle = 'rgba(' + fetti.color.r + ', ' + fetti.color.g + ', ' + fetti.color.b + ', ' + (1 - progress) + ')';
context.beginPath();
if (fetti.shape === 'circle') {
if (canUsePaths && fetti.shape.type === 'path' && typeof fetti.shape.path === 'string' && Array.isArray(fetti.shape.matrix)) {
context.fill(transformPath2D(
fetti.shape.path,
fetti.shape.matrix,
fetti.x,
fetti.y,
Math.abs(x2 - x1) * 0.1,
Math.abs(y2 - y1) * 0.1,
Math.PI / 10 * fetti.wobble
));
} else if (fetti.shape === 'circle') {
context.ellipse ?

@@ -632,2 +645,82 @@ context.ellipse(fetti.x, fetti.y, Math.abs(x2 - x1) * fetti.ovalScalar, Math.abs(y2 - y1) * fetti.ovalScalar, Math.PI / 10 * fetti.wobble, 0, 2 * Math.PI) :

function transformPath2D(pathString, pathMatrix, x, y, scaleX, scaleY, rotation) {
var path2d = new Path2D(pathString);
var t1 = new Path2D();
t1.addPath(path2d, new DOMMatrix(pathMatrix));
var t2 = new Path2D();
// see https://developer.mozilla.org/en-US/docs/Web/API/DOMMatrix/DOMMatrix
t2.addPath(t1, new DOMMatrix([
Math.cos(rotation) * scaleX,
Math.sin(rotation) * scaleX,
-Math.sin(rotation) * scaleY,
Math.cos(rotation) * scaleY,
x,
y
]));
return t2;
}
function createPathFetti(pathData) {
if (!canUsePaths) {
throw new Error('path confetti are not supported in this browser');
}
var path, matrix;
if (typeof pathData === 'string') {
path = pathData;
} else {
path = pathData.path;
matrix = pathData.matrix;
}
var path2d = new Path2D(path);
var tempCanvas = document.createElement('canvas');
var tempCtx = tempCanvas.getContext('2d');
if (!matrix) {
// attempt to figure out the width of the path, up to 1000x1000
var maxSize = 1000;
var minX = maxSize;
var minY = maxSize;
var maxX = 0;
var maxY = 0;
var width, height;
// do some line skipping... this is faster than checking
// every pixel and will be mostly still correct
for (var x = 0; x < maxSize; x += 2) {
for (var y = 0; y < maxSize; y += 2) {
if (tempCtx.isPointInPath(path2d, x, y, 'nonzero')) {
minX = Math.min(minX, x);
minY = Math.min(minY, y);
maxX = Math.max(maxX, x);
maxY = Math.max(maxY, y);
}
}
}
width = maxX - minX;
height = maxY - minY;
var maxDesiredSize = 10;
var scale = Math.min(maxDesiredSize/width, maxDesiredSize/height);
matrix = [
scale, 0, 0, scale,
-Math.round((width/2) + minX) * scale,
-Math.round((height/2) + minY) * scale
];
}
return {
type: 'path',
path: path,
matrix: matrix
};
}
module.exports = function() {

@@ -640,2 +733,3 @@ return getDefaultFire().apply(this, arguments);

module.exports.create = confettiCannon;
module.exports.shapeFromPath = createPathFetti;
}((function () {

@@ -642,0 +736,0 @@ if (typeof window !== 'undefined') {

2

package.json
{
"name": "canvas-confetti",
"version": "1.7.0",
"version": "1.8.0",
"description": "performant confetti animation in the browser",

@@ -5,0 +5,0 @@ "main": "src/confetti.js",

@@ -33,3 +33,3 @@ # [![Canvas Confetti](https://cdn.jsdelivr.net/gh/catdad-experiments/catdad-experiments-org@5ed78b/canvas-confetti/logo.jpg)](https://github.com/catdad/canvas-confetti/)

```html
<script src="https://cdn.jsdelivr.net/npm/canvas-confetti@1.7.0/dist/confetti.browser.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/canvas-confetti@1.8.0/dist/confetti.browser.min.js"></script>
```

@@ -76,3 +76,3 @@

- `colors` _Array&lt;String&gt;_: An array of color strings, in the HEX format... you know, like `#bada55`.
- `shapes` _Array&lt;String&gt;_: An array of shapes for the confetti. The possible values are `square`, `circle`, and `star`. The default is to use both squares and circles in an even mix. To use a single shape, you can provide just one shape in the array, such as `['star']`. You can also change the mix by providing a value such as `['circle', 'circle', 'square']` to use two third circles and one third squares.
- `shapes` _Array&lt;String|Shape&gt;_: An array of shapes for the confetti. There are 3 built-in values of `square`, `circle`, and `star`. The default is to use both squares and circles in an even mix. To use a single shape, you can provide just one shape in the array, such as `['star']`. You can also change the mix by providing a value such as `['circle', 'circle', 'square']` to use two third circles and one third squares. You can also create your own shapes using the `confetti.shapeFromPath` helper method.
- `scalar` _Number (default: 1)_: Scale factor for each confetti particle. Use decimals to make the confetti smaller. Go on, try teeny tiny confetti, they are adorable!

@@ -82,2 +82,23 @@ - `zIndex` _Integer (default: 100)_: The confetti should be on top, after all. But if you have a crazy high page, you can set it even higher.

### `confetti.shapeFromPath({ path, matrix? })` -> `Shape`
This helper method lets you create a custom confetti shape using an [SVG Path string](https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/d). Any valid path should work, though there are a few caveats:
- All paths will be filed. If you were hoping to have a stroke path, that is not implemented.
- Paths are limited to a single color, so keep that in mind.
- All paths need a valid transform matrix. You can pass one in, or you can leave it out and use this helper to calculate the matrix for you. Do note that calculating the matrix is a bit expensive, so it is best to calculate it once for each path in development and cache that value, so that production confetti remain fast. The matrix is deterministic and will always be the same given the same path value.
- For best forward compatibility, it is best to re-generate and re-cache the matrix if you update the `canvas-confetti` library.
- Support for path-based confetti is limited to browsers which support [`Path2D`](https://developer.mozilla.org/en-US/docs/Web/API/Path2D), which should really be all major browser at this point.
This method will return a `Shape` -- it's really just a plain object with some properties, but shhh... we'll pretend it's a shape. Pass this `Shape` object into the `shapes` array directly.
As an example, here's how you might do a triangle confetti:
```javascript
var triangle = confetti.shapeFromPath({ path: 'M0 10 L5 0 L10 10z' });
confetti({
shapes: [triangle]
});
```
### `confetti.create(canvas, [globalOptions])` → `function`

@@ -84,0 +105,0 @@

@@ -13,2 +13,4 @@ (function main(global, module, isWorker, workerSize) {

var canUsePaths = typeof Path2D === 'function' && typeof DOMMatrix === 'function';
function noop() {}

@@ -345,5 +347,16 @@

context.fillStyle = 'rgba(' + fetti.color.r + ', ' + fetti.color.g + ', ' + fetti.color.b + ', ' + (1 - progress) + ')';
context.beginPath();
if (fetti.shape === 'circle') {
if (canUsePaths && fetti.shape.type === 'path' && typeof fetti.shape.path === 'string' && Array.isArray(fetti.shape.matrix)) {
context.fill(transformPath2D(
fetti.shape.path,
fetti.shape.matrix,
fetti.x,
fetti.y,
Math.abs(x2 - x1) * 0.1,
Math.abs(y2 - y1) * 0.1,
Math.PI / 10 * fetti.wobble
));
} else if (fetti.shape === 'circle') {
context.ellipse ?

@@ -629,2 +642,82 @@ context.ellipse(fetti.x, fetti.y, Math.abs(x2 - x1) * fetti.ovalScalar, Math.abs(y2 - y1) * fetti.ovalScalar, Math.PI / 10 * fetti.wobble, 0, 2 * Math.PI) :

function transformPath2D(pathString, pathMatrix, x, y, scaleX, scaleY, rotation) {
var path2d = new Path2D(pathString);
var t1 = new Path2D();
t1.addPath(path2d, new DOMMatrix(pathMatrix));
var t2 = new Path2D();
// see https://developer.mozilla.org/en-US/docs/Web/API/DOMMatrix/DOMMatrix
t2.addPath(t1, new DOMMatrix([
Math.cos(rotation) * scaleX,
Math.sin(rotation) * scaleX,
-Math.sin(rotation) * scaleY,
Math.cos(rotation) * scaleY,
x,
y
]));
return t2;
}
function createPathFetti(pathData) {
if (!canUsePaths) {
throw new Error('path confetti are not supported in this browser');
}
var path, matrix;
if (typeof pathData === 'string') {
path = pathData;
} else {
path = pathData.path;
matrix = pathData.matrix;
}
var path2d = new Path2D(path);
var tempCanvas = document.createElement('canvas');
var tempCtx = tempCanvas.getContext('2d');
if (!matrix) {
// attempt to figure out the width of the path, up to 1000x1000
var maxSize = 1000;
var minX = maxSize;
var minY = maxSize;
var maxX = 0;
var maxY = 0;
var width, height;
// do some line skipping... this is faster than checking
// every pixel and will be mostly still correct
for (var x = 0; x < maxSize; x += 2) {
for (var y = 0; y < maxSize; y += 2) {
if (tempCtx.isPointInPath(path2d, x, y, 'nonzero')) {
minX = Math.min(minX, x);
minY = Math.min(minY, y);
maxX = Math.max(maxX, x);
maxY = Math.max(maxY, y);
}
}
}
width = maxX - minX;
height = maxY - minY;
var maxDesiredSize = 10;
var scale = Math.min(maxDesiredSize/width, maxDesiredSize/height);
matrix = [
scale, 0, 0, scale,
-Math.round((width/2) + minX) * scale,
-Math.round((height/2) + minY) * scale
];
}
return {
type: 'path',
path: path,
matrix: matrix
};
}
module.exports = function() {

@@ -637,2 +730,3 @@ return getDefaultFire().apply(this, arguments);

module.exports.create = confettiCannon;
module.exports.shapeFromPath = createPathFetti;
}((function () {

@@ -639,0 +733,0 @@ if (typeof window !== 'undefined') {

Sorry, the diff of this file is not supported yet