Skip to content

Commit ed495dd

Browse files
committed
Wrap Cube in a container, and add repeatDelay
1 parent 2982f27 commit ed495dd

File tree

1 file changed

+184
-0
lines changed

1 file changed

+184
-0
lines changed

components/cube/cube.jsx

Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
import React, { PropTypes } from 'react';
2+
import './cube-style';
3+
4+
export default class Cube extends React.Component {
5+
constructor(props) {
6+
super(props);
7+
8+
this.listeners = {
9+
spin: this._spin.bind(this),
10+
reset: this._reset.bind(this)
11+
};
12+
13+
this.state = {
14+
x: 0,
15+
y: 0,
16+
z: 0
17+
};
18+
}
19+
20+
render() {
21+
let { x, y, z } = this.state;
22+
let { theme, depth, className = '' } = this.props;
23+
24+
return (
25+
<div
26+
className={ `cube__container ${className}` }
27+
style={{
28+
width: `${depth * 1.5}px`,
29+
height: `${depth * 1.5}px`,
30+
paddingLeft: `${depth / 1.7}px`
31+
}}>
32+
<span
33+
ref={ ref => this.container = ref }
34+
className={ `cube cube--${theme}` }
35+
style={{
36+
width: `${depth}px`,
37+
paddingBottom: `${depth * 0.5}px`
38+
}}>
39+
<figure
40+
className="cube__outer"
41+
style={{
42+
width: `${depth}px`,
43+
height: `${depth}px`,
44+
transform: `translateX(-50%)
45+
scale3d(1,1,1)
46+
rotateX(${x}deg)
47+
rotateY(${y}deg)
48+
rotateZ(${z}deg)`
49+
}}>
50+
{ this._getFaces() }
51+
</figure>
52+
<figure
53+
className="cube__inner"
54+
style={{
55+
width: `${depth}px`,
56+
height: `${depth}px`,
57+
transform: `translateX(-50%)
58+
scale3d(0.5,0.5,0.5)
59+
rotateX(${-x}deg)
60+
rotateY(${-y}deg)
61+
rotateZ(${-z}deg)`
62+
}}>
63+
{ this._getFaces() }
64+
</figure>
65+
</span>
66+
</div>
67+
);
68+
}
69+
70+
componentDidMount() {
71+
let { hover, continuous, repeatDelay } = this.props;
72+
73+
if (hover) {
74+
this.container.addEventListener('mouseenter', this.listeners.spin);
75+
this.container.addEventListener('mouseleave', this.listeners.reset);
76+
77+
} else if (continuous) {
78+
let degrees = 0;
79+
let axis = 'y';
80+
81+
this._interval = setInterval(() => {
82+
let obj = {};
83+
let sign = Math.random() < 0.5 ? -1 : 1;
84+
85+
obj[axis] = degrees += 90;
86+
87+
// axis = axis === 'x' ? 'y' : axis === 'y' ? 'z' : 'x';
88+
89+
this.setState(obj);
90+
console.log('cube spin');
91+
}, repeatDelay);
92+
}
93+
}
94+
95+
componentWillUnmount() {
96+
let { hover, continuous } = this.props;
97+
98+
if (hover) {
99+
this.container.removeEventListener('mouseenter', this.listeners.spin);
100+
this.container.removeEventListener('mouseleave', this.listeners.reset);
101+
102+
} else if (continuous) {
103+
clearInterval(this._interval);
104+
}
105+
}
106+
107+
/**
108+
* Get all faces for a cube
109+
*
110+
* @return {array} - An array of nodes
111+
*/
112+
_getFaces() {
113+
return [
114+
'rotateX(0deg)',
115+
'rotateX(-90deg)',
116+
'rotateX(90deg)',
117+
'rotateY(-90deg)',
118+
'rotateY(90deg)',
119+
'rotateY(180deg)'
120+
].map((rotation, i) => {
121+
return (
122+
<section
123+
key={ i }
124+
className="cube__face"
125+
style={{
126+
transform: `${rotation} translateZ(${ this.props.depth / 2 }px)`
127+
}} />
128+
);
129+
});
130+
}
131+
132+
/**
133+
* Get a random axis
134+
*
135+
* @return {string} - A random axis (i.e. x, y, or z)
136+
*/
137+
_getRandomAxis() {
138+
let axes = Object.keys(this.state);
139+
140+
return axes[ Math.floor(Math.random() * axes.length) ];
141+
}
142+
143+
/**
144+
* Spin the cubes in opposite directions semi-randomly
145+
*
146+
* @param {object} e - Native event
147+
*/
148+
_spin(e) {
149+
let obj = {};
150+
let axis = this._getRandomAxis();
151+
let sign = Math.random() < 0.5 ? -1 : 1;
152+
153+
obj[axis] = sign * 90;
154+
155+
this.setState(obj);
156+
}
157+
158+
/**
159+
* Rotate the cubes back to their original position
160+
*
161+
* @param {object} e - Native event
162+
*/
163+
_reset(e) {
164+
this.setState({
165+
x: 0,
166+
y: 0,
167+
z: 0
168+
});
169+
}
170+
}
171+
172+
Cube.propTypes = {
173+
hover: PropTypes.bool,
174+
theme: PropTypes.string,
175+
depth: PropTypes.number,
176+
repeatDelay: PropTypes.number
177+
};
178+
179+
Cube.defaultProps = {
180+
hover: false,
181+
theme: 'dark',
182+
depth: 30,
183+
repeatDelay: 1000,
184+
};

0 commit comments

Comments
 (0)