-
Notifications
You must be signed in to change notification settings - Fork 0
/
rotates.js
executable file
·71 lines (63 loc) · 1.83 KB
/
rotates.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
// map six sides (directions) to three coordinate axises
const DIR_MAP_TO_AXIS = {
'u': 'y',
'd': 'y',
'l': 'x',
'r': 'x',
'f': 'z',
'b': 'z'
}
// offsets from center point of six sides
const DIR_OFFSET = {
'u': 1,
'd': -1,
'l': -1,
'r': 1,
'f': 1,
'b': -1
}
const blocks = document.querySelectorAll('a-box')
const getTargetBlocks = (dir, axis) => {
return Array.prototype.filter.call(blocks, block => {
const targetCoor = centerCoor[axis] + DIR_OFFSET[dir]
return block.getAttribute('position')[axis] === targetCoor
})
}
const generateRotation = dir => {
return () => {
const axis = DIR_MAP_TO_AXIS[dir]
const blocks = getTargetBlocks(dir, axis)
const deg = axis === 'x'
? '360 0 0'
: (axis === 'y' ? '0 360 0' : '0 0 360')
blocks.forEach(block => {
block.setAttribute('rotation', {
x: 0,
y: 0,
z: 0
})
block.setAttribute('pivot-point', {
x: (axis === 'x' ? 0 : centerCoor.x - block.getAttribute('position').x),
y: (axis === 'y' ? 0 : centerCoor.y - block.getAttribute('position').y),
z: (axis === 'z' ? 0 : centerCoor.z - block.getAttribute('position').z)
})
const oldAnimation = block.getElementsByTagName('a-animation')[0]
if (oldAnimation) {
block.removeChild(oldAnimation)
}
const animation = document.createElement('a-animation')
animation.setAttribute('attribute', 'rotation')
animation.setAttribute('dur', 1500)
animation.setAttribute('repeat', 0)
animation.setAttribute('to', deg)
block.appendChild(animation)
})
}
}
const U = generateRotation('u')
const R = generateRotation('r')
const F = generateRotation('f')
const L = generateRotation('l')
const D = generateRotation('d')
const B = generateRotation('b')
const Rotate = { U, R, F, L, D, B }