Skip to content

Commit

Permalink
simplifying implementation to dedupe unnecessary actions
Browse files Browse the repository at this point in the history
  • Loading branch information
catdad committed Sep 28, 2023
1 parent d5f0982 commit cd85326
Showing 1 changed file with 6 additions and 22 deletions.
28 changes: 6 additions & 22 deletions src/confetti.js
Original file line number Diff line number Diff line change
Expand Up @@ -335,13 +335,12 @@
context.beginPath();

if (canUsePaths && typeof fetti.shape.path === 'string' && typeof fetti.shape.scale === 'number') {
var path = scalePath2D(new Path2D(fetti.shape.path), fetti.shape.scale);
context.fill(transformPath2D(
path,
fetti.shape.path,
fetti.x + fetti.wobble,
fetti.y + fetti.wobble,
Math.abs(x2 - x1) * 0.15,
Math.abs(y2 - y1) * 0.15,
Math.abs(x2 - x1) * 0.15 * fetti.shape.scale,
Math.abs(y2 - y1) * 0.15 * fetti.shape.scale,
Math.PI / 10 * fetti.wobble
));
} else if (fetti.shape === 'circle') {
Expand Down Expand Up @@ -623,7 +622,9 @@
return defaultFire;
}

function transformPath2D(path2d, x, y, scaleX, scaleY, rotation) {
function transformPath2D(pathString, x, y, scaleX, scaleY, rotation) {
var path2d = new Path2D(pathString);

// this would be ideal, but it does not work in workers
// var matrix = new DOMMatrix('translate(' + x + 'px, ' + y + 'px) rotate(' + rotation + 'rad) scale(' + (scaleX) + ', ' + (scaleY) + ')');

Expand All @@ -642,21 +643,6 @@
return transformed;
}

function scalePath2D(path2d, scale) {
// workers can't use:
// * new DOMMatrix(scale, 0, 0, scale, 0, 0);
// * new DOMMatrix(`scale(${scale})`);
// so create an empty one and add the necessary values
var matrix = new DOMMatrix();
matrix.a = scale;
matrix.d = scale;

var scaledPath2D = new Path2D();
scaledPath2D.addPath(path2d, matrix);

return scaledPath2D;
}

function createPathFetti(path, width, height) {
if (!canUsePaths) {
throw new Error('path confetti are not supported in this browser');
Expand Down Expand Up @@ -698,8 +684,6 @@
height: height,
scale: scale
};

return scalePath2D(path2d, scale);
}

module.exports = function() {
Expand Down

0 comments on commit cd85326

Please sign in to comment.