質問編集履歴

5

追記

2017/12/08 08:35

投稿

narururu
narururu

スコア172

test CHANGED
File without changes
test CHANGED
@@ -70,9 +70,41 @@
70
70
 
71
71
  ```
72
72
 
73
+ <!DOCTYPE html>
74
+
75
+ <html lang="ja">
76
+
77
+ <head>
78
+
79
+ <meta charset="utf-8">
80
+
81
+ <title>Interactive Art</title>
82
+
83
+ <style>
84
+
85
+ canvas {
86
+
87
+ background: black;
88
+
89
+ }
90
+
91
+ </style>
92
+
93
+ </head>
94
+
95
+ <body>
96
+
97
+ <canvas id="mycanvas" width="500" height="250">
98
+
99
+ Canvasに対応したブラウザを使ってください。
100
+
101
+ </canvas>
73
102
 
74
103
 
104
+
105
+ <script>
106
+
75
- var img = new Image();
107
+ var img = new Image();
76
108
 
77
109
 
78
110
 
@@ -96,6 +128,12 @@
96
128
 
97
129
  img.src = "logo.PNG";
98
130
 
131
+ </script>
132
+
133
+ </html>
134
+
135
+
136
+
99
137
  ```
100
138
 
101
139
  上記のコードを追記した結果、リロードするタイミングで一瞬だけ画像が表示されますが、すぐに消えてしまう現象に陥っております。

4

追記

2017/12/08 08:35

投稿

narururu
narururu

スコア172

test CHANGED
File without changes
test CHANGED
@@ -70,304 +70,32 @@
70
70
 
