-
Notifications
You must be signed in to change notification settings - Fork 101
/
Copy pathreact-confetti.js
120 lines (114 loc) · 2.79 KB
/
react-confetti.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import React from 'react'
import PropTypes from 'prop-types'
import confetti from './confetti'
export default class ReactConfetti extends React.PureComponent {
static propTypes = {
style: PropTypes.object,
width: PropTypes.number,
height: PropTypes.number,
numberOfPieces: PropTypes.number,
confettiSource: PropTypes.shape({
x: PropTypes.number,
y: PropTypes.number,
w: PropTypes.number,
h: PropTypes.number,
}),
friction: PropTypes.number,
wind: PropTypes.number,
gravity: PropTypes.number,
colors: PropTypes.arrayOf(PropTypes.string),
opacity: PropTypes.number,
recycle: PropTypes.bool,
run: PropTypes.bool,
}
static defaultProps = {
width: typeof window !== 'undefined' ? window.innerWidth : 300,
height: typeof window !== 'undefined' ? window.innerHeight : 200,
numberOfPieces: 200,
confettiSource: {},
friction: 0.99,
wind: 0,
gravity: 0.1,
colors: [
'#f44336', '#e91e63', '#9c27b0', '#673ab7', '#3f51b5',
'#2196f3', '#03a9f4', '#00bcd4', '#009688', '#4CAF50',
'#8BC34A', '#CDDC39', '#FFEB3B', '#FFC107', '#FF9800',
'#FF5722', '#795548',
],
opacity: 1.0,
recycle: true,
run: true,
}
componentDidMount() {
const {
numberOfPieces,
confettiSource,
friction,
wind,
gravity,
colors,
opacity,
recycle,
run,
} = this.props
this.confetti = confetti(this.canvas)
.numberOfPieces(numberOfPieces)
.confettiSource(confettiSource)
.friction(friction)
.wind(wind)
.gravity(gravity)
.colors(colors)
.opacity(opacity)
.recycle(recycle)
.run(run)()
}
componentWillReceiveProps(nextProps) {
this.confetti
.numberOfPieces(nextProps.numberOfPieces)
.confettiSource(nextProps.confettiSource)
.friction(nextProps.friction)
.wind(nextProps.wind)
.gravity(nextProps.gravity)
.colors(nextProps.colors)
.opacity(nextProps.opacity)
.recycle(nextProps.recycle)
.run(nextProps.run)
}
render() {
const {
width,
height,
style,
/* eslint-disable no-unused-vars */
numberOfPieces,
friction,
wind,
gravity,
colors,
opacity,
recycle,
confettiSource,
run,
/* eslint-enable no-unused-vars */
...passedProps
} = this.props
const canvasStyles = Object.assign({}, {
zIndex: 2,
position: 'absolute',
top: 0,
left: 0,
pointerEvents: 'none',
WebkitPointerEvents: 'none',
MozPointerEvents: 'none',
}, style)
return (
<canvas
width={width}
height={height}
ref={c => { this.canvas = c }}
style={canvasStyles}
{...passedProps}
/>
)
}
}