質問編集履歴

1

CDNの追加・ソースコードの記載をいたしました。

2021/11/10 03:01

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -22,17 +22,33 @@
22
22
 
23
23
 
24
24
 
25
+ そして、こちらでご回答をいただき、head内に
26
+
27
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r79/three.min.js"></script>
28
+
29
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>
30
+
31
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/1.19.0/TweenMax.min.js"></script>
32
+
33
+ の3行も記載いたしました。
34
+
35
+
36
+
25
37
  しかし、画面は真っ白で何も動かないようです。
26
38
 
27
39
  devツールのconsoleには、
28
40
 
41
+
42
+
29
43
  ```
30
44
 
31
45
  エラーメッセージ
32
46
 
33
- test.js:18 Uncaught ReferenceError: _ is not defined
47
+ test.js:81 Uncaught TypeError: Cannot read properties of null (reading 'innerHTML')
48
+
34
-
49
+ at App.start (test.js:81)
50
+
35
- at new App (test.js:18)
51
+ at new App (test.js:50)
36
52
 
37
53
  at test.js:165
38
54
 
@@ -50,9 +66,483 @@
50
66
 
51
67
  ### 試したこと
52
68
 
53
- htmlに<script>を使う場合は<head>内にもjsの読み込みが必要?
54
-
55
- との記事を見たので記入してみましたが、何も変わりませんでした。
69
+
70
+
71
+
72
+
73
+
74
+
75
+ 以下、ソースコードを記載します。
76
+
77
+ HTML
78
+
79
+ ```
80
+
81
+ <!DOCTYPE html>
82
+
83
+ <html lang="en">
84
+
85
+ <head>
86
+
87
+ <meta charset="UTF-8">
88
+
89
+ <meta http-equiv="X-UA-Compatible" content="IE=edge">
90
+
91
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
92
+
93
+ <title>Document</title>
94
+
95
+ <link rel="stylesheet" href="style.css">
96
+
97
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r79/three.min.js"></script>
98
+
99
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>
100
+
101
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/1.19.0/TweenMax.min.js"></script>
102
+
103
+ <script src="test.js"></script>
104
+
105
+ </head>
106
+
107
+ <body>
108
+
109
+ <script id="fragmentShader" type="x-shader/x-fragment">
110
+
111
+
112
+
113
+ precision mediump float;
114
+
115
+
116
+
117
+ uniform float time;
118
+
119
+ uniform float mouseX;
120
+
121
+ uniform float mouseY;
122
+
123
+ uniform sampler2D texture;
124
+
125
+ varying vec2 vUv;
126
+
127
+
128
+
129
+ void main() {
130
+
131
+ float d = -distance(vec2(mouseX,mouseY), gl_FragCoord.xy);
132
+
133
+ float r = dot(gl_FragCoord.xy, vec2(0.005,0.005))/d;
134
+
135
+ vec2 tex = vec2(vUv.x + r, vUv.y + r);
136
+
137
+ gl_FragColor = vec4(texture2D(texture, tex).rgb, 1.0);
138
+
139
+ }
140
+
141
+ </script>
142
+
143
+
144
+
145
+ <script id="vertexShader" type="x-shader/x-vertex">
146
+
147
+ precision mediump float;
148
+
149
+ varying vec2 vUv;
150
+
151
+ uniform float time;
152
+
153
+ void main() {
154
+
155
+ vUv = uv;
156
+
157
+ gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
158
+
159
+ }
160
+
161
+ </script>
162
+
163
+
164
+
165
+ <div class="stage"></div>
166
+
167
+ <canvas class="noise-canvas"></canvas>
168
+
169
+
170
+
171
+ </body>
172
+
173
+ </html>
174
+
175
+ ```
176
+
177
+
178
+
179
+ css
180
+
181
+ ```
182
+
183
+ .noise-canvas{
184
+
185
+ position: absolute;
186
+
187
+ top: 0;
188
+
189
+ left: 0;
190
+
191
+ bottom: 0;
192
+
193
+ right: 0;
194
+
195
+ width: 100%;
196
+
197
+ height: 100%;
198
+
199
+ opacity: 0.3;
200
+
201
+ z-index: 2;
202
+
203
+ }
204
+
205
+ ```
206
+
207
+ js
208
+
209
+ ```
210
+
211
+ // Copyright (c) 2021 by Corentin (https://codepen.io/corentinfardeau/pen/VmZBpv)
212
+
213
+ // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
214
+
215
+ // The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
216
+
217
+ // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
218
+
219
+
220
+
221
+ 'use strict'
222
+
223
+
224
+
225
+ console.clear();
226
+
227
+ const APP_CONFIG = {
228
+
229
+ debug: false,
230
+
231
+ gridDebugSize: {
232
+
233
+ x: 10,
234
+
235
+ y: 10
236
+
237
+ },
238
+
239
+ fontSize: 600,
240
+
241
+ axisHelperSize: 10
242
+
243
+ }
244
+
245
+
246
+
247
+ class App {
248
+
249
+
250
+
251
+ constructor(){
252
+
253
+ _.bindAll(this, 'animate', 'onResize', 'onMouseMove');
254
+
255
+
256
+
257
+ this.time = 0;
258
+
259
+
260
+
261
+ this.planeHeight = 50;
262
+
263
+ this.ratio = window.innerWidth / window.innerHeight;
264
+
265
+ this.planeWidth = this.planeHeight*this.ratio;
266
+
267
+ //SET-UP CAMERA
268
+
269
+ this.cameraOpts = {
270
+
271
+ aspect: window.innerWidth / window.innerHeight,
272
+
273
+ near: 0.1,
274
+
275
+ far : 10000,
276
+
277
+ z: this.planeHeight
278
+
279
+ }
280
+
281
+
282
+
283
+ let fov = 2 * Math.atan( this.planeHeight / ( 2 * this.cameraOpts.z ) ) * ( 180 / Math.PI );
284
+
285
+
286
+
287
+ this.camera = new THREE.PerspectiveCamera(fov, this.cameraOpts.aspect, this.cameraOpts.near, this.cameraOpts.far);
288
+
289
+ this.camera.position.z = this.cameraOpts.z;
290
+
291
+
292
+
293
+ //SET-UP STAGE
294
+
295
+ this.stage = new THREE.Scene();
296
+
297
+ this.stage.add(this.camera);
298
+
299
+
300
+
301
+ //SET-UP RENDERER
302
+
303
+ this.renderer = new THREE.WebGLRenderer({ antialias: true});
304
+
305
+ this.renderer.setSize(window.innerWidth, window.innerHeight);
306
+
307
+
308
+
309
+ this.start();
310
+
311
+ }
312
+
313
+
314
+
315
+ start(){
316
+
317
+
318
+
319
+ if (APP_CONFIG.debug){
320
+
321
+ this.debug();
322
+
323
+ }
324
+
325
+ let texture = new THREE.Texture(this.createCanvas("Interactive Art Director"));
326
+
327
+ texture.needsUpdate = true;
328
+
329
+
330
+
331
+ let planeGeometry = new THREE.PlaneGeometry(this.planeWidth, this.planeHeight, 0, 0);
332
+
333
+ this.uniforms = {
334
+
335
+ texture: {
336
+
337
+ type: 't',
338
+
339
+ value: texture
340
+
341
+ },
342
+
343
+ time: {
344
+
345
+ type: "f",
346
+
347
+ value: this.time
348
+
349
+ },
350
+
351
+ mouseX: {
352
+
353
+ type: "f",
354
+
355
+ value: 0
356
+
357
+ },
358
+
359
+ mouseY: {
360
+
361
+ type: "f",
362
+
363
+ value: 0
364
+
365
+ }
366
+
367
+ }
368
+
369
+
370
+
371
+ var vertShader = document.getElementById('vertexShader').innerHTML;
372
+
373
+ var fragShader = document.getElementById('fragmentShader').innerHTML;
374
+
375
+
376
+
377
+ let planeMaterial = new THREE.ShaderMaterial({
378
+
379
+ uniforms: this.uniforms,
380
+
381
+ vertexShader: vertShader,
382
+
383
+ fragmentShader: fragShader,
384
+
385
+ wireframe : false,
386
+
387
+ wireframeLinewidth : 2,
388
+
389
+ transparent : true
390
+
391
+ });
392
+
393
+
394
+
395
+ this.plane = new THREE.Mesh(planeGeometry, planeMaterial);
396
+
397
+ this.stage.add(this.plane);
398
+
399
+
400
+
401
+ let container = document.querySelector('.stage');
402
+
403
+ container.appendChild(this.renderer.domElement);
404
+
405
+ TweenMax.ticker.addEventListener('tick', this.animate);
406
+
407
+
408
+
409
+ //ADD EVENTS LISTENER
410
+
411
+ this.listen();
412
+
413
+ }
414
+
415
+
416
+
417
+ createCanvas(text){
418
+
419
+
420
+
421
+ this.canvas = document.createElement( 'canvas' );
422
+
423
+ this.canvas.height = 4096;
424
+
425
+ this.canvas.width = this.canvas.height*this.ratio;
426
+
427
+ let context = this.canvas.getContext( '2d' );
428
+
429
+
430
+
431
+ context.beginPath();
432
+
433
+ context.rect(0, 0, this.canvas.width, this.canvas.height);
434
+
435
+ context.fillStyle = '#202020';
436
+
437
+ context.fill();
438
+
439
+ context.closePath();
440
+
441
+
442
+
443
+ context.beginPath();
444
+
445
+ context.font = 'Bold '+ APP_CONFIG.fontSize +'px Avenir';
446
+
447
+ context.fillStyle = '#262626';
448
+
449
+ let width = context.measureText(text).width;
450
+
451
+ context.fillText(text, this.canvas.width/2 - width/2, this.canvas.height/2);
452
+
453
+ context.fill();
454
+
455
+
456
+
457
+ return this.canvas;
458
+
459
+
460
+
461
+ }
462
+
463
+
464
+
465
+ debug(){
466
+
467
+ let gridHelper = new THREE.GridHelper( APP_CONFIG.gridDebugSize.x, APP_CONFIG.gridDebugSize.y );
468
+
469
+ this.stage.add( gridHelper );
470
+
471
+ let axisHelper = new THREE.AxisHelper( APP_CONFIG.axisHelperSize );
472
+
473
+ this.stage.add( axisHelper );
474
+
475
+ }
476
+
477
+
478
+
479
+ listen(){
480
+
481
+ let lazyLayout = _.debounce(this.onResize, 300);
482
+
483
+ window.addEventListener('resize', lazyLayout);
484
+
485
+ window.addEventListener('mousemove', this.onMouseMove);
486
+
487
+ }
488
+
489
+
490
+
491
+ onResize(e){
492
+
493
+ this.renderer.setSize(window.innerWidth, window.innerHeight);
494
+
495
+ this.camera.updateProjectionMatrix();
496
+
497
+ }
498
+
499
+
500
+
501
+ onMouseMove(e){
502
+
503
+ this.mousePos = {
504
+
505
+ x: e.clientX,
506
+
507
+ y: e.clientY
508
+
509
+ }
510
+
511
+ }
512
+
513
+
514
+
515
+ animate(){
516
+
517
+
518
+
519
+ if(this.mousePos){
520
+
521
+ this.uniforms.mouseX.value = this.mousePos.x;
522
+
523
+ this.uniforms.mouseY.value = window.innerHeight - this.mousePos.y;
524
+
525
+ }
526
+
527
+
528
+
529
+ this.renderer.render(this.stage, this.camera);
530
+
531
+ this.uniforms.time.value += 0.1;
532
+
533
+ }
534
+
535
+ }
536
+
537
+
538
+
539
+ new App();
540
+
541
+ ```
542
+
543
+ また、HTMLのエディタ画面では、コードが普段はすべてカラーのはずが一部白文字になっているため、正しく読み込まれていないのは分かりました。
544
+
545
+ ![イメージ説明](5d9e6c15085204ec7b1d1dd23898e0d8.png)
56
546
 
57
547
 
58
548