Skip to content

Commit

Permalink
Adding custom colors, small perf refacto and fadeout (#2)
Browse files Browse the repository at this point in the history
* adding custom colors, small perf refacto and fadeout

* update doc

* fixing review

* fixing review

* fixing review

* fixing review

* cleaning synthax

* cleaning synthax
  • Loading branch information
alanlanglois authored and VincentCATILLON committed Mar 28, 2019
1 parent 5b3de4f commit d4b0a38
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 27 deletions.
14 changes: 8 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ const MyComponent = () => (

## Props

| Name | Type | Description | Required | Default |
|----------------|------------------------|--------------------------------------------|----------|---------|
| count | number | items count to display | required | |
| origin | {x: number, y: number} | animation position origin | required | |
| explosionSpeed | number | explosion duration (ms) from origin to top | | 350 |
| fallSpeed | number | fall duration (ms) from top to bottom | | 3000 |
| Name | Type | Description | Required | Default |
|----------------|------------------------|--------------------------------------------|----------|----------------|
| count | number | items count to display | required | |
| origin | {x: number, y: number} | animation position origin | required | |
| explosionSpeed | number | explosion duration (ms) from origin to top | | 350 |
| fallSpeed | number | fall duration (ms) from top to bottom | | 3000 |
| fadeOut | boolean | make the confettis disappear at the end | | false |
| colors | Array<string> | give your own colors to the confettis | | default colors |
24 changes: 7 additions & 17 deletions src/components/confetti.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,24 @@ import {randomValue} from '../utils';
type Props = {|
left: Animated.Interpolation,
bottom: Animated.Interpolation,
transform: Array<{ [key: string]: Animated.Interpolation }>
transform: Array<{ [key: string]: Animated.Interpolation }>,
color: string,
opacity: Animated.Interpolation,
|};

const colors: Array<string> = [
'#e67e22',
'#2ecc71',
'#3498db',
'#84AAC2',
'#E6D68D',
'#F67933',
'#42A858',
'#4F50A2',
'#A86BB7',
'#e74c3c',
'#1abc9c'
];


class Confetti extends React.PureComponent<Props> {
props: Props;
width: number = randomValue(8, 16);
height: number = randomValue(6, 12);
isRounded: boolean = Math.round(randomValue(0, 1)) === 1;
backgroundColor: string = colors[Math.round(randomValue(0, colors.length - 1))];
backgroundColor: string = this.props.color;

render() {
const { left, bottom, transform } = this.props;
const { left, bottom, transform, opacity } = this.props;
const { width, height, isRounded, backgroundColor } = this;
const style = { left, bottom, width, height, backgroundColor, transform };
const style = { left, bottom, width, height, backgroundColor, transform, opacity};

return (
<Animated.View style={[styles.confetti, isRounded && styles.rounded, style]} />
Expand Down
29 changes: 25 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ type Props = {|
y: number
},
explosionSpeed?: number,
fallSpeed?: number
fallSpeed?: number,
colors?: Array<string>,
fadeOut?: boolean
|};

type Item = {|
Expand All @@ -32,6 +34,19 @@ type State = {|
|};

const TOP_MIN = 0.7;
const DEFAULT_COLORS: Array<string> =[
'#e67e22',
'#2ecc71',
'#3498db',
'#84AAC2',
'#E6D68D',
'#F67933',
'#42A858',
'#4F50A2',
'#A86BB7',
'#e74c3c',
'#1abc9c'
];

class Explosion extends React.PureComponent<Props, State> {
props: Props;
Expand Down Expand Up @@ -88,9 +103,9 @@ class Explosion extends React.PureComponent<Props, State> {
};

render() {
const { origin } = this.props;
const { origin, colors = DEFAULT_COLORS, fadeOut } = this.props;
const { height, width } = Dimensions.get('window');

return (
<React.Fragment>
{this.state && this.state.items && this.state.items.map((item: Item, index: number) => {
Expand Down Expand Up @@ -118,10 +133,16 @@ class Explosion extends React.PureComponent<Props, State> {
inputRange: [0, 0.4, 1.2, 2],
outputRange: [0, -(item.swingDelta * 30), (item.swingDelta * 30), 0]
})

const opacity = this.animation.interpolate({
inputRange: [0, 1, 1.8, 2],
outputRange: [1, 1, 1, fadeOut ? 0 : 1]
});

const transform = [{rotateX}, {rotateY}, {rotateZ}, {translateX}];

return (
<Confetti left={left} bottom={bottom} transform={transform} key={index} />
<Confetti color={colors[Math.round(randomValue(0, colors.length - 1))]} left={left} bottom={bottom} transform={transform} opacity={opacity} key={index} />
);
})}
</React.Fragment>
Expand Down

0 comments on commit d4b0a38

Please sign in to comment.