このファイル: /home/web6047/www/cgi-bin/prj/20180901-indexJS/a.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
var indexjs = new App();
63
App.prototype.onloadx = function( e ) {
64
this.onloadxCount++;
65
console.log( this.onloadxCount + " / " + this.onloadxCountMax );
66
67
if( this.onloadxCount == this.onloadxCountMax ) {
68
var parentEL = document.getElementById( "whiteareaID" );
69
this.onloadx1( parentEL );
70
}
71
};
72
73
74
75
76
77
indexjs.images = {
78
"red" : "20180901-indexJS/imgs/red.png",
79
"green" : "20180901-indexJS/imgs/green.png",
80
"blue" : "20180901-indexJS/imgs/blue.png",
81
"mountainLeft" : "20180901-indexJS/imgs/mountainLeft.png",
82
"title" : "20180901-indexJS/imgs/title.png",
83
}
84
indexjs.onloadxCount = 0;
85
indexjs.onloadxCountMax = Object.keys( indexjs.images ).length + 1;
86
for( var name in indexjs.images ) {
87
var src = indexjs.images[ name ];
88
indexjs.images[ name ] = new Image();
89
indexjs.images[ name ].onload = indexjs.onloadx.bind( indexjs );
90
indexjs.images[ name ].src = src;
91
}
92
addEventListener( "load", indexjs.onloadx.bind( indexjs ) );
93
94
95
function App() {
96
this.name = "test";
97
}
98
99
App.prototype.onloadx1 = function( parentEL ) {
100
101
102
103
console.log( "onloadx1()" );
104
105
this.speedstep = [ 10, 75, 200, 500 ];
106
this.timerMS = this.speedstep[ 1 ];
107
108
109
110
111
if( ! parentEL ) parentEL = document.getElementsByTagName( "body" )[ 0 ];
112
113
114
this.parentEL = parentEL;
115
with( this.parentEL.style ) {
116
position = "relative";
117
zIndex = 0;
118
}
119
120
121
this.swEL = document.createElement( "div" );
122
this.parentEL.appendChild( this.swEL );
123
with( this.swEL.style ) {
124
border = "solid 0px black";
125
position = "absolute";
126
right = "0px";
127
top = "0px";
128
boxSizing = "border-box";
129
zIndex = 2;
130
padding = ".5em 1em";
131
}
132
this.swEL.innerHTML = "";
133
this.swEL.innerHTML += "<a style='color:black;' href=\"javascript:if( indexjs.timerID ) indexjs.stop(); else indexjs.start(); void(0);\">STOP</a><BR>";
134
this.swEL.innerHTML += '<a style=\'color:black;\' href=%%dquote26%%>SPEED</a>';
135
136
137
this.canvasEL = document.createElement( "canvas" );
138
this.parentEL.appendChild( this.canvasEL );
139
this.canvasEL.setAttribute( "id", "indexjs_canvasELID" );
140
with( this.canvasEL.style ) {
141
border = "solid 0px black";
142
position = "absolute";
143
left = "0px";
144
top = "0px";
145
boxSizing = "border-box";
146
zIndex = -1;
147
148
}
149
150
151
152
if( true ) {
153
this.canvasEL.style.imageRendering = "pixelated";
154
this.canvasEL.style.imageRendering = "optimizeSpeed";
155
}
156
this.lowmode = true;
157
this.cc = this.canvasEL.getContext( "2d" );
158
159
this.onresizex();
160
161
162
addEventListener( "resize", ( function( e ) { this.onresizex( e ); } ).bind( this ), false );
163
164
165
addEventListener( "scroll", function( e ) {
166
167
var scrollEL = document.documentElement ? document.documentElement : document.body;
168
var r = indexjs.cc.canvas.getBoundingClientRect();
169
170
if( scrollEL.scrollTop + r.top + r.height / 2 < 0 )
171
indexjs.stop();
172
else
173
if( ! indexjs.timerID ) indexjs.start();
174
175
}, false );
176
177
this.onloadx2();
178
179
}
180
181
App.prototype.onresizex = function( e ) {
182
console.log( "onresizex()" );
183
184
185
186
var wa = this.parentEL;
187
var waRect = wa.getBoundingClientRect();
188
this.canvasW = Math.round( waRect.width );
189
190
191
var htags = document.getElementsByTagName( "h2" );
192
if( htags.length > 0 ) {
193
this.canvasH = Math.round( htags[ 0 ].getBoundingClientRect().top - wa.getBoundingClientRect().top ) - 16;
194
} else {
195
this.canvasH = 480;
196
}
197
198
this.canvasEL.style.width = this.canvasW + "px";
199
this.canvasEL.style.height = this.canvasH + "px";
200
this.canvasEL.setAttribute( "width", this.canvasW );
201
this.canvasEL.setAttribute( "height", this.canvasH );
202
203
204
205
if( this.lowmode ) {
206
var pixelsize = 2;
207
this.canvasEL.style.width = this.canvasEL.width + "px";
208
this.canvasEL.style.height = this.canvasEL.height + "px";
209
this.canvasEL.width /= pixelsize;
210
this.canvasEL.height /= pixelsize;
211
this.cc.scale( 1 / pixelsize, 1 / pixelsize );
212
}
213
214
console.log( this.canvasW, this.canvasH );
215
216
if( e ) this.draw();
217
218
}
219
220
221
222
223
224
225
App.prototype.onloadx2 = function() {
226
227
228
229
console.log( "onloadx2()" );
230
231
this.layers = new Array();
232
233
var layer = new Object();
234
layer.image = this.images.blue;
235
layer.px = 0;
236
layer.py = this.canvasH - layer.image.height;
237
layer.addX = 4;
238
layer.width = layer.image.width + 50;
239
this.layers.push( layer );
240
241
var layer = new Object();
242
layer.image = this.images.green;
243
layer.px = 0;
244
layer.py = this.canvasH - layer.image.height;
245
layer.addX = 8;
246
layer.width = layer.image.width + 100;
247
this.layers.push( layer );
248
249
var layer = new Object();
250
layer.image = this.images.red;
251
layer.px = 0;
252
layer.py = this.canvasH - layer.image.height;
253
layer.addX = 16;
254
layer.width = layer.image.width * 1.7;
255
this.layers.push( layer );
256
257
this.onloadx3();
258
};
259
260
App.prototype.onloadx3 = function() {
261
262
263
264
265
266
console.log( "onloadx3()" );
267
268
269
270
this.draw();
271
this.start();
272
}
273
274
275
276
App.prototype.start = function() {
277
console.log( "start()", this.timerMS );
278
this.timerID = setInterval( this.run.bind( this ), this.timerMS );
279
};
280
App.prototype.stop = function() {
281
if( this.timerID ) {
282
console.log( "stop()" );
283
clearInterval( this.timerID );
284
this.timerID = null;
285
}
286
};
287
App.prototype.reset = function() {
288
this.stop();
289
this.onloadx3();
290
};
291
App.prototype.run = function() {
292
for( var i = 0; i < this.layers.length; i++ ) {
293
var layer = this.layers[ i ];
294
layer.px += layer.addX;
295
296
if( layer.px > 0 ) {
297
layer.px = -layer.width;
298
}
299
}
300
this.draw();
301
};
302
303
App.prototype.draw = function() {
304
var cc = this.cc;
305
cc.fillStyle = "blue";
306
cc.fillRect( 0,0,this.canvasW, this.canvasH );
307
308
for( var i = 0; i < this.layers.length; i++ ) {
309
var layer = this.layers[ i ];
310
var cnt = 0;
311
while( 1 ) {
312
var gx = layer.px + layer.width * cnt;
313
314
if( gx > this.canvasW ) break;
315
316
cc.drawImage( layer.image, gx, layer.py );
317
cnt++;
318
}
319
}
320
321
cc.drawImage( this.images.mountainLeft, 0, this.canvasH - this.images.mountainLeft.height );
322
var x = ( this.canvasW - this.images.title.width ) / 2;
323
var y = ( this.canvasH - this.images.title.height ) / 2;
324
cc.drawImage( this.images.title, x + 65, y );
325
326
327
cc.fillStyle = "black";
328
var step = 4;
329
for( var i = 0; i < Math.round( this.canvasH / step ); i++ ) {
330
cc.fillRect( 0, i * step, this.canvasW, Math.floor( step / 2 ) );
331
}
332
};