このファイル: /home/web6047/www/cgi-bin/prj/index - 201806.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
var idxjs = new App();
45
46
47
addEventListener( "load", function() {
48
var parentEL = document.getElementById( "whiteareaID" );
49
idxjs.onloadx1( parentEL );
50
}, false );
51
52
function App() {
53
}
54
55
App.prototype.onloadx1 = function( parentEL ) {
56
57
58
59
60
if( ! parentEL ) parentEL = document.body;
61
62
63
this.parentEL = parentEL;
64
with( this.parentEL.style ) {
65
position = "relative";
66
zIndex = 0;
67
}
68
69
70
this.swEL = document.createElement( "div" );
71
this.parentEL.appendChild( this.swEL );
72
with( this.swEL.style ) {
73
border = "solid 0px black";
74
position = "absolute";
75
right = "0px";
76
top = "0px";
77
boxSizing = "border-box";
78
zIndex = 2;
79
padding = ".5em 1em";
80
}
81
this.swEL.innerHTML = "<a href=\"javascript:if( timerID ) { clearInterval( timerID ); timerID = 0;} else idxjs.start();void(0);\">STOP</a>";
82
83
84
this.canvasEL = document.createElement( "canvas" );
85
this.parentEL.appendChild( this.canvasEL );
86
this.canvasEL.setAttribute( "id", "idxjs_canvasELID" );
87
with( this.canvasEL.style ) {
88
border = "solid 0px black";
89
position = "absolute";
90
left = "0px";
91
top = "0px";
92
boxSizing = "border-box";
93
zIndex = -1;
94
95
}
96
97
98
99
if( true ) {
100
this.canvasEL.style.imageRendering = "pixelated";
101
this.canvasEL.style.imageRendering = "optimizeSpeed";
102
}
103
104
this.canvas = this.canvasEL.getContext( "2d" );
105
this.onresizex();
106
107
108
this_closure = this;
109
addEventListener( "resize", function( e ) {
110
this_closure.onresizex( e );
111
}, false );
112
113
this.onloadx2();
114
115
}
116
117
App.prototype.onresizex = function( e ) {
118
119
var waRect = this.parentEL.getBoundingClientRect();
120
this.canvasW = waRect.width;
121
this.canvasH = 480;
122
this.canvasEL.setAttribute( "width", this.canvasW );
123
this.canvasEL.setAttribute( "height", this.canvasH );
124
125
with( this.canvasEL.style ) {
126
width = this.canvasW;
127
height = this.canvasH;
128
}
129
130
131
if( true ) {
132
var pixelsize = 2;
133
this.canvasEL.style.width = this.canvasEL.width + "px";
134
this.canvasEL.style.height = this.canvasEL.height + "px";
135
this.canvasEL.width /= pixelsize;
136
this.canvasEL.height /= pixelsize;
137
this.canvas.scale( 1 / pixelsize, 1 / pixelsize );
138
}
139
}
140
141
142
143
144
145
146
App.prototype.onloadx2 = function() {
147
148
149
150
151
152
153
154
155
156
157
this.squareMaster = {
158
tens : [
159
[ -1, -1, 0 ],
160
[ 1, -1, 0 ],
161
[ 1, 1, 0 ],
162
[ -1, 1, 0 ]
163
],
164
};
165
166
167
this.square0 = {
168
ref : this.squareMaster,
169
scale : 100,
170
pos : xyz( 600, 0, 1000 ),
171
rotation : xyz( 0, 0, 0 ),
172
type : 0,
173
}
174
175
176
this.squares = [
177
this.square0,
178
{
179
ref : this.squareMaster,
180
scale : 75,
181
pos : xyz( 500, 0, 1000 ),
182
rotation : xyz( 0, 0, 0 ),
183
type : 0,
184
}
185
186
]
187
188
189
this.animates = new Array();
190
this.animates.push( {
191
pos : xyz( 0, 0, 1000 ),
192
rotation : xyz( 0, 0, 0 ),
193
rotationAdd : xyz( 0, 0.05, 0 ),
194
} );
195
this.animates.push( {
196
pos : xyz( 0, 0, 0 ),
197
rotation : xyz( 0, 0, 0 ),
198
rotationAdd : xyz( 0, 0, -0.05 ),
199
} );
200
this.animates.push( {
201
pos : xyz( -100, 0, 1000 ),
202
rotation : xyz( 0, 0, 3.14/1.75 ),
203
rotationAdd : xyz( 0, 0.05, 0 ),
204
} );
205
206
207
this.camera = {
208
pos : xyz( 0, 150, 00 ),
209
s : 40,
210
zoom : 17.25,
211
rotation : xyz( -0.3, 0, -0.2 ),
212
};
213
214
mouseX = 0;
215
mouseY = 0;
216
217
this.draw();
218
this.start();
219
onmousemove = function( e ) {
220
221
mouseX = e.clientX;
222
mouseY = e.clientY;
223
};
224
}
225
226
App.prototype.start = function() {
227
228
var this_closure = this;
229
timerID = setInterval( function() {
230
this_closure.run();
231
}, 80 );
232
}
233
234
App.prototype.run = function() {
235
236
237
for( var i = 0; i < this.animates.length; i++ ) {
238
with( this.animates[ i ] ) {
239
rotation.x += rotationAdd.x;
240
rotation.y += rotationAdd.y;
241
rotation.z += rotationAdd.z;
242
243
244
245
246
247
248
}
249
}
250
251
252
var square = objcopy( this.square0 );
253
square.type = 1;
254
square.cntmax = 15;
255
square.cnt = 1;
256
var rate = 70;
257
square.pos.x += Math.random() * rate;
258
square.pos.y += Math.random() * rate;
259
square.pos.z += Math.random() * rate * 0.5;
260
var rate = 1;
261
square.rotation.x += Math.random() * rate;
262
square.rotation.y += Math.random() * rate;
263
square.rotation.z += Math.random() * rate;
264
this.squares.push( square );
265
266
267
for( var i = this.squares.length - 1; i >= 0; i-- ) {
268
var square = this.squares[ i ];
269
270
if( square.type == 0 ) continue;
271
272
square.cnt++;
273
274
if( square.cnt >= square.cntmax ) {
275
this.squares.splice( i, 1 );
276
}
277
}
278
279
this.draw();
280
}
281
282
App.prototype.draw = function() {
283
284
285
286
287
288
289
290
291
292
for( var j = 0; j < this.squares.length; j++ ) {
293
var square = this.squares[ j ];
294
var is0 = square == this.square0;
295
square.jusin = xyz( 0, 0, 0);
296
square.tensC = new Array();
297
298
for( var i = 0; i < square.ref.tens.length; i++ ) {
299
var refTen = square.ref.tens[ i ];
300
301
302
var x = refTen[ 0 ];
303
var y = refTen[ 1 ];
304
var z = refTen[ 2 ];
305
306
307
308
309
var a = square.type == 0 ? 1 : ( 1 / ( square.cnt * 0.75 ) );
310
x *= square.scale * a;
311
y *= square.scale * a;
312
z *= square.scale * a;
313
314
315
316
var res = rotate1( x, z, square.rotation.y );
317
x = res.X;
318
z = res.Y;
319
var res = rotate1( z, y, square.rotation.x );
320
z = res.X;
321
y = res.Y;
322
var res = rotate1( x, y, square.rotation.z );
323
x = res.X;
324
y = res.Y;
325
326
327
var animate = this.animates[ 1 ];
328
329
var res = rotate2( animate.pos.z, animate.pos.y, z, y, animate.rotation.x );
330
z = res.X;
331
y = res.Y;
332
333
var res = rotate2( animate.pos.x, animate.pos.z, x, z, animate.rotation.y );
334
x = res.X;
335
z = res.Y;
336
337
var res = rotate2( animate.pos.x, animate.pos.y, x, y, animate.rotation.z );
338
x = res.X;
339
y = res.Y;
340
341
342
343
x += square.pos.x;
344
y += square.pos.y;
345
z += square.pos.z;
346
347
348
var animate;
349
var a;
350
if( square.type == 0 && square != this.square0 ) {
351
animate = this.animates[ 2 ];
352
a = 0;
353
} else {
354
animate = this.animates[ 0 ];
355
var b = .15;
356
a = ( is0 ? 0 : ( b + square.cnt * 0.075 ) );
357
}
358
359
var rotation = animate.rotation.x - ( animate.rotation.x != 0 ? a : 0 );
360
var res = rotate2( animate.pos.z, animate.pos.y, z, y, rotation );
361
z = res.X;
362
y = res.Y;
363
364
var rotation = animate.rotation.y - ( animate.rotation.y != 0 ? a : 0 );
365
var res = rotate2( animate.pos.x, animate.pos.z, x, z, rotation );
366
x = res.X;
367
z = res.Y;
368
369
var rotation = animate.rotation.z - ( animate.rotation.z != 0 ? a : 0 );
370
var res = rotate2( animate.pos.x, animate.pos.y, x, y, rotation );
371
x = res.X;
372
y = res.Y;
373
374
375
376
377
x -= this.camera.pos.x;
378
y -= this.camera.pos.y;
379
z -= this.camera.pos.z;
380
381
382
var res = rotate1( x, z, -this.camera.rotation.y );
383
x = res.X;
384
z = res.Y;
385
var res = rotate1( z, y, -this.camera.rotation.x );
386
z = res.X;
387
y = res.Y;
388
var res = rotate1( x, y, -this.camera.rotation.z );
389
x = res.X;
390
y = res.Y;
391
392
393
394
square.jusin.x += x;
395
square.jusin.y += y;
396
square.jusin.z += z;
397
398
399
400
var h = x * ( this.camera.s / z );
401
var v = -y * ( this.camera.s / z );
402
403
h *= this.camera.zoom;
404
v *= this.camera.zoom;
405
406
407
square.tensC[ i ] = {
408
x : x,
409
y : y,
410
z : z,
411
h : h,
412
v : v,
413
};
414
}
415
416
417
square.jusin.x /= 4;
418
square.jusin.y /= 4;
419
square.jusin.z /= 4;
420
421
422
if( square.jusin.z > this.animates[ 0 ].pos.z || isMouseOver( this.canvasEL ) ) {
423
this.canvasEL.style.zIndex = -1;
424
} else {
425
this.canvasEL.style.zIndex = 1;
426
}
427
}
428
429
430
this.squares.sort( function( a, b ) {
431
return ( a.jusin.z < b.jusin.z ) - ( a.jusin.z > b.jusin.z );
432
} );
433
434
435
with( this.canvas ) {
436
clearRect( 0, 0, this.canvasW, this.canvasH );
437
438
save();
439
translate( this.canvasEL.width, this.canvasEL.height );
440
441
442
443
444
for( var j = 0; j < this.squares.length; j++ ) {
445
var square = this.squares[ j ];
446
447
beginPath();
448
for( var i = 0; i < square.tensC.length; i++ ) {
449
var tenC = square.tensC[ i ];
450
if( i == 0 ) {
451
moveTo( tenC.h, tenC.v );
452
} else {
453
lineTo( tenC.h, tenC.v );
454
}
455
}
456
closePath();
457
458
if( square.type == 0 ) {
459
460
fillStyle = square == this.square0 ? rgb( 0,255,192 ) : rgb( 255,128,192 );
461
} else {
462
463
fillStyle = rgb( 0, 255, 192 * ( 1 / ( square.cnt * .5 ) ) );
464
}
465
fill();
466
}
467
restore();
468
}
469
}
470
function isMouseOver( element ) {
471
var r = element.getBoundingClientRect();
472
var res = mouseX >= r.left && mouseX <= r.left + r.width && mouseY >= r.top && mouseY <= r.top + r.height;
473
return res;
474
}
475
function xyz( x, y, z ) {
476
return { x : x, y : y, z : z };
477
}
478
function rgb( r, g, b ) {
479
return "rgb(" + r + "," + g + "," + b + ")"
480
}
481
function rotate1( x, y, theta2 ) {
482
483
var theta1 = Math.atan2( y, x );
484
var hankei = Math.sqrt( x * x + y * y );
485
var rx = Math.cos( theta1 + theta2 ) * hankei;
486
var ry = Math.sin( theta1 + theta2 ) * hankei;
487
return { X : rx, Y : ry };
488
}
489
function rotate2( cx, cy, x, y, theta2 ) {
490
491
x -= cx;
492
y -= cy;
493
var res = rotate1( x, y, theta2 );
494
res.X += cx;
495
res.Y += cy;
496
return res;
497
}
498
function objcopy( obj ) {
499
var res;
500
501
if( obj instanceof Array ) {
502
503
res = new Array();
504
for( var i = 0; i < obj.length; i++ ) {
505
if( typeof obj[ i ] == "object" ) {
506
res[ i ] = objcopy( obj[ i ] );
507
} else {
508
res[ i ] = obj[ i ];
509
}
510
}
511
512
} else {
513
514
res = new Object();
515
for( var name in obj ) {
516
if( typeof obj[ name ] == "object" ) {
517
res[ name ] = objcopy( obj[ name ] );
518
} else {
519
res[ name ] = obj[ name ];
520
}
521
}
522
}
523
return res;
524
}
525
526
function printObj( obj ) {
527
for( var name in obj ) {
528
if( typeof obj[ name ] == "object" ) {
529
console.log( name );
530
printObj( obj[ name ] );
531
} else {
532
console.log( name, obj[ name ] );
533
}
534
}
535
}
536
function mark( x, y, canvas ) {
537
canvas.save();
538
canvas.fillRect( x, y, 4, 4 );
539
canvas.restore();
540
}