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

canvas-confetti

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.0.0

@@ -1,57 +0,145 @@

// canvas-confetti v0.5.0 built on 2019-11-20T23:30:42.161Z
// canvas-confetti v1.0.0 built on 2019-12-22T14:36:26.796Z
!(function (window, module) {
// source content
(function () {
(function main(global, isWorker, workerSize) {
var canUseWorker = global.Worker &&
global.Blob &&
global.OffscreenCanvas &&
global.Promise &&
global.URL &&
!!global.URL.createObjectURL;
function noop() {}
// create a promise if it exists, otherwise, just
// call the function directly
function promise(func) {
var ModulePromise = module.exports.Promise;
var Prom = ModulePromise !== void 0 ? ModulePromise : global.Promise;
if (typeof Prom === 'function') {
return new Prom(func);
}
func(noop, noop);
return null;
}
var raf = (function () {
var TIME = Math.floor(1000 / 60);
var frame, cancel;
var frames = {};
var lastFrameTime = 0;
function init() {
if (window.requestAnimationFrame && window.cancelAnimationFrame) {
frame = window.requestAnimationFrame;
cancel = window.cancelAnimationFrame;
} else {
['webkit', 'moz', 'o', 'ms'].forEach(function (name) {
if (frame && cancel) {
return;
}
if (typeof requestAnimationFrame === 'function' && typeof cancelAnimationFrame === 'function') {
frame = function (cb) {
var id = Math.random();
var framename = name + 'RequestAnimationFrame';
var cancelname = name + 'CancelAnimationFrame';
frames[id] = requestAnimationFrame(function onFrame(time) {
if (lastFrameTime === time || lastFrameTime + TIME - 1 < time) {
lastFrameTime = time;
delete frames[id];
if (window[framename] && window[cancelname]) {
frame = window[framename];
cancel = window[cancelname];
cb();
} else {
frames[id] = requestAnimationFrame(onFrame);
}
});
}
if (!(frame && cancel)) {
frame = function (cb) {
return window.setTimeout(cb, 1000 / 60);
};
cancel = function (timer) {
return window.clearTimeout(timer);
};
}
return id;
};
cancel = function (id) {
if (frames[id]) {
cancelAnimationFrame(frames[id]);
}
};
} else {
frame = function (cb) {
return setTimeout(cb, TIME);
};
cancel = function (timer) {
return clearTimeout(timer);
};
}
return {
frame: function (arg) {
if (frame) {
return frame(arg);
}
return { frame: frame, cancel: cancel };
}());
init();
return frame(arg);
},
cancel: function (arg) {
if (cancel) {
return cancel(arg);
var getWorker = (function () {
var worker;
var resolves = {};
function decorate(worker) {
worker.init = function initWorker(canvas) {
var offscreen = canvas.transferControlToOffscreen();
worker.postMessage({ canvas: offscreen }, [offscreen]);
};
worker.fire = function fireWorker(options, size, done) {
var id = Math.random().toString(36).slice(2);
return promise(function (resolve) {
function workerDone(msg) {
if (msg.data.callback !== id) {
return;
}
delete resolves[id];
worker.removeEventListener('message', workerDone);
done();
resolve();
}
worker.addEventListener('message', workerDone);
worker.postMessage({ options: options || {}, callback: id });
resolves[id] = workerDone.bind(null, { data: { callback: id }});
});
};
worker.reset = function resetWorker() {
worker.postMessage({ reset: true });
for (var id in resolves) {
resolves[id]();
delete resolves[id];
}
};
}
init();
return cancel(arg);
return function () {
if (worker) {
return worker;
}
if (!isWorker && canUseWorker) {
var code = [
'var CONFETTI, SIZE = {}, module = {};',
'(' + main.toString() + ')(this, true, SIZE);',
'onmessage = function(msg) {',
' if (msg.data.options) {',
' CONFETTI(msg.data.options).then(function () {',
' postMessage({ callback: msg.data.callback });',
' });',
' } else if (msg.data.reset) {',
' CONFETTI.reset();',
' } else if (msg.data.resize) {',
' SIZE.width = msg.data.resize.width;',
' SIZE.height = msg.data.resize.height;',
' } else if (msg.data.canvas) {',
' SIZE.width = msg.data.canvas.width;',
' SIZE.height = msg.data.canvas.height;',
' CONFETTI = module.exports.create(msg.data.canvas);',
' }',
'}',
].join('\n');
worker = new Worker(URL.createObjectURL(new Blob([code])));
decorate(worker);
}
return worker;
};
}());
})();

@@ -80,19 +168,2 @@ var defaults = {

function noop() {}
// create a promise if it exists, otherwise, just
// call the function directly
function promise(func) {
var ModulePromise = module.exports.Promise;
var Prom = ModulePromise !== void 0 ? ModulePromise : window.Promise;
if (typeof Prom === 'function') {
return new Prom(func);
}
func(noop, noop);
return null;
}
function convert(val, transform) {

@@ -158,4 +229,2 @@ return transform ? transform(val) : val;

setCanvasWindowSize(canvas);
canvas.style.position = 'fixed';

@@ -242,17 +311,8 @@ canvas.style.top = '0px';

function animate(canvas, fettis, isLibCanvas, allowResize, done) {
function animate(canvas, fettis, resizer, size, done) {
var animatingFettis = fettis.slice();
var context = canvas.getContext('2d');
var width = canvas.width;
var height = canvas.height;
var resizer = isLibCanvas ? setCanvasWindowSize : setCanvasRectSize;
var animationFrame;
var destroy;
function onResize() {
// don't actually query the size here, since this
// can execute frequently and rapidly
width = height = null;
}
var prom = promise(function (resolve) {

@@ -262,8 +322,4 @@ function onDone() {

if (allowResize) {
window.removeEventListener('resize', onResize);
}
context.clearRect(0, 0, size.width, size.height);
context.clearRect(0, 0, width, height);
done();

@@ -274,9 +330,14 @@ resolve();

function update() {
if (!width && !height) {
if (isWorker && !(size.width === workerSize.width && size.height === workerSize.height)) {
size.width = canvas.width = workerSize.width;
size.height = canvas.height = workerSize.height;
}
if (!size.width && !size.height) {
resizer(canvas);
width = canvas.width;
height = canvas.height;
size.width = canvas.width;
size.height = canvas.height;
}
context.clearRect(0, 0, width, height);
context.clearRect(0, 0, size.width, size.height);

@@ -298,6 +359,2 @@ animatingFettis = animatingFettis.filter(function (fetti) {

if (allowResize) {
window.addEventListener('resize', onResize, false);
}
return {

@@ -326,6 +383,9 @@ addFettis: function (fettis) {

var allowResize = !!prop(globalOpts || {}, 'resize');
var resized = false;
var shouldUseWorker = canUseWorker && !!prop(globalOpts || {}, 'useWorker');
var worker = shouldUseWorker ? getWorker() : null;
var resizer = isLibCanvas ? setCanvasWindowSize : setCanvasRectSize;
var initialized = false;
var animationObj;
function fire(options) {
function fireLocal(options, size, done) {
var particleCount = prop(options, 'particleCount', Math.floor);

@@ -338,3 +398,2 @@ var angle = prop(options, 'angle', Number);

var ticks = prop(options, 'ticks', Number);
var zIndex = prop(options, 'zIndex', Number);
var shapes = prop(options, 'shapes');

@@ -346,10 +405,2 @@ var origin = getOrigin(options);

if (isLibCanvas) {
canvas = animationObj ? animationObj.canvas : getCanvas(zIndex);
} else if (allowResize && !resized) {
// initialize the size of a user-supplied canvas
setCanvasRectSize(canvas);
resized = true;
}
var startX = canvas.width * origin.x;

@@ -380,18 +431,79 @@ var startY = canvas.height * origin.y;

if (isLibCanvas) {
animationObj = animate(canvas, fettis, resizer, size , done);
return animationObj.promise;
}
function fire(options) {
var zIndex = prop(options, 'zIndex', Number);
if (isLibCanvas && animationObj) {
// use existing canvas from in-progress animation
canvas = animationObj.canvas;
} else if (isLibCanvas && !canvas) {
// create and initialize a new canvas
canvas = getCanvas(zIndex);
document.body.appendChild(canvas);
}
animationObj = animate(canvas, fettis, isLibCanvas, (isLibCanvas || allowResize), function () {
if (allowResize && !initialized) {
// initialize the size of a user-supplied canvas
resizer(canvas);
}
var size = {
width: canvas.width,
height: canvas.height
};
if (worker && !initialized) {
worker.init(canvas);
}
initialized = true;
function onResize() {
if (worker) {
// TODO this really shouldn't be immediate, because it is expensive
var obj = {};
resizer(obj);
worker.postMessage({ resize: obj });
return;
}
// don't actually query the size here, since this
// can execute frequently and rapidly
size.width = size.height = null;
}
function done() {
animationObj = null;
if (isLibCanvas) {
if (allowResize) {
global.removeEventListener('resize', onResize);
}
if (isLibCanvas && canvas) {
document.body.removeChild(canvas);
canvas = null;
initialized = false;
}
});
}
return animationObj.promise;
if (allowResize) {
global.addEventListener('resize', onResize, false);
}
if (worker) {
return worker.fire(options, size, done);
}
return fireLocal(options, size, done);
}
fire.reset = function () {
if (worker) {
worker.reset();
}
if (animationObj) {

@@ -405,6 +517,16 @@ animationObj.reset();

module.exports = confettiCannon();
module.exports = confettiCannon(null, { useWorker: true, resize: true });
module.exports.create = confettiCannon;
}());
}((function () {
if (typeof window !== 'undefined') {
return window;
}
if (typeof self !== 'undefined') {
return self;
}
return this;
})(), false));
// end source content

@@ -411,0 +533,0 @@

{
"name": "canvas-confetti",
"version": "0.5.0",
"version": "1.0.0",
"description": "performant confetti animation in the browser",

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

@@ -33,3 +33,3 @@ # 🎊 canvas confetti 🎊

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

@@ -80,2 +80,3 @@

* `resize` _Boolean (default: false)_: Whether to allow setting the canvas image size, as well as keep it correctly sized if the window changes size (e.g. resiging the window, rotating a mobile device, etc.). By default, the canvas size will not be modified.
* `useWorker` _Boolean (default: false)_: Whether to use an asynchronous web worker to render the confetti animation, whenever possible. This is turned off by default, meaning that the animation will always execute on the main thread. If turned on and the browser supports it, the animation will execute off of the main thread so that it is not blocking any other work your page needs to do. If it is not supported by the browser, this value will be ignored.

@@ -86,3 +87,6 @@ ```javascript

var myConfetti = confetti.create(myCanvas, { resize: true });
var myConfetti = confetti.create(myCanvas, {
resize: true,
useWorker: true
});
myConfetti({

@@ -89,0 +93,0 @@ particleCount: 100,

@@ -1,54 +0,142 @@

(function () {
(function main(global, isWorker, workerSize) {
var canUseWorker = global.Worker &&
global.Blob &&
global.OffscreenCanvas &&
global.Promise &&
global.URL &&
!!global.URL.createObjectURL;
function noop() {}
// create a promise if it exists, otherwise, just
// call the function directly
function promise(func) {
var ModulePromise = module.exports.Promise;
var Prom = ModulePromise !== void 0 ? ModulePromise : global.Promise;
if (typeof Prom === 'function') {
return new Prom(func);
}
func(noop, noop);
return null;
}
var raf = (function () {
var TIME = Math.floor(1000 / 60);
var frame, cancel;
var frames = {};
var lastFrameTime = 0;
function init() {
if (window.requestAnimationFrame && window.cancelAnimationFrame) {
frame = window.requestAnimationFrame;
cancel = window.cancelAnimationFrame;
} else {
['webkit', 'moz', 'o', 'ms'].forEach(function (name) {
if (frame && cancel) {
return;
}
if (typeof requestAnimationFrame === 'function' && typeof cancelAnimationFrame === 'function') {
frame = function (cb) {
var id = Math.random();
var framename = name + 'RequestAnimationFrame';
var cancelname = name + 'CancelAnimationFrame';
frames[id] = requestAnimationFrame(function onFrame(time) {
if (lastFrameTime === time || lastFrameTime + TIME - 1 < time) {
lastFrameTime = time;
delete frames[id];
if (window[framename] && window[cancelname]) {
frame = window[framename];
cancel = window[cancelname];
cb();
} else {
frames[id] = requestAnimationFrame(onFrame);
}
});
}
if (!(frame && cancel)) {
frame = function (cb) {
return window.setTimeout(cb, 1000 / 60);
};
cancel = function (timer) {
return window.clearTimeout(timer);
};
}
return id;
};
cancel = function (id) {
if (frames[id]) {
cancelAnimationFrame(frames[id]);
}
};
} else {
frame = function (cb) {
return setTimeout(cb, TIME);
};
cancel = function (timer) {
return clearTimeout(timer);
};
}
return {
frame: function (arg) {
if (frame) {
return frame(arg);
}
return { frame: frame, cancel: cancel };
}());
init();
return frame(arg);
},
cancel: function (arg) {
if (cancel) {
return cancel(arg);
var getWorker = (function () {
var worker;
var resolves = {};
function decorate(worker) {
worker.init = function initWorker(canvas) {
var offscreen = canvas.transferControlToOffscreen();
worker.postMessage({ canvas: offscreen }, [offscreen]);
};
worker.fire = function fireWorker(options, size, done) {
var id = Math.random().toString(36).slice(2);
return promise(function (resolve) {
function workerDone(msg) {
if (msg.data.callback !== id) {
return;
}
delete resolves[id];
worker.removeEventListener('message', workerDone);
done();
resolve();
}
worker.addEventListener('message', workerDone);
worker.postMessage({ options: options || {}, callback: id });
resolves[id] = workerDone.bind(null, { data: { callback: id }});
});
};
worker.reset = function resetWorker() {
worker.postMessage({ reset: true });
for (var id in resolves) {
resolves[id]();
delete resolves[id];
}
};
}
init();
return cancel(arg);
return function () {
if (worker) {
return worker;
}
if (!isWorker && canUseWorker) {
var code = [
'var CONFETTI, SIZE = {}, module = {};',
'(' + main.toString() + ')(this, true, SIZE);',
'onmessage = function(msg) {',
' if (msg.data.options) {',
' CONFETTI(msg.data.options).then(function () {',
' postMessage({ callback: msg.data.callback });',
' });',
' } else if (msg.data.reset) {',
' CONFETTI.reset();',
' } else if (msg.data.resize) {',
' SIZE.width = msg.data.resize.width;',
' SIZE.height = msg.data.resize.height;',
' } else if (msg.data.canvas) {',
' SIZE.width = msg.data.canvas.width;',
' SIZE.height = msg.data.canvas.height;',
' CONFETTI = module.exports.create(msg.data.canvas);',
' }',
'}',
].join('\n');
worker = new Worker(URL.createObjectURL(new Blob([code])));
decorate(worker);
}
return worker;
};
}());
})();

@@ -77,19 +165,2 @@ var defaults = {

function noop() {}
// create a promise if it exists, otherwise, just
// call the function directly
function promise(func) {
var ModulePromise = module.exports.Promise;
var Prom = ModulePromise !== void 0 ? ModulePromise : window.Promise;
if (typeof Prom === 'function') {
return new Prom(func);
}
func(noop, noop);
return null;
}
function convert(val, transform) {

@@ -155,4 +226,2 @@ return transform ? transform(val) : val;

setCanvasWindowSize(canvas);
canvas.style.position = 'fixed';

@@ -239,17 +308,8 @@ canvas.style.top = '0px';

function animate(canvas, fettis, isLibCanvas, allowResize, done) {
function animate(canvas, fettis, resizer, size, done) {
var animatingFettis = fettis.slice();
var context = canvas.getContext('2d');
var width = canvas.width;
var height = canvas.height;
var resizer = isLibCanvas ? setCanvasWindowSize : setCanvasRectSize;
var animationFrame;
var destroy;
function onResize() {
// don't actually query the size here, since this
// can execute frequently and rapidly
width = height = null;
}
var prom = promise(function (resolve) {

@@ -259,8 +319,4 @@ function onDone() {

if (allowResize) {
window.removeEventListener('resize', onResize);
}
context.clearRect(0, 0, size.width, size.height);
context.clearRect(0, 0, width, height);
done();

@@ -271,9 +327,14 @@ resolve();

function update() {
if (!width && !height) {
if (isWorker && !(size.width === workerSize.width && size.height === workerSize.height)) {
size.width = canvas.width = workerSize.width;
size.height = canvas.height = workerSize.height;
}
if (!size.width && !size.height) {
resizer(canvas);
width = canvas.width;
height = canvas.height;
size.width = canvas.width;
size.height = canvas.height;
}
context.clearRect(0, 0, width, height);
context.clearRect(0, 0, size.width, size.height);

@@ -295,6 +356,2 @@ animatingFettis = animatingFettis.filter(function (fetti) {

if (allowResize) {
window.addEventListener('resize', onResize, false);
}
return {

@@ -323,6 +380,9 @@ addFettis: function (fettis) {

var allowResize = !!prop(globalOpts || {}, 'resize');
var resized = false;
var shouldUseWorker = canUseWorker && !!prop(globalOpts || {}, 'useWorker');
var worker = shouldUseWorker ? getWorker() : null;
var resizer = isLibCanvas ? setCanvasWindowSize : setCanvasRectSize;
var initialized = false;
var animationObj;
function fire(options) {
function fireLocal(options, size, done) {
var particleCount = prop(options, 'particleCount', Math.floor);

@@ -335,3 +395,2 @@ var angle = prop(options, 'angle', Number);

var ticks = prop(options, 'ticks', Number);
var zIndex = prop(options, 'zIndex', Number);
var shapes = prop(options, 'shapes');

@@ -343,10 +402,2 @@ var origin = getOrigin(options);

if (isLibCanvas) {
canvas = animationObj ? animationObj.canvas : getCanvas(zIndex);
} else if (allowResize && !resized) {
// initialize the size of a user-supplied canvas
setCanvasRectSize(canvas);
resized = true;
}
var startX = canvas.width * origin.x;

@@ -377,18 +428,79 @@ var startY = canvas.height * origin.y;

if (isLibCanvas) {
animationObj = animate(canvas, fettis, resizer, size , done);
return animationObj.promise;
}
function fire(options) {
var zIndex = prop(options, 'zIndex', Number);
if (isLibCanvas && animationObj) {
// use existing canvas from in-progress animation
canvas = animationObj.canvas;
} else if (isLibCanvas && !canvas) {
// create and initialize a new canvas
canvas = getCanvas(zIndex);
document.body.appendChild(canvas);
}
animationObj = animate(canvas, fettis, isLibCanvas, (isLibCanvas || allowResize), function () {
if (allowResize && !initialized) {
// initialize the size of a user-supplied canvas
resizer(canvas);
}
var size = {
width: canvas.width,
height: canvas.height
};
if (worker && !initialized) {
worker.init(canvas);
}
initialized = true;
function onResize() {
if (worker) {
// TODO this really shouldn't be immediate, because it is expensive
var obj = {};
resizer(obj);
worker.postMessage({ resize: obj });
return;
}
// don't actually query the size here, since this
// can execute frequently and rapidly
size.width = size.height = null;
}
function done() {
animationObj = null;
if (isLibCanvas) {
if (allowResize) {
global.removeEventListener('resize', onResize);
}
if (isLibCanvas && canvas) {
document.body.removeChild(canvas);
canvas = null;
initialized = false;
}
});
}
return animationObj.promise;
if (allowResize) {
global.addEventListener('resize', onResize, false);
}
if (worker) {
return worker.fire(options, size, done);
}
return fireLocal(options, size, done);
}
fire.reset = function () {
if (worker) {
worker.reset();
}
if (animationObj) {

@@ -402,4 +514,14 @@ animationObj.reset();

module.exports = confettiCannon();
module.exports = confettiCannon(null, { useWorker: true, resize: true });
module.exports.create = confettiCannon;
}());
}((function () {
if (typeof window !== 'undefined') {
return window;
}
if (typeof self !== 'undefined') {
return self;
}
return this;
})(), false));

Sorry, the diff of this file is not supported yet