Skip to content

Commit bfc2ff5

Browse files
committed
feat: add initialVelocityX and initialVelocityY props
Add `initialVelocityX` and `initialVelocityY` knobs to stories.
1 parent 65e1560 commit bfc2ff5

File tree

5 files changed

+47
-3
lines changed

5 files changed

+47
-3
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ export default () => {
5252
| `friction` | `Number` | 0.99 | |
5353
| `wind` | `Number` | 0 | |
5454
| `gravity` | `Number` | 0.1 | |
55+
| `initialVelocityX` | `Number` | 4 | How fast confetti is emitted horizontally |
56+
| `initialVelocityY` | `Number` | 10 | How fast confetti is emitted vertically |
5557
| `colors` | `String[]` | `['#f44336'`</br>`'#e91e63'`</br>`'#9c27b0'`</br>`'#673ab7'`</br>`'#3f51b5'`</br>`'#2196f3'`</br>`'#03a9f4'`</br>`'#00bcd4'`</br>`'#009688'`</br>`'#4CAF50'`</br>`'#8BC34A'`</br>`'#CDDC39'`</br>`'#FFEB3B'`</br>`'#FFC107'`</br>`'#FF9800'`</br>`'#FF5722'`</br>`'#795548']`</br> | All available Colors for the confetti pieces. |
5658
| `opacity` | `Number` | 1.0 | |
5759
| `recycle` | `Bool` | true | Keep spawning confetti after `numberOfPieces` pieces have been shown. |

src/Confetti.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,16 @@ export interface IConfettiOptions {
3333
* @default 0.1
3434
*/
3535
gravity: number
36+
/**
37+
* How fast the confetti is emitted horizontally
38+
* @default 4
39+
*/
40+
initialVelocityX: number
41+
/**
42+
* How fast the confetti is emitted vertically
43+
* @default 10
44+
*/
45+
initialVelocityY: number
3646
/**
3747
* Array of colors to choose from.
3848
*/
@@ -94,6 +104,8 @@ export const confettiDefaults: Pick<IConfettiOptions, Exclude<keyof IConfettiOpt
94104
friction: 0.99,
95105
wind: 0,
96106
gravity: 0.1,
107+
initialVelocityX: 4,
108+
initialVelocityY: 10,
97109
colors: [
98110
'#f44336', '#e91e63', '#9c27b0', '#673ab7', '#3f51b5',
99111
'#2196f3', '#03a9f4', '#00bcd4', '#009688', '#4CAF50',

src/Particle.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,19 @@ enum RotationDirection {
1515
export default class Particle {
1616
constructor(context: CanvasRenderingContext2D, getOptions: () => IConfettiOptions, x: number, y: number) {
1717
this.getOptions = getOptions
18-
const { colors } = this.getOptions()
18+
const {
19+
colors,
20+
initialVelocityX,
21+
initialVelocityY,
22+
} = this.getOptions()
1923
this.context = context
2024
this.x = x
2125
this.y = y
2226
this.w = randomRange(5, 20)
2327
this.h = randomRange(5, 20)
2428
this.radius = randomRange(5, 10)
25-
this.vx = randomRange(-4, 4)
26-
this.vy = randomRange(-10, -0)
29+
this.vx = randomRange(-initialVelocityX, initialVelocityX)
30+
this.vy = randomRange(-initialVelocityY, 0)
2731
this.shape = randomInt(0, 2)
2832
this.angle = degreesToRads(randomRange(0, 360))
2933
this.angularSpin = randomRange(-0.2, 0.2)

stories/index.story.jsx

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,18 @@ storiesOf('Props|Demos', module)
4949
max: 1,
5050
step: 0.01,
5151
})}
52+
initialVelocityX={number('Initial X', 4, {
53+
range: true,
54+
min: 0,
55+
max: 10,
56+
step: 0.1,
57+
})}
58+
initialVelocityY={number('Initial Y', 10, {
59+
range: true,
60+
min: 0,
61+
max: 20,
62+
step: 0.1,
63+
})}
5264
opacity={number('Opacity', 100, {
5365
range: true,
5466
min: 0,
@@ -83,6 +95,18 @@ storiesOf('Props|Demos', module)
8395
max: 1,
8496
step: 0.01,
8597
})}
98+
initialVelocityX={number('Initial X', 2, {
99+
range: true,
100+
min: 0,
101+
max: 10,
102+
step: 0.1,
103+
})}
104+
initialVelocityY={number('Initial Y', 5, {
105+
range: true,
106+
min: 0,
107+
max: 20,
108+
step: 0.1,
109+
})}
86110
opacity={number('Opacity', 100, {
87111
range: true,
88112
min: 0,

stories/mouse-rain.story.jsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ const MouseRain = () => {
2222
<SizedConfetti
2323
style={{ pointerEvents: 'none' }}
2424
numberOfPieces={100}
25+
initialVelocityX={2}
26+
initialVelocityY={5}
2527
ref={ref}
2628
gravity={0.5}
2729
{...activeProps}

0 commit comments

Comments
 (0)