-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
127 lines (121 loc) · 3.91 KB
/
index.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
var $canvas = document.getElementById('canvas');
var $center = document.querySelector('#canvas > .row > .instance');
var $row = document.querySelector('#canvas > .row');
var centerLeft = 2200;
var centerTop = 1300;
var spaceV = 168;
var spaceH = 228;
var deg = 22.5;
var right = 8;
var left = 8;
var rowsUp = 7;
var rowsDown = 7;
var i = 0;
var num = 1;
var $clone;
var $instances;
rowHTML = '';
canvasHTML = '';
// debugging
// right = 1;
// left = 1;
// rowsUp = 1;
// rowsDown = 1;
// centerLeft = 500;
function render(relative){
// turn to the right
for(i=0;i<right;i++){
num = i+1;
$clone = $center.cloneNode(true);
$clone.style.left = (centerLeft + (spaceH*num));
$rect = $clone.childNodes[1].childNodes[1];
$rect.style.transform = "rotateY("+(deg*num)+"deg)";
rowHTML += $clone.outerHTML;
}
// // turn to the left
for(i=0;i<left;i++){
num = i+1;
$clone = $center.cloneNode(true);
$clone.style.left = (centerLeft - (spaceH*num));
$rect = $clone.childNodes[1].childNodes[1];
$rect.style.transform = "rotateY("+(-deg*num)+"deg)";
rowHTML += $clone.outerHTML;
}
$row.insertAdjacentHTML('beforeend', rowHTML);
// // row up
for(i=0;i<rowsUp;i++){
num = i+1;
$clone = $row.cloneNode(true);
$clone.style.top = (centerTop - (spaceV*num));
$instances = $clone.childNodes;
transform = "rotateX("+(deg*num)+"deg)";
Array.prototype.forEach.call($instances, function(el, i){
if(el.className!=='instance'){
return;
}
var rectWrap = el.childNodes[1];
var rect = rectWrap.childNodes[1];
// store the dynamic rotations in the data- attributes
// so we can toggle relative/absolute angles
rect.dataset.relative = rect.style.transform + ' ' + transform;
rect.dataset.absolute = rect.style.transform;
// we will only place this transform in absolute mode
rectWrap.dataset.relative = '';
rectWrap.dataset.absolute = transform;
// add dynamic class for filtering later
rectWrap.className+=' dynamic';
rect.className+=' dynamic';
// start off in relative mode
rect.style.transform = rect.dataset.relative;
});
canvasHTML += $clone.outerHTML;
}
// // row down
for(i=0;i<rowsDown;i++){
num = i+1;
$clone = $row.cloneNode(true);
$clone.style.top = (centerTop + (spaceV*num));
$instances = $clone.childNodes;
transform = "rotateX("+(-deg*num)+"deg)";
Array.prototype.forEach.call($instances, function(el){
if(el.className!=='instance'){
return;
}
var rectWrap = el.childNodes[1];
var rect = rectWrap.childNodes[1];
rect.dataset.relative = rect.style.transform + ' ' + transform;
rect.dataset.absolute = rect.style.transform;
rectWrap.dataset.relative = '';
rectWrap.dataset.absolute = transform;
rectWrap.className+=' dynamic';
rect.className+=' dynamic';
// start off in relative mode
rect.style.transform = rect.dataset.relative;
});
canvasHTML += $clone.outerHTML;
}
$canvas.insertAdjacentHTML('beforeend', canvasHTML);
}
// controls
var sheet = document.styleSheets[1];
var rules = sheet.cssRules||sheet.rules;
var $perspective = document.getElementById('perspective');
$perspective.addEventListener('change', function(e, l){
console.log('perspective', e.target.value);
rules[0].style.perspective = e.target.value+'px';
})
var $canvasZoom = document.getElementById('zoom');
$canvasZoom.addEventListener('change', function(e, l){
console.log('zoom', e.target.value);
rules[1].style.zoom = e.target.value;
});
var $relativeTilt = document.getElementById('relative');
$relativeTilt.addEventListener('change', function(e, l){
$elems = document.querySelectorAll('.dynamic');
var state = e.target.checked ? 'relative' : 'absolute';
Array.prototype.forEach.call($elems, function(el){
// update the transformations
el.style.transform = el.dataset[state];
});
});
render(true);