71
71
  ```
72
72
 
73
- <script>
74
73
 
75
- (function() {
76
74
 
77
- "use strict";
75
+ var img = new Image();
78
76
 
79
77
 
80
78
 
81
- var canvas;
82
-
83
- var ctx;
84
-
85
- var Ball;
86
-
87
- var balls = [];
79
+ img.onload = (function(){
88
-
89
- var Stage;
90
-
91
- var stage;
92
80
 
93
81
 
94
82
 
95
- var img = new Image();
83
+ var canvas = document.getElementById('logo');
96
-
97
- var Draw;
98
-
99
- var draw;
100
-
101
- /////////////////////////////////////////////////////////////////////////
102
-
103
- var App;
104
-
105
- var app;
106
-
107
- /////////////////////////////////////////////////////////////////////////
108
84
 
109
85
 
110
86
 
111
-
112
-
113
- canvas = document.getElementById("mycanvas");
114
-
115
- if (!canvas || !canvas.getContext) return false;
116
-
117
- ctx = canvas.getContext("2d");
87
+ // var ctx = canvas.getContext('2d');
118
88
 
119
89
 
120
90
 
121
- function rand(min, max) {
91
+ ctx.drawImage(img, 0, 0);
122
92
 
123
- return min + Math.floor(Math.random() * (max - min + 1));
124
-
125
- }
93
+ });
126
94
 
127
95
 
128
96
 
129
- function adjustPosition(pos, r, max) {
130
-
131
- // if (x - r < 0) x = r;
132
-
133
- // if (y - r < 0) y = r;
134
-
135
- // if (x + r > canvas.width) x = canvas.width - r;
136
-
137
- // if (y + r > canvas.height) y = canvas.height - r;
138
-
139
- if (pos - r < 0) {
140
-
141
- return r;
142
-
143
- } else if (pos + r > max) {
144
-
145
- return max - r;
146
-
147
- } else {
148
-
149
- return pos;
150
-
151
- }
152
-
153
- }
154
-
155
-
156
-
157
- //
158
-
159
- (function() {
160
-
161
- var canvas = document.getElementById('mycanvas');
162
-
163
- var container = document.getElementById('wrap');
164
-
165
- sizing();
166
-
167
-
168
-
169
- function sizing() {
170
-
171
- canvas.height = container.offsetHeight;
172
-
173
- canvas.width = container.offsetWidth;
174
-
175
- }
176
-
177
-
178
-
179
- window.addEventListener('resize', function() {
180
-
181
- (!window.requestAnimationFrame) ? setTimeout(sizing, 300): window.requestAnimationFrame(sizing);
182
-
183
- });
184
-
185
- })();
186
-
187
- //
188
-
189
-
190
-
191
-
192
-
193
- canvas.addEventListener("click", function(e) {
194
-
195
- var x, y, r;
196
-
197
- var rect;
198
-
199
- // x = rand(100, 400);
200
-
201
- // y = rand(100, 200);
202
-
203
- rect = e.target.getBoundingClientRect();
204
-
205
- x = e.clientX - rect.left;
206
-
207
- y = e.clientY - rect.top;
208
-
209
- r = rand(0, 100) < 20 ? rand(50, 80) : rand(10, 35);
210
-
211
-
212
-
213
-
214
-
215
- x = adjustPosition(x, r, canvas.width);
216
-
217
- y = adjustPosition(y, r, canvas.height);
218
-
219
- balls.push(new Ball(x, y, r));
220
-
221
- });
222
-
223
-
224
-
225
- Ball = function(x, y, r) {
226
-
227
- this.x = x;
228
-
229
- this.y = y;
230
-
231
- this.r = r;
232
-
233
- this.vx = rand(-10, 10);
234
-
235
- this.vy = rand(-10, 10);
236
-
237
- this.color = "hsla(" + rand(150, 250) + ", " + rand(60, 100) + "%," + rand(40, 60) + "%," + Math.random() + ")";
238
-
239
- };
240
-
241
-
242
-
243
- Ball.prototype.draw = function() {
244
-
245
- ctx.beginPath();
246
-
247
- ctx.arc(this.x, this.y, this.r, 0, Math.PI*2);
248
-
249
- ctx.fillStyle = this.color;
250
-
251
- ctx.closePath();
252
-
253
- ctx.fill();
254
-
255
- };
256
-
257
-
258
-
259
- Ball.prototype.move = function() {
260
-
261
- if (this.x + this.r > canvas.width || this.x - this.r < 0) {
262
-
263
- this.vx *= -1;
264
-
265
- }
266
-
267
- if (this.y + this.r > canvas.height || this.y - this.r < 0) {
268
-
269
- this.vy *= -1;
270
-
271
- }
272
-
273
- this.x += this.vx;
274
-
275
- this.y += this.vy;
276
-
277
- };
278
-
279
-
280
-
281
- // var ball = new Ball(rand(100, 200), rand(100, 200), rand(10, 50));
282
-
283
- // ball.draw();
284
-
285
-
286
-
287
- Stage = function() {
288
-
289
- this.update = function() {
290
-
291
- var i;
292
-
293
- this.clear();
294
-
295
- for (i = 0; i < balls.length; i++) {
296
-
297
- balls[i].draw();
298
-
299
- balls[i].move();
300
-
301
- }
302
-
303
- setTimeout(function() {
304
-
305
- this.update();
306
-
307
- }.bind(this), 30);
308
-
309
- };
310
-
311
- this.clear = function() {
312
-
313
- ctx.fillStyle = "black";
314
-
315
- ctx.fillRect(0, 0, canvas.width, canvas.height);
316
-
317
- }
318
-
319
- };
320
-
321
-
322
-
323
- img.src = "img/logo.PNG";
97
+ img.src = "logo.PNG";
324
-
325
- img.addEventListener("load", function() {
326
-
327
- stage = new Stage();
328
-
329
- stage.update();
330
-
331
- draw = new Draw();
332
-
333
- });
334
-
335
- /////////////////////////////////////////////////////////////////////////
336
-
337
- App = function() {
338
-
339
- this.draw = function() {
340
-
341
- ctx.fillRect(0, 0, width, height);
342
-
343
-
344
-
345
- var img_logo = new Image();
346
-
347
- img_logo.src = 'img/logo.png';
348
-
349
- ctx.drawImage(img_logo, 0, 0);
350
-
351
-
352
-
353
- app.draw();
354
-
355
- };
356
-
357
- /////////////////////////////////////////////////////////////////////////
358
-
359
- })();
360
-
361
- </script>
362
-
363
- </body>
364
-
365
- </html>
366
98
 
367
99
  ```
368
100
 
369
- 「///」で囲ってある箇所は画像挿入するために自分でコーディングした箇所です。
101
+ 上記のコード追記した結果、リロードするタイミングで一瞬だけ画像が表示されまが、すぐに消えてしまう現象に陥っております
370
-
371
- 他のコードはできているものをそのまま写した箇所です。
372
-
373
- 現在は画面が真っ白な状態となっております。

3

追記

2017/12/08 07:35

投稿

narururu
narururu

スコア172

