-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.jsx
149 lines (132 loc) · 4.33 KB
/
index.jsx
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import getCaretCoordinates from 'textarea-caret';
import ReactDOM from 'react-dom';
import throttle from 'lodash.throttle';
import React, { PropTypes } from 'react';
const MAX_PARTICLES = 500;
const PARTICLE_NUM_RANGE = () => 5 + Math.round(Math.random() * 5);
const PARTICLE_GRAVITY = 0.075;
const PARTICLE_ALPHA_FADEOUT = 0.96;
const PARTICLE_VELOCITY_RANGE = {
x: [-1, 1],
y: [-3.5, -1.5]
};
const COLORS = [
'#1f77b4',
'#ff7f0e',
'#2ca02c',
'#d62728',
'#9467bd',
'#8c564b',
'#e377c2',
'#bcbd22',
'#17becf'
];
class RagePower extends React.Component {
static propTypes = {
children: PropTypes.node,
onInput: PropTypes.func,
colors: PropTypes.array
}
static defaultProps = {
colors: COLORS
}
constructor(props, context) {
super(props, context);
this._drawFrame = this._drawFrame.bind(this);
this._onInput = this._onInput.bind(this);
this._shake = throttle(this._shake.bind(this), 100, { trailing: false });
this._spawnParticles = throttle(this._spawnParticles.bind(this), 25, { trailing: false });
this._particles = [];
}
componentDidMount() {
this.canvas = document.createElement('canvas');
this.canvas.width = window.innerWidth;
this.canvas.height = window.innerHeight;
this.canvas.style.position = 'absolute';
this.canvas.style.top = '0';
this.canvas.style.pointerEvents = 'none';
this.canvasContext = this.canvas.getContext('2d');
document.body.appendChild(this.canvas);
window.requestAnimationFrame(this._drawFrame);
}
componentWillUnmount() {
document.body.removeChild(this.canvas);
}
render() {
const { children, style, colors: _, ...others } = this.props;
const newChildren = React.cloneElement(children, {
onInput: this._onInput
});
return (
<div
{...others}
style={{ position: 'relative', ...style }}
ref={(ref) => this.node = ref}
>
{ newChildren }
</div>
);
}
/**
* Following code is ported from: https://atom.io/packages/power-mode
*/
_drawFrame() {
this.canvasContext.clearRect(0, 0, this.canvas.width, this.canvas.height);
this._particles.forEach((particle) => {
particle.velocity.y += PARTICLE_GRAVITY;
particle.x += particle.velocity.x;
particle.y += particle.velocity.y;
particle.alpha *= PARTICLE_ALPHA_FADEOUT;
this.canvasContext.fillStyle = `rgba(${particle.color.join(',')}, ${particle.alpha})`;
this.canvasContext.fillRect(Math.round(particle.x - 1), Math.round(particle.y - 1), 3, 3);
});
this._particles = this._particles
.slice(Math.max(this._particles.length - MAX_PARTICLES, 0))
.filter((particle) => particle.alpha > 0.1);
window.requestAnimationFrame(this._drawFrame);
}
_shake() {
const intensity = 1 + 2 * Math.random();
const x = intensity * (Math.random() > 0.5 ? -1 : 1);
const y = intensity * (Math.random() > 0.5 ? -1 : 1);
this.node.style.transform = `translate3d(${x}px, ${y}px, 0)`;
setTimeout(() => this.node.style.transform = '', 75);
}
_spawnParticles(x, y) {
const { colors } = this.props;
const numParticles = PARTICLE_NUM_RANGE();
for (let i = 0; i < numParticles; i++) {
const colorCode = colors[i % colors.length];
const r = parseInt(colorCode.slice(1, 3), 16);
const g = parseInt(colorCode.slice(3, 5), 16);
const b = parseInt(colorCode.slice(5, 7), 16);
const color = [r, g, b];
this._particles.push(this._createParticle(x, y, color));
}
}
_createParticle(x, y, color) {
return {
x,
y: y,
alpha: 1,
color,
velocity: {
x: PARTICLE_VELOCITY_RANGE.x[0] + Math.random() *
(PARTICLE_VELOCITY_RANGE.x[1] - PARTICLE_VELOCITY_RANGE.x[0]),
y: PARTICLE_VELOCITY_RANGE.y[0] + Math.random() *
(PARTICLE_VELOCITY_RANGE.y[1] - PARTICLE_VELOCITY_RANGE.y[0])
}
};
}
_onInput(...args) {
const { onInput } = this.props;
onInput && onInput(...args);
this._shake();
const target = args[0].target;
const origin = target.getBoundingClientRect();
const { top, left } = getCaretCoordinates(target, target.selectionEnd);
const charHeight = parseInt(getComputedStyle(target)['font-size']);
setTimeout(() => this._spawnParticles(left + origin.left, top + origin.top + charHeight), 0);
}
}
export default RagePower;