forked from timhutton/zomes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
original.html
424 lines (367 loc) · 12.8 KB
/
original.html
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
.slidecontainer {
width: 100%; /* Width of the outside container */
}
/* The slider itself */
.slider {
-webkit-appearance: none; /* Override default CSS styles */
appearance: none;
width: 100%; /* Full-width */
height: 25px; /* Specified height */
background: #d3d3d3; /* Grey background */
outline: none; /* Remove outline */
opacity: 0.7; /* Set transparency (for mouse-over effects on hover) */
-webkit-transition: .2s; /* 0.2 seconds transition on hover */
transition: opacity .2s;
}
/* Mouse-over effects */
.slider:hover {
opacity: 1; /* Fully shown on mouse-over */
}
/* The slider handle (use -webkit- (Chrome, Opera, Safari, Edge) and -moz- (Firefox) to override default look) */
.slider::-webkit-slider-thumb {
-webkit-appearance: none; /* Override default look */
appearance: none;
width: 25px; /* Set a specific slider handle width */
height: 25px; /* Slider handle height */
background: #4CAF50; /* Green background */
cursor: pointer; /* Cursor on hover */
}
.slider::-moz-range-thumb {
width: 25px; /* Set a specific slider handle width */
height: 25px; /* Slider handle height */
background: #4CAF50; /* Green background */
cursor: pointer; /* Cursor on hover */
}
</style>
<script>
function p3( x, y, z ) { return { x:x, y:y, z:z }; }
function p2( x, y ) { return { x:x, y:y, z:0 }; }
function origin() { return p3(0,0,0); }
function xaxis() { return p3(1,0,0); }
function yaxis() { return p3(0,1,0); }
function zaxis() { return p3(0,0,1); }
function add( a, b ) { return p3( a.x + b.x, a.y + b.y, a.z + b.z ); }
function sub( a, b ) { return p3( a.x - b.x, a.y - b.y, a.z - b.z ); }
function mul( a, f ) { return p3( a.x * f, a.y * f, a.z * f ); }
function dot( a, b ) { return a.x * b.x + a.y * b.y + a.z * b.z; }
function len2( a ) { return dot(a,a); }
function dist( a, b ) { return Math.sqrt( len2( sub( a, b ) ) ); }
function assign( a, b ) { a.x = b.x; a.y = b.y; a.z = b.z; }
function normalize( a ) { return mul( a, 1 / Math.sqrt( len2( a ) ) ); }
function cross( a, b ) { return p3( a.y * b.z - a.z * b.y, a.z*b.x - a.x * b.z, a.x * b.y - a.y * b.x ); }
function mid( a, b ) { return mul( add( a, b ), 0.5 ); }
function angle( p1, p2, p3 ) { var a = dist(p2,p3); var b = dist(p1,p2); var c = dist(p1,p3); return Math.acos( ( c*c - a*a - b*b ) / ( -2*a*b ) ); }
function rot2d( v, theta ) { var s = Math.sin(theta); var c = Math.cos(theta); return p2( v.x*c - v.y*s, v.x*s + v.y*c ); }
function clear() {
verts = [];
verts2d = [];
edges = [];
faces = [];
angles = [];
}
function getMousePos(canvas, evt) {
var rect = canvas.getBoundingClientRect();
return p2( evt.clientX - rect.left, evt.clientY - rect.top );
}
function onMouseMove( evt ) {
var pos = getMousePos( canvas, evt );
view_height = 1.3 * ( canvas.height / 2.0 - pos.y ) / canvas.height;
repositionCamera();
}
function onMouseDown( evt ) {
var pos = getMousePos( canvas, evt );
isSpinning = !isSpinning;
if( animationRequestID && isSpinning ) animate();
}
function onMouseUp( evt ) {
redraw();
}
function repositionCamera() {
var look_at = p3(0,0,0);
var d = 40;
var vd = d * view_height;
var hd = Math.sqrt( d*d - vd*vd );
camera.p = p3( hd*Math.cos(theta), hd*Math.sin(theta), vd );
camera.z = normalize( sub( look_at, camera.p ) );
var up = p3(0,0,1);
camera.x = normalize( cross( camera.z, up ) );
camera.y = normalize( cross( camera.x, camera.z ) );
}
function pointInRect( p, rect ) {
return p.x > rect.x && p.x < ( rect.x + rect.width ) &&
p.y > rect.y && p.y < ( rect.y + rect.height );
}
function add_vert( p ) {
verts.push( p );
}
function make_edge(i,j) {
edges.push( [i,j] );
}
function make_face3(i,j,k) {
faces.push( [i,j,k] );
make_edge(i,j);
make_edge(j,k);
make_edge(k,i);
}
function make_face4(i,j,k,m) {
faces.push( [i,j,k,m] );
make_edge(i,j);
make_edge(j,k);
make_edge(k,m);
make_edge(m,i);
}
function make_zome() {
clear();
var H = height * R; // height of first ring
var P = bulge * R; // shift outwards of first ring
var Z = -10; // vertical position of base
// add the base ring of vertices
for(var i=0;i<N;i++) {
var a = i*2*Math.PI / N;
add_vert( p3( R * Math.sin(a), R * Math.cos(a), Z ) );
}
// add the next ring of vertices
for(var i=0;i<N;i++) {
var b = (i+0.5)*2*Math.PI / N;
add_vert( p3( (R+P) * Math.sin(b), (R+P) * Math.cos(b), Z+H ) );
make_face3( i, (i+1)%N, N+i );
}
angles.push( angle( verts[0], verts[N], verts[1] ) );
// add the remaining rings
for(var r=2;r<M;r++) {
for(var i=0;i<N;i++) {
var ia = (r-1)*N+i;
var ib = (r-2)*N+(i+1)%N;
var ic = (r-1)*N+(i+1)%N;
var id = r*N+i;
var a = verts[ ia ];
var b = verts[ ib ];
var c = verts[ ic ];
var d = add(b,mul(sub(mid(a,c),b),1+K)); // make a kite (rhombus if K=1)
add_vert( d );
make_face4( ia, ib, ic, id );
if( i == 0 ) {
angles.push( angle(a,b,c) );
}
}
}
}
function init() {
canvas = document.getElementById('canvas');
ctx = canvas.getContext('2d');
theta = Math.PI / 2;
view_height = 0.5;
camera = { p:p3(0,3,-6),
x:p3(1,0,0),
y:p3(0,1,0),
z:p3(0,0,1),
f:canvas.height,
pp:p2(canvas.width/2-100,canvas.height/2)
};
repositionCamera();
var width_slider = document.getElementById("width_slider");
width_slider.updatefromvalue = function() {
R = parseFloat(this.value);
document.getElementById("width_label").innerHTML = "Width: "+R;
}
width_slider.oninput = function() {
this.updatefromvalue();
make_zome();
}
width_slider.updatefromvalue();
var height_slider = document.getElementById("height_slider");
height_slider.updatefromvalue = function() {
height = parseFloat(this.value);
document.getElementById("height_label").innerHTML = "Height: "+height;
}
height_slider.oninput = function() {
this.updatefromvalue();
make_zome();
}
height_slider.updatefromvalue();
var bulge_slider = document.getElementById("bulge_slider");
bulge_slider.updatefromvalue = function() {
bulge = parseFloat(this.value);
document.getElementById("bulge_label").innerHTML = "Bulge: "+bulge;
}
bulge_slider.oninput = function() {
this.updatefromvalue();
make_zome();
}
bulge_slider.updatefromvalue();
var rings_slider = document.getElementById("rings_slider");
rings_slider.updatefromvalue = function() {
M = parseFloat(this.value);
document.getElementById("rings_label").innerHTML = "Number of rings: "+M;
}
rings_slider.oninput = function() {
this.updatefromvalue();
make_zome();
}
rings_slider.updatefromvalue();
var triangles_slider = document.getElementById("triangles_slider");
triangles_slider.updatefromvalue = function() {
N = parseFloat(this.value);
document.getElementById("triangles_label").innerHTML = "Number of triangles: "+N;
}
triangles_slider.oninput = function() {
this.updatefromvalue();
make_zome();
}
triangles_slider.updatefromvalue();
var kite_slider = document.getElementById("kite_slider");
kite_slider.updatefromvalue = function() {
K = parseFloat(this.value);
document.getElementById("kite_label").innerHTML = "Kite ratio: "+K;
}
kite_slider.oninput = function() {
this.updatefromvalue();
make_zome();
}
kite_slider.updatefromvalue();
make_zome();
redraw();
canvas.addEventListener( 'mousemove', onMouseMove, false );
canvas.addEventListener( 'touchmove', onMouseMove, false );
canvas.addEventListener( 'touchstart', onMouseDown, false );
canvas.addEventListener( 'mousedown', onMouseDown, false );
canvas.addEventListener( 'touchend', onMouseUp, false );
canvas.addEventListener( 'mouseup', onMouseUp, false );
canvas.addEventListener( 'mouseout', onMouseUp, false );
isSpinning = true;
animate();
}
function camera_projection( p, camera ) {
var ray = sub( p, camera.p ); // the ray from camera center to point
var cp = p3( dot( camera.x, ray ), dot( camera.y, ray ), dot( camera.z, ray ) ); // into camera space
return p3( cp.x * camera.f / cp.z + camera.pp.x,
canvas.height - ( cp.y * camera.f / cp.z + camera.pp.y ),
cp.z );
}
function redraw() {
drawMesh();
drawNet();
}
function point( p ) {
ctx.beginPath();
ctx.arc( p.x, p.y, 2, 0, 2.0 * Math.PI );
ctx.fill();
}
function drawMesh() {
// project the mesh onto the screen
for( var iVert = 0; iVert < verts.length; ++iVert ) {
verts2d[ iVert ] = camera_projection( verts[ iVert ], camera );
}
ctx.strokeStyle = "rgb(200,200,200)";
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.strokeRect(0, 0, canvas.width, canvas.height);
// draw the faces
ctx.fillStyle = "rgba(200,210,255,0.2)";
for( var iFace = 0; iFace < faces.length; ++iFace ) {
var a = verts2d[ faces[iFace][0] ];
ctx.beginPath();
ctx.moveTo( a.x, a.y );
for( var i = 1; i < faces[iFace].length; ++i ) {
a = verts2d[ faces[iFace][i] ];
ctx.lineTo( a.x, a.y );
}
ctx.fill();
}
// draw the edges
ctx.strokeStyle = "rgb(200,200,200)";
ctx.beginPath();
for( var i = 0; i < edges.length; ++i ) {
var a = verts2d[ edges[i][0] ];
var b = verts2d[ edges[i][1] ];
ctx.moveTo( a.x, a.y );
ctx.lineTo( b.x, b.y );
}
ctx.stroke();
// draw the verts
ctx.fillStyle = "rgb(0,0,0)";
for( var i = 0; i < verts2d.length; ++i )
point( verts2d[i], "" );
}
function drawNet() {
ctx.strokeStyle = "rgb(0,0,0)";
ctx.beginPath();
// first triangle:
var a = p2(500,580);
var b = p2(a.x+R*10,580);
var angle = angles[0];
var h = 0.5*dist(a,b) / Math.tan(angles[0]/2);
var c = add(mid(a,b),p2(0,-h));
ctx.moveTo( a.x, a.y );
ctx.lineTo( b.x, b.y );
ctx.lineTo( c.x, c.y );
ctx.lineTo( a.x, a.y );
// remaining rhombi
for(var i=1;i<angles.length;i++) {
var angle = angles[i];
var bc = sub(c,b);
var rotated_bc = rot2d
var new_b = add( b, rot2d(bc, angle) );
var new_c = add(b,mul(sub(mid(c,new_b),b),1+K)); // make a kite (rhombus if K=1)
ctx.moveTo( c.x, c.y );
ctx.lineTo( b.x, b.y );
ctx.lineTo( new_b.x, new_b.y );
ctx.lineTo( new_c.x, new_c.y );
ctx.lineTo( c.x, c.y );
b = new_b;
c = new_c;
}
ctx.stroke();
}
function drawRect( x, y, w, h ) {
ctx.beginPath();
ctx.rect( x, y, w, h );
ctx.fill();
ctx.stroke();
}
function animate() {
if( isSpinning ) {
// rotate round
theta += 0.004;
repositionCamera();
}
redraw();
animationRequestID = requestAnimationFrame( animate );
}
window.onload = init;
</script>
<noscript>
<p>For full functionality of this site it is necessary to enable JavaScript.
Here are the <a href="http://www.enable-javascript.com/" target="_blank">
instructions how to enable JavaScript in your web browser</a>.
</p></noscript>
</head>
<body>
<table border="0">
<tr>
<td><canvas id="canvas" width="800" height="600">(Canvas drawing not supported by your browser.)</canvas></td>
<td width="300px" valign="center">
<p id="width_label">Width:</p>
<div class="slidecontainer"><input type="range" min="0.3" max="15" value="10" step="0.001" class="slider" id="width_slider"></div>
<p id="height_label">Height:</p>
<div class="slidecontainer"><input type="range" min="0.0" max="0.7" value="0.3" step="0.0001" class="slider" id="height_slider"></div>
<p id="bulge_label">Bulge:</p>
<div class="slidecontainer"><input type="range" min="-1" max="1" value="0.035" step="0.0001" class="slider" id="bulge_slider"></div>
<p id="rings_label">Number of rings:</p>
<div class="slidecontainer"><input type="range" min="2" max="30" value="8" step="1" class="slider" id="rings_slider"></div>
<p id="triangles_label">Number of triangles:</p>
<div class="slidecontainer"><input type="range" min="3" max="30" value="12" step="1" class="slider" id="triangles_slider"></div>
<p id="kite_label">Kite ratio:</p>
<div class="slidecontainer"><input type="range" min="0.1" max="3" value="1" step="0.001" class="slider" id="kite_slider"></div>
</td>
</tr>
</table>
<p>
Source code: <a href="https://github.com/timhutton/zomes">https://github.com/timhutton/zomes</a>
</p>
</body>
</html>