test CHANGED
File without changes
test CHANGED
@@ -369,3 +369,5 @@
369
369
  「///」で囲ってある箇所は画像を挿入するために自分でコーディングした箇所です。
370
370
 
371
371
  他のコードはできているものをそのまま写した箇所です。
372
+
373
+ 現在は画面が真っ白な状態となっております。

2

修正

2017/12/08 04:58

投稿

narururu
narururu

スコア172

test CHANGED
File without changes
test CHANGED
@@ -70,32 +70,6 @@
70
70
 
71
71
  ```
72
72
 
73
- <!DOCTYPE html>
74
-
75
- <html lang="ja">
76
-
77
- <head>
78
-
79
- <meta charset="utf-8">
80
-
81
- <title>インタラクティブアート</title>
82
-
83
- <link rel="stylesheet" href="styles.css">
84
-
85
- </head>
86
-
87
- <body>
88
-
89
- <div id="wrap">
90
-
91
- <canvas id="mycanvas" width="100%" height="400">
92
-
93
- Canvasに対応したブラウザを使ってください。
94
-
95
- </canvas>
96
-
97
- </div>
98
-
99
73
  <script>
100
74
 
101
75
  (function() {

1

追記

2017/12/08 04:42

投稿

narururu
narururu

スコア172

test CHANGED
File without changes
test CHANGED
@@ -63,3 +63,335 @@
63
63
 
64
64
 
65
65
  以上、ご確認お願いいたします。
66
+
67
+
68
+
69
+ ###追記
70
+
71
+ ```
72
+
73
+ <!DOCTYPE html>
74
+
75
+ <html lang="ja">
76
+
77
+ <head>
78
+
79
+ <meta charset="utf-8">
80
+
81
+ <title>インタラクティブアート</title>
82
+
83
+ <link rel="stylesheet" href="styles.css">
84
+
85
+ </head>
86
+
87
+ <body>
88
+
89
+ <div id="wrap">
90
+
91
+ <canvas id="mycanvas" width="100%" height="400">
92
+
93
+ Canvasに対応したブラウザを使ってください。
94
+
95
+ </canvas>
96
+
97
+ </div>
98
+
99
+ <script>
100
+
101
+ (function() {
102
+
103
+ "use strict";
104
+
105
+
106
+
107
+ var canvas;
108
+
109
+ var ctx;
110
+
111
+ var Ball;
112
+
113
+ var balls = [];
114
+
115
+ var Stage;
116
+
117
+ var stage;
118
+
119
+
120
+
121
+ var img = new Image();
122
+
123
+ var Draw;
124
+
125
+ var draw;
126
+
127
+ /////////////////////////////////////////////////////////////////////////
128
+
129
+ var App;
130
+
131
+ var app;
132
+
133
+ /////////////////////////////////////////////////////////////////////////
134
+
135
+
136
+
137
+
138
+
139
+ canvas = document.getElementById("mycanvas");
140
+
141
+ if (!canvas || !canvas.getContext) return false;
142
+
143
+ ctx = canvas.getContext("2d");
144
+
145
+
146
+
147
+ function rand(min, max) {
148
+
149
+ return min + Math.floor(Math.random() * (max - min + 1));
150
+
151
+ }
152
+
153
+
154
+
155
+ function adjustPosition(pos, r, max) {
156
+
157
+ // if (x - r < 0) x = r;
158
+
159
+ // if (y - r < 0) y = r;
160
+
161
+ // if (x + r > canvas.width) x = canvas.width - r;
162
+
163
+ // if (y + r > canvas.height) y = canvas.height - r;
164
+
165
+ if (pos - r < 0) {
166
+
167
+ return r;
168
+
169
+ } else if (pos + r > max) {
170
+
171
+ return max - r;
172
+
173
+ } else {
174
+
175
+ return pos;
176
+
177
+ }
178
+
179
+ }
180
+
181
+
182
+
183
+ //
184
+
185
+ (function() {
186
+
187
+ var canvas = document.getElementById('mycanvas');
188
+
189
+ var container = document.getElementById('wrap');
190
+
191
+ sizing();
192
+
193
+
194
+
195
+ function sizing() {
196
+
197
+ canvas.height = container.offsetHeight;
198
+
199
+ canvas.width = container.offsetWidth;
200
+
201
+ }
202
+
203
+
204
+
205
+ window.addEventListener('resize', function() {
206
+
207
+ (!window.requestAnimationFrame) ? setTimeout(sizing, 300): window.requestAnimationFrame(sizing);
208
+
209
+ });
210
+
211
+ })();
212
+
213
+ //
214
+
215
+
216
+
217
+
218
+
219
+ canvas.addEventListener("click", function(e) {
220
+
221
+ var x, y, r;
222
+
223
+ var rect;
224
+
225
+ // x = rand(100, 400);
226
+
227
+ // y = rand(100, 200);
228
+
229
+ rect = e.target.getBoundingClientRect();
230
+
231
+ x = e.clientX - rect.left;
232
+
233
+ y = e.clientY - rect.top;
234
+
235
+ r = rand(0, 100) < 20 ? rand(50, 80) : rand(10, 35);
236
+
237
+
238
+
239
+
240
+
241
+ x = adjustPosition(x, r, canvas.width);
242
+
243
+ y = adjustPosition(y, r, canvas.height);
244
+
245
+ balls.push(new Ball(x, y, r));
246
+
247
+ });
248
+
249
+
250
+
251
+ Ball = function(x, y, r) {
252
+
253
+ this.x = x;
254
+
255
+ this.y = y;
256
+
257
+ this.r = r;
258
+
259
+ this.vx = rand(-10, 10);
260
+
261
+ this.vy = rand(-10, 10);
262
+
263
+ this.color = "hsla(" + rand(150, 250) + ", " + rand(60, 100) + "%," + rand(40, 60) + "%," + Math.random() + ")";
264
+
265
+ };
266
+
267
+
268
+
269
+ Ball.prototype.draw = function() {
270
+
271
+ ctx.beginPath();
272
+
273
+ ctx.arc(this.x, this.y, this.r, 0, Math.PI*2);
274
+
275
+ ctx.fillStyle = this.color;
276
+
277
+ ctx.closePath();
278
+
279
+ ctx.fill();
280
+
281
+ };
282
+
283
+
284
+
285
+ Ball.prototype.move = function() {
286
+
287
+ if (this.x + this.r > canvas.width || this.x - this.r < 0) {
288
+
289
+ this.vx *= -1;
290
+
291
+ }
292
+
293
+ if (this.y + this.r > canvas.height || this.y - this.r < 0) {
294
+
295
+ this.vy *= -1;
296
+
297
+ }
298
+
299
+ this.x += this.vx;
300
+
301
+ this.y += this.vy;
302
+
303
+ };
304
+
305
+
306
+
307
+ // var ball = new Ball(rand(100, 200), rand(100, 200), rand(10, 50));
308
+
309
+ // ball.draw();
310
+
311
+
312
+
313
+ Stage = function() {
314
+
315
+ this.update = function() {
316
+
317
+ var i;
318
+
319
+ this.clear();
320
+
321
+ for (i = 0; i < balls.length; i++) {
322
+
323
+ balls[i].draw();
324
+
325
+ balls[i].move();
326
+
327
+ }
328
+
329
+ setTimeout(function() {
330
+
331
+ this.update();
332
+
333
+ }.bind(this), 30);
334
+
335
+ };
336
+
337
+ this.clear = function() {
338
+
339
+ ctx.fillStyle = "black";
340
+
341
+ ctx.fillRect(0, 0, canvas.width, canvas.height);
342
+
343
+ }
344
+
345
+ };
346
+
347
+
348
+
349
+ img.src = "img/logo.PNG";
350
+
351
+ img.addEventListener("load", function() {
352
+
353
+ stage = new Stage();
354
+
355
+ stage.update();
356
+
357
+ draw = new Draw();
358
+
359
+ });
360
+
361
+ /////////////////////////////////////////////////////////////////////////
362
+
363
+ App = function() {
364
+
365
+ this.draw = function() {
366
+
367
+ ctx.fillRect(0, 0, width, height);
368
+
369
+
370
+
371
+ var img_logo = new Image();
372
+
373
+ img_logo.src = 'img/logo.png';
374
+
375
+ ctx.drawImage(img_logo, 0, 0);
376
+
377
+
378
+
379
+ app.draw();
380
+
381
+ };
382
+
383
+ /////////////////////////////////////////////////////////////////////////
384
+
385
+ })();
386
+
387
+ </script>
388
+
389
+ </body>
390
+
391
+ </html>
392
+
393
+ ```
394
+
395
+ 「///」で囲ってある箇所は画像を挿入するために自分でコーディングした箇所です。
396
+
397
+ 他のコードはできているものをそのまま写した箇所です。