-
Notifications
You must be signed in to change notification settings - Fork 6
/
cubes.js
132 lines (100 loc) · 3.54 KB
/
cubes.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
121
122
123
124
125
126
127
128
129
130
131
132
var vw = window.innerWidth
var $plane = document.getElementById('plane')
var $grid = document.getElementById('grid')
var $cubes = document.getElementById('cubes')
var $cubeModel = document.querySelector('.cube')
var $colorize = document.getElementById('colorize')
var $colorPreview = document.getElementById('color-preview')
var color = '#ffaa22'
var widthInSpaces = 10
var heightInSpaces = 10
var spaceSize = $plane.clientWidth / widthInSpaces
var totalSpaces = widthInSpaces * heightInSpaces
/** METHODS **/
function addSpace(index) {
var $space = document.createElement('div')
$space.classList.add('space')
var x = index === 0 ? 0 : (index % widthInSpaces) * spaceSize
var y = index === 0 ? 0 : Math.floor(index / widthInSpaces) * spaceSize
$space.style.left = x + 'px'
$space.style.top = y + 'px'
$grid.appendChild($space)
$space.addEventListener('mouseenter', function(e) { e.target.style.backgroundColor = color })
$space.addEventListener('mouseleave', function(e) { e.target.style.backgroundColor = 'transparent' })
$space.addEventListener('click', addCube)
}
function addCube(e) {
e.preventDefault()
var target = e.target
var layer = 0
var cubeTranslation = 'translate3d(0, 0, 0)'
if (e.target.classList.contains('face')) {
target = e.target.parentNode
layer = parseInt(target.getAttribute('data-layer')) + 1
cubeTranslation = `translate3d(0, 0, ${layer * spaceSize}px)`
}
var $cube = $cubeModel.cloneNode(true)
var $face1 = $cube.querySelector('.face-1')
$cube.classList.remove('is--hidden')
$cube.setAttribute('data-layer', layer)
$cube.style.left = target.style.left
$cube.style.top = target.style.top
$cube.style.transform = cubeTranslation
$face1.addEventListener('click', addCube)
colorizeCube($cube)
$cubes.appendChild($cube)
}
function colorizeCube($cube) {
var $faces = Array.from($cube.querySelectorAll('.face'))
$faces.forEach(function($face, i) {
$face.style.backgroundColor = getColorNeighbor(color, i)
})
}
function getRandomColor() {
var chars = '0123456789abcdef'.split('')
var hex = '#'
for (var n = 0; n < 6; n++) {
var rand = Math.round(Math.random() * (chars.length - 1))
hex += chars[rand]
}
return hex
}
function getColorNeighbor(hex, n) {
var chars = '0123456789abcdef'.split('')
var hexArr = hex.replace('#', '').split('')
var big = (n % 3) * 2
var small = (n % 2) === 0 ? big + 1 : big - 1
if (small === -1) small = 5
var neighborBig = hexArr[big] === 'f' ? 'e' : chars[chars.indexOf(hexArr[big]) + 1]
var neighborSmall = hexArr[big] === 'f' ? 'e' : chars[chars.indexOf(hexArr[big]) + 1]
hexArr[big] = neighborBig
hexArr[small] = neighborSmall
return '#' + hexArr.join('')
}
function isMobileAt(vw) {
return vw < 500
}
/** EVENT LISTENERS **/
window.addEventListener('resize', function() {
if (window.innerWidth === vw) return
if (isMobileAt(vw) === isMobileAt(window.innerWidth)) return
vw = window.innerWidth
var conversion = isMobileAt(vw) ? 3 / 5 : 5 / 3
var $cubes = Array.from(document.querySelectorAll('.cube'))
var $spaces = Array.from(document.querySelectorAll('.space'))
var $nodes = $cubes.concat($spaces)
$nodes.forEach(function($node) {
$node.style.left = parseInt($node.style.left) * conversion + 'px'
$node.style.top = parseInt($node.style.top) * conversion + 'px'
})
})
$colorize.addEventListener('click', function() {
color = getRandomColor()
$colorPreview.style.backgroundColor = color
})
/** RUN **/
var spaceIndex = 0
while (spaceIndex < totalSpaces) {
addSpace(spaceIndex)
spaceIndex ++
}