Giter Site home page Giter Site logo

catdad / canvas-confetti Goto Github PK

View Code? Open in Web Editor NEW
8.7K 30.0 318.0 327 KB

πŸŽ‰ performant confetti animation in the browser

Home Page: https://catdad.github.io/canvas-confetti/

License: ISC License

HTML 37.54% JavaScript 61.97% Shell 0.49%
canvas confetti jsdelivr animation burst fireworks snow particles canvas-confetti hacktoberfest

canvas-confetti's People

Contributors

0xflotus avatar abhisheksoni121 avatar adamreisnz avatar ammendonca avatar benv avatar catdad avatar d38u6 avatar edoardolincetto avatar ernes avatar jchristopher avatar jessbarnes avatar liamcmitchell-sc avatar mozi47 avatar swhitf avatar tessasac avatar tidwell avatar ulitcos avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

canvas-confetti's Issues

not working for IE

Not working for IE giving SCRIPT438: Object doesn't support property or method 'ellipse' error.

set dpr on the canvas, for optimal quality

function setupCanvas(canvas) {
  // Get the device pixel ratio, falling back to 1.
  var dpr = window.devicePixelRatio || 1;
  // Get the size of the canvas in CSS pixels.
  var rect = canvas.getBoundingClientRect();
  // Give the canvas pixel dimensions of their CSS
  // size * the device pixel ratio.
  canvas.width = rect.width * dpr;
  canvas.height = rect.height * dpr;
  var ctx = canvas.getContext('2d');
  // Scale all drawing operations by the dpr, so you
  // don't have to worry about the difference.
  ctx.scale(dpr, dpr);
  return ctx;
}

// Now this line will be the same size on the page
// but will look sharper on high-DPI devices!
var ctx = setupCanvas(document.querySelector('.my-canvas'));
ctx.lineWidth = 5;
ctx.beginPath();
ctx.moveTo(100, 100);
ctx.lineTo(200, 200);
ctx.stroke();

https://www.html5rocks.com/en/tutorials/canvas/hidpi/

fix rotation issue

It should look more natural... I think there is an issue with the rotation origin.

Question - Updates

Hi,

Is it possible to include to your releases, a summary what's new and what's changed?

E.g. like they are doing it here

image

Thank you :)

handle window resizes

These are actually pretty common on mobile, when the address bar disappears while scrolling (or when someone rotates their phone).

Extra property/css on canvas

This is a feature request regarding the options, it would be good if we can include some extra property or class in the options, so it would be easier to refer them later on or style them.

make sure the module can be browserified

  • add tests to make sure it can be required and built
  • add distribution artifact ready for the browser
  • make the requirable module is the main in package.json
  • add step to browserify the module file
  • add tests to make sure browserified version works

Reset added but not working, or i'm stupid ;P

Hi, thanks for added the reset!

I know it uses an instance to reset the confetti but what about the example below?
I use that one to create a confetti canon like animation.

How to stop those?

// do this for 30 seconds
var duration = 30 * 1000;
var end = Date.now() + duration;

(function frame() {
  // launch a few confetti from the left edge
  confetti({
    particleCount: 7,
    angle: 60,
    spread: 55,
    origin: { x: 0 }
  });
  // and launch a few from the right edge
  confetti({
    particleCount: 7,
    angle: 120,
    spread: 55,
    origin: { x: 1 }
  });

  // keep going until we are out of time
  if (Date.now() < end) {
    requestAnimationFrame(frame);
  }
}());

consider adding image shape type

Next to rectangles and ellipses, i'd love to see the option of using images as particle shapes.

One could use it with the standard shape array like ['image', 'image', 'circle'] to accomodate for distribution of a single image next to common shapes.
To get the single image source, it would be one way to just define it in the confetti object like:

confetti: {
  image: {
    source: 'URL',
    options: {}
  }
}

Another way could be defining a set of images in shapes as well: ['images']. Probability should fit the current system, but can be precised in its own child images' property.

To define them we use another object inside confetti:

confetti: {
  images: [
    image: {
      source: 'URL',
      chance: 0.5
      options: {}
    },
    image: {
      source: 'URL',
      chance: 0.5
      options: {}
    }
  ]
}

Thanks for this awesome project which looks by far the best! ✌πŸ₯³

