-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.js
233 lines (213 loc) · 5.56 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/**
* Layout component for A-Frame.
* Some layouts adapted from http://www.vb-helper.com/tutorial_platonic_solids.html
*/
AFRAME.registerComponent('layout', {
schema: {
columns: {default: 1, min: 0, if: {type: ['box']}},
margin: {default: 1, min: 0, if: { type: ['box', 'line']}},
radius: {default: 1, min: 0, if: {
type: ['circle', 'cube', 'dodecahedron', 'pyramid']
}},
type: {default: 'line', oneOf: [
'box', 'circle', 'cube', 'dodecahedron', 'line', 'pyramid'
]}
},
/**
* Store initial positions in case need to reset on component removal.
*/
init: function () {
var el = this.el;
this.children = el.getChildEntities();
var initialPositions = initialPositions = [];
this.children.forEach(function (childEl) {
initialPositions.push(childEl.getComputedAttribute('position'));
});
this.childAttachedCallback = this.update.bind(this);
el.addEventListener('child-attached', this.childAttachedCallback);
},
/**
* Update child entity positions.
*/
update: function (oldData) {
var children = this.children;
var data = this.data;
var el = this.el;
var numChildren = children.length;
var positionFn;
var positions;
var startPosition = el.getComputedAttribute('position');
// Calculate different positions based on layout shape.
switch (data.type) {
case 'box': {
positionFn = getBoxPositions;
break;
}
case 'circle': {
positionFn = getCirclePositions;
break;
}
case 'cube': {
positionFn = getCubePositions;
break;
}
case 'dodecahedron': {
positionFn = getDodecahedronPositions;
break;
}
case 'pyramid': {
positionFn = getPyramidPositions;
break;
}
default: {
// Line.
positionFn = getLinePositions;
}
}
positions = positionFn(data, numChildren, startPosition);
setPositions(children, positions);
},
/**
* Reset positions.
*/
remove: function () {
el.removeEventListener('child-attached', this.childAttachedCallback);
setPositions(children, this.initialPositions);
}
});
/**
* Get positions for `box` layout.
*/
function getBoxPositions (data, numChildren, startPosition) {
var positions = [];
var rows = Math.ceil(numChildren / data.columns);
for (var row = 0; row < rows; row++) {
for (var column = 0; column < data.columns; column++) {
positions.push([
column * data.margin,
row * data.margin,
0
]);
}
}
return positions;
}
module.exports.getBoxPositions = getBoxPositions;
/**
* Get positions for `circle` layout.
* TODO: arcLength.
*/
function getCirclePositions (data, numChildren, startPosition) {
var positions = [];
for (var i = 0; i < numChildren; i++) {
var rad = i * (2 * Math.PI) / numChildren;
positions.push([
startPosition.x + data.radius * Math.cos(rad),
startPosition.y,
startPosition.z + data.radius * Math.sin(rad)
]);
}
return positions;
}
module.exports.getCirclePositions = getCirclePositions;
/**
* Get positions for `line` layout.
* TODO: 3D margins.
*/
function getLinePositions (data, numChildren, startPosition) {
data.columns = numChildren;
return getBoxPositions(data, numChildren, startPosition);
}
module.exports.getLinePositions = getLinePositions;
/**
* Get positions for `cube` layout.
*/
function getCubePositions (data, numChildren, startPosition) {
return transform([
[1, 0, 0],
[0, 1, 0],
[0, 0, 1],
[-1, 0, 0],
[0, -1, 0],
[0, 0, -1],
], startPosition, data.radius / 2);
}
module.exports.getCubePositions = getCubePositions;
/**
* Get positions for `dodecahedron` layout.
*/
function getDodecahedronPositions (data, numChildren, startPosition) {
var PHI = (1 + Math.sqrt(5)) / 2;
var B = 1 / PHI;
var C = 2 - PHI;
var NB = -1 * B;
var NC = -1 * C;
return transform([
[-1, C, 0],
[-1, NC, 0],
[0, -1, C],
[0, -1, NC],
[0, 1, C],
[0, 1, NC],
[1, C, 0],
[1, NC, 0],
[B, B, B],
[B, B, NB],
[B, NB, B],
[B, NB, NB],
[C, 0, 1],
[C, 0, -1],
[NB, B, B],
[NB, B, NB],
[NB, NB, B],
[NB, NB, NB],
[NC, 0, 1],
[NC, 0, -1],
], startPosition, data.radius / 2);
}
module.exports.getDodecahedronPositions = getDodecahedronPositions;
/**
* Get positions for `pyramid` layout.
*/
function getPyramidPositions (data, numChildren, startPosition) {
var SQRT_3 = Math.sqrt(3);
var NEG_SQRT_1_3 = -1 / Math.sqrt(3);
var DBL_SQRT_2_3 = 2 * Math.sqrt(2 / 3);
return transform([
[0, 0, SQRT_3 + NEG_SQRT_1_3],
[-1, 0, NEG_SQRT_1_3],
[1, 0, NEG_SQRT_1_3],
[0, DBL_SQRT_2_3, 0]
], startPosition, data.radius / 2);
}
module.exports.getPyramidPositions = getPyramidPositions;
/**
* Multiply all coordinates by a scale factor and add translate.
*
* @params {array} positions - Array of coordinates in array form.
* @returns {array} positions
*/
function transform (positions, translate, scale) {
translate = [translate.x, translate.y, translate.z];
return positions.map(function (position) {
return position.map(function (point, i) {
return point * scale + translate[i];
});
});
};
/**
* Set position on child entities.
*
* @param {array} els - Child entities to set.
* @param {array} positions - Array of coordinates.
*/
function setPositions (els, positions) {
els.forEach(function (el, i) {
var position = positions[i];
el.setAttribute('position', {
x: position[0],
y: position[1],
z: position[2]
});
});
}