|
| 1 | +import React, { PropTypes } from 'react'; |
| 2 | + |
| 3 | +export default class Cube extends React.Component { |
| 4 | + constructor(props) { |
| 5 | + super(props); |
| 6 | + |
| 7 | + this.listeners = { |
| 8 | + spin: this._spin.bind(this), |
| 9 | + reset: this._reset.bind(this) |
| 10 | + }; |
| 11 | + |
| 12 | + this.state = { |
| 13 | + x: 0, |
| 14 | + y: 0, |
| 15 | + z: 0, |
| 16 | + iteration: 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('outer') } |
| 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('inner') } |
| 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 | + obj[axis] = degrees += 90; |
| 84 | + |
| 85 | + this.setState({ ...obj, iteration: (this.state.iteration + 1) % 4 }); |
| 86 | + }, repeatDelay); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + componentWillUnmount() { |
| 91 | + let { hover, continuous } = this.props; |
| 92 | + |
| 93 | + if (hover) { |
| 94 | + this.container.removeEventListener('mouseenter', this.listeners.spin); |
| 95 | + this.container.removeEventListener('mouseleave', this.listeners.reset); |
| 96 | + |
| 97 | + } else if (continuous) { |
| 98 | + clearInterval(this._interval); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Get all faces for a cube |
| 104 | + * |
| 105 | + * @param {'inner' | 'outer' } type |
| 106 | + * @return {array} - An array of nodes |
| 107 | + */ |
| 108 | + _getFaces(type) { |
| 109 | + let { iteration } = this.state; |
| 110 | + |
| 111 | + // Keep the thicker border on |
| 112 | + // the outside on each iteration |
| 113 | + const borderWidthMap = { |
| 114 | + 0: { |
| 115 | + left: [1, 1, 1, 6], |
| 116 | + right: [6, 1, 1, 1], |
| 117 | + top: [1, 1, 1, 1], |
| 118 | + bottom: [6, 1, 1, 6], |
| 119 | + }, |
| 120 | + 1: { |
| 121 | + left: [1, 1, 1, 1], |
| 122 | + right: [1, 1, 1, 1], |
| 123 | + top: [1, 1, 1, 1], |
| 124 | + bottom: [1, 1, 1, 1], |
| 125 | + }, |
| 126 | + 2: { |
| 127 | + left: [1, 1, 6, 6], |
| 128 | + right: [6, 6, 1, 1], |
| 129 | + top: [6, 1, 1, 6], |
| 130 | + bottom: [1, 6, 6, 1], |
| 131 | + }, |
| 132 | + 3: { |
| 133 | + left: [6, 1, 1, 1], |
| 134 | + right: [1, 6, 1, 1], |
| 135 | + top: [1, 1, 1, 1], |
| 136 | + bottom: [6, 6, 1, 1], |
| 137 | + }, |
| 138 | + 4: { |
| 139 | + left: [1, 1, 6, 1], |
| 140 | + right: [1, 1, 1, 6], |
| 141 | + top: [1, 1, 1, 1], |
| 142 | + bottom: [1, 1, 6, 6], |
| 143 | + }, |
| 144 | + 5: { |
| 145 | + left: [1, 6, 1, 1], |
| 146 | + right: [1, 1, 6, 1], |
| 147 | + top: [1, 1, 1, 1], |
| 148 | + bottom: [1, 6, 6, 1], |
| 149 | + } |
| 150 | + }; |
| 151 | + |
| 152 | + return [ |
| 153 | + 'rotateX(0deg)', |
| 154 | + 'rotateX(-90deg)', |
| 155 | + 'rotateX(90deg)', |
| 156 | + 'rotateY(-90deg)', |
| 157 | + 'rotateY(90deg)', |
| 158 | + 'rotateY(180deg)' |
| 159 | + ].map((rotation, i) => { |
| 160 | + const borderStyles = type === 'outer' ? { |
| 161 | + borderTopWidth: borderWidthMap[i].top[iteration], |
| 162 | + borderRightWidth: borderWidthMap[i].right[iteration], |
| 163 | + borderBottomWidth: borderWidthMap[i].bottom[iteration], |
| 164 | + borderLeftWidth: borderWidthMap[i].left[iteration], |
| 165 | + } : {}; |
| 166 | + |
| 167 | + return ( |
| 168 | + <section |
| 169 | + key={ i } |
| 170 | + className="cube__face" |
| 171 | + style={{ |
| 172 | + transform: `${rotation} translateZ(${ this.props.depth / 2 }px)`, |
| 173 | + ...borderStyles, |
| 174 | + }} /> |
| 175 | + ); |
| 176 | + }); |
| 177 | + } |
| 178 | + |
| 179 | + /** |
| 180 | + * Get a random axis |
| 181 | + * |
| 182 | + * @return {string} - A random axis (i.e. x, y, or z) |
| 183 | + */ |
| 184 | + _getRandomAxis() { |
| 185 | + let axes = Object.keys(this.state); |
| 186 | + |
| 187 | + return axes[ Math.floor(Math.random() * axes.length) ]; |
| 188 | + } |
| 189 | + |
| 190 | + /** |
| 191 | + * Spin the cubes in opposite directions semi-randomly |
| 192 | + * |
| 193 | + * @param {object} e - Native event |
| 194 | + */ |
| 195 | + _spin(e) { |
| 196 | + let obj = {}; |
| 197 | + let axis = this._getRandomAxis(); |
| 198 | + let sign = Math.random() < 0.5 ? -1 : 1; |
| 199 | + |
| 200 | + obj[axis] = sign * 90; |
| 201 | + |
| 202 | + this.setState(obj); |
| 203 | + } |
| 204 | + |
| 205 | + /** |
| 206 | + * Rotate the cubes back to their original position |
| 207 | + * |
| 208 | + * @param {object} e - Native event |
| 209 | + */ |
| 210 | + _reset(e) { |
| 211 | + this.setState({ |
| 212 | + x: 0, |
| 213 | + y: 0, |
| 214 | + z: 0 |
| 215 | + }); |
| 216 | + } |
| 217 | +} |
| 218 | + |
| 219 | +Cube.propTypes = { |
| 220 | + hover: PropTypes.bool, |
| 221 | + theme: PropTypes.string, |
| 222 | + depth: PropTypes.number, |
| 223 | + repeatDelay: PropTypes.number |
| 224 | +}; |
| 225 | + |
| 226 | +Cube.defaultProps = { |
| 227 | + hover: false, |
| 228 | + theme: 'dark', |
| 229 | + depth: 30, |
| 230 | + repeatDelay: 1000, |
| 231 | +}; |
0 commit comments