2.0.1 • Published 8 months ago

confetti-cannon v2.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
8 months ago

Confetti Cannon

Launch Confetti

Example

Install

npm i confetti-cannon

How to use

This is the basic use of the cannon where we render everything we need and then create confetti on the canvas where the user clicks. For more advanced uses, check out the stories.

import {
  ConfettiCanvas,
  Environment,
  SpriteCanvas,
  useConfettiCannon,
  ConfettiCanvasHandle,
  SpriteCanvasHandle,
} from "confetti-cannon";

const SPRITES = [
  require("./images/square.svg"),
  require("./images/circle.svg"),
];
const COLORS = ["#FF73FA", "#FFC0FF"];
const SIZE = 40;

function Example() {
  const [confettiCanvas, setConfettiCanvas] =
    React.useState<ConfettiCanvasHandle | null>(null);
  const [spriteCanvas, setSpriteCanvas] =
    React.useState<SpriteCanvasHandle | null>(null);
  const environment = React.useMemo(() => new Environment(), []);
  const cannon = useConfettiCannon(confettiCanvas, spriteCanvas);

  const addConfetti = React.useCallback(
    (x: number, y: number) => {
      cannon.createConfetti({
        position: {
          type: "static",
          value: { x, y },
        },
        size: {
          type: "static",
          value: SIZE,
        },
      });
    },
    [cannon]
  );

  const handleClick = (e: MouseEvent) => {
    const { x, y } = getClickPosition(e, confettiCanvas?.getCanvas());
    addConfetti(x, y);
  };

  return (
    <>
      <SpriteCanvas
        ref={setSpriteCanvas}
        sprites={SPRITES}
        colors={COLORS}
        spriteWidth={SIZE}
        spriteHeight={SIZE}
      />
      <ConfettiCanvas
        ref={setConfettiCanvas}
        onClick={handleClick}
        environment={environment}
      />
    </>
  );
}

Components

SpriteCanvas

A SpriteCanvas is used to pre-render your confetti. You'll need to render this somewhere in your app in order to launch confetti on an ConfettiCanvas. This will not be visible to users.

PropTypeDescription
spriteWidthnumberThe width to render the confetti at. Should be the largest width your confetti will be rendered at if possible
spriteHeightnumberThe height to render the confetti at. Should be the largest width your confetti will be rendered at if possible
colorsstring[]The colors your confetti will be rendered as
spritesArray<string \| {src: string, colorized: boolean}>The sources of your confetti images. If you do not want to color an image, use {src, colorize: false}
visiblebooleanUsed for debugging if you'd like to see the SpriteCanvas on screen

ConfettiCanvas

A ConfettiCanvas is the canvas that will render your confetti on screen

PropTypeDescription
environmentEnvironmentAn object representing the environment effecting the confetti
onClick(e: MouseEvent, confetti: Confetti \| null) => voidEvent fired when the user clicks the canvas, if they clicked a confetti, that confetti is included
onMouseDown(e: MouseEvent, confetti: Confetti \| null) => voidEvent fired when the user mouses down on the canvas, if they moused down on a confetti, that confetti is included

to create an Environment, use new Environment().

ArgTypeDefaultDescription
gravitynumber-9.8How confetti will be effected on the y axis
windnumber0How confetti will be effected on the x axis

useConfettiCannon

useConfettiCannon is the hook that will allow you to launch confetti. This takes a ConfettiCanvas and a SpriteCanvas and provides a few functions to create confetti.

Cannon methods

There's several methods available to add and manage confetti on the canvas. Typically, you'll want to use createMultipleConfetti.

MethodDescription
createConfettiCreate a single confetti and add it to the canvas
createMultipleConfettiCreate multiple confetti and add them to the canvas
addConfettiAdd a confetti on the canvas
deleteConfettiDelete a confetti from the canvas
clearConfettiDelete all confetti from the canvas

CreateConfettiArgs

These are passed to createConfetti methods to create a confetti and define how it is updated. Each includes a type, which then defines the rest of the args.

ArgTypeDefaultDescription
id?stringA uuidA unique id to a confetti if you want to reference it later, will be a uuid if not specified
positionConfigVector2InputN/AThe position to launch the confetti at and how it will be updated
velocity?ConfigVector2Inputstatic, 0The velocity to launch the confetti at and how it will be updated
rotation?ConfigVector3Inputstatic, 0The rotation to launch the confetti at and how it will be updated
dragCoefficient?ConfigVector2Inputstatic, 0.001The drag coefficient to launch the confetti at and how it will be updated. This effects how much gravity and wind effect the confetti
sizeConfigVector2InputN/AThe size to launch the confetti at and how it will be updated
opacity?ConfigNumberInputstatic 1The opacity to launch the confetti at and how it will be updated

Config Inputs Config inputs are helper objects that will eventually create an UpdatableValue that lives on Confetti that tells the Confetti how to update on every tick.

Valid types include:

  • static: Will not change on updates (gravity and wind will still effect relevant fields)
  • linear: Will update linearly on every update
  • oscillating: Will oscillate between two values with a given easing

Each type also includes a -random option (ex: static-random) which allows you to add randomization with the initial value and the update values.

Any Vector2 or Vector3 will also accept a number as a shortcut to set all x, y, and z values to that number.

CreateConfettiRequestedOptions

This is an optional object that will request that the canvas create a specific sprite or color. The sprite and color must be included on your sprite canvas for this to work.

ArgTypeDescription
sprite?Array<string \| {src: string, colorized: boolean}>The requested sprite
color?stringThe requested color
2.0.1

8 months ago

2.0.0

8 months ago

1.0.4

11 months ago

1.0.3

11 months ago

1.0.2

12 months ago

1.0.1

12 months ago

1.0.0

12 months ago