consider adding path shape type

Example code (limited to more modern browsers):

function path(context, x, y, scaleX, scaleY, rotation, path) {
  context.save();
  context.translate(x, y);
  context.rotate(rotation);
  context.scale(scaleX, scaleY);
  context.fill(new Path2D(path));
  context.restore();
}
...
path(context, fetti.x + fetti.wobble, fetti.y + fetti.wobble, Math.abs(x2 - x1) * 0.2, Math.abs(y2 - y1) * 0.2, Math.PI / 10 * fetti.wobble, fetti.shape.path);
...
confetti({
    particleCount: 100,
    shapes: ['circle', { type: 'path', path: 'M1 1 h 8 v 8 h -8 Z' }]
});

A path will probably need to provide a width and height or a scale factor, so that the resulting path can be scaled appropriately.

This code has very poor performance. Any acceptable solution will need to solve that.

add option for circle shape

I saw this in an animation somewhere. It had confetti as a mixture of circles and squares, and it looked pretty cool.

How to use this confetti in Angular 4

Hi guys !
I install this canvas-confetti using npm install.. after that i dont have an idea to use this in my angular project.
how can i import this ?

initialize module with user-supplied canvas

  • a whole new set of internal bits is created for each new canvas
    • remove global animationObj
  • multiple canvases can exist on the same page and both include animations
  • cannot call setCanvasSize on user canvas, as the lib should not be modifying those
  • add tests for custom canvas
  • add webpage example
  • add readme documentation

License missing

Hi,

can you please provide a license file or state under which license this project is published. Thanks and keep up the good work!

support for builds with server-side rendering

When using SSR (for example, with something like Razzle), window cannot be assumed as an existing global, because it does not exist at the time of the build (the code has to be evaluated on the server), so such uses need to reserved for function execution.

/mnt/d/Git/test/confetti-razzle/node_modules/canvas-confetti/src/confetti.js:370
}());
 ^
ReferenceError: window is not defined
    at /mnt/d/Git/test/confetti-razzle/node_modules/canvas-confetti/src/confetti.js:4:5
    at /mnt/d/Git/test/confetti-razzle/node_modules/canvas-confetti/src/confetti.js:31:4
    at Object.<anonymous> (/mnt/d/Git/test/confetti-razzle/node_modules/canvas-confetti/src/confetti.js:370:2)
    at Module._compile (internal/modules/cjs/loader.js:956:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:973:10)
    at Module.load (internal/modules/cjs/loader.js:812:32)
    at Function.Module._load (internal/modules/cjs/loader.js:724:14)
    at Module.require (internal/modules/cjs/loader.js:849:19)
    at require (internal/modules/cjs/helpers.js:74:18)
    at Object.canvas-confetti (/mnt/d/Git/test/confetti-razzle/external "canvas-confetti":1:1)

Broken in older versions of iOS Safari (v. 9.0)

Broken in older versions of iOS Safari (and probably other browsers) that don't support ECMAScript 6 features such as let.

Throws the following error:
Screen Shot 2019-09-12 at 10 54 49 AM


I was able to fix this issue by simply changing 'let' to 'var' in confetti.module.mjs:

OLD

export let create = module.exports.create;

NEW

export var create = module.exports.create;

make promises required

This will allow some internal improvements, especially related to #83 where asynchronous code will need to be kept track of across threads. Promises are ubiquitous now, and users who support browsers without promises will be required to polyfill them.

consider adding char shape type (i.e. emoji)

Example code:

function text(context, x, y, scaleX, scaleY, rotation, char) {
  context.setTransform(scaleX, 0, 0, scaleY, x, y);
  context.rotate(rotation);
  context.textAlign = 'center';
  context.font = 'normal normal 16px auto';
  context.fillText(char, 0, 0);
  context.setTransform(1,0,0,1,0,0);
}
...
text(context, fetti.x, fetti.y, Math.abs(x2 - x1) * 0.2, Math.abs(y2 - y1) * 0.2, Math.PI / 10 * fetti.wobble, fetti.shape.char);
...
confetti({
  shapes: [{ type: 'char', char: 'πŸ’©' }]
});

I am guessing we'd need an additional font family argument.

This code has very poor performance. Any acceptable solution will need to solve that.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.