質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
CSS3

CSS(Cascading Style Sheet)の第3版です。CSS3と略されることが多いです。色やデザインを柔軟に変更することが可能になります。

HTML5

HTML5 (Hyper Text Markup Language、バージョン 5)は、マークアップ言語であるHTMLの第5版です。

Three.js

Three.jsはWebGLをサポートしているJavaScriptの3D描画用ライブラリです。

JavaScript

JavaScriptは、プログラミング言語のひとつです。ネットスケープコミュニケーションズで開発されました。 開発当初はLiveScriptと呼ばれていましたが、業務提携していたサン・マイクロシステムズが開発したJavaが脚光を浴びていたことから、JavaScriptと改名されました。 動きのあるWebページを作ることを目的に開発されたもので、主要なWebブラウザのほとんどに搭載されています。

WebGL

WebGL(ウェブジーエル)は、ウェブブラウザで 3次元コンピュータグラフィックスを表示させるための標準仕様です。

Q&A

解決済

1回答

746閲覧

canvasのサイズを変更したいです。

m_y_kameta

総合スコア14

CSS3

CSS(Cascading Style Sheet)の第3版です。CSS3と略されることが多いです。色やデザインを柔軟に変更することが可能になります。

HTML5

HTML5 (Hyper Text Markup Language、バージョン 5)は、マークアップ言語であるHTMLの第5版です。

Three.js

Three.jsはWebGLをサポートしているJavaScriptの3D描画用ライブラリです。

JavaScript

JavaScriptは、プログラミング言語のひとつです。ネットスケープコミュニケーションズで開発されました。 開発当初はLiveScriptと呼ばれていましたが、業務提携していたサン・マイクロシステムズが開発したJavaが脚光を浴びていたことから、JavaScriptと改名されました。 動きのあるWebページを作ることを目的に開発されたもので、主要なWebブラウザのほとんどに搭載されています。

WebGL

WebGL(ウェブジーエル)は、ウェブブラウザで 3次元コンピュータグラフィックスを表示させるための標準仕様です。

0グッド

0クリップ

投稿2022/08/30 08:16

編集2022/08/31 02:03

前提

Three.jsでWebGLを操作して、下記URLのようなスライドを
アイキャッチとして配置をしたいです。

https://codepen.io/ReGGae/pen/bmyYEj

実現したいこと

自作ホームページへ実装することができましたが、スライド部分のサイズが合いません。
画面いっぱいに表示されるように指定したいです。

発生している問題・エラーメッセージ

今回、canvasを使用しているのですが、htmlには記述せず、Three.jsでcanvasが追加されるようになっていると解釈しています。
実装したのち、開発ツールで確認すると下記のようになっていました。

<div class="slider__inner js-slider__inner">     <canvas width="1903" height="26" style="width: 1903px; height: 26px;"></canvas> </div>

これでは、画面時の上部に26px分しか表示されません。

そこでcssで高さや横幅の指定を行いました。
ですが、それでも修正ができませんでした。

また、下記js上での、setSize()を触ってみたりもしましたが変更しませんでした。
指定するとしたら下記コードをどのように記述するのが良いでしょうか?

javascript

1class Slider { 2 constructor() { 3 this.bindAll(); 4 5 this.vert = ` 6 varying vec2 vUv; 7 void main() { 8 vUv = uv; 9 gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0); 10 } 11 `; 12 13 this.frag = ` 14 varying vec2 vUv; 15 16 uniform sampler2D texture1; 17 uniform sampler2D texture2; 18 uniform sampler2D disp; 19 20 uniform float dispPower; 21 uniform float intensity; 22 23 uniform vec2 size; 24 uniform vec2 res; 25 26 vec2 backgroundCoverUv( vec2 screenSize, vec2 imageSize, vec2 uv ) { 27 float screenRatio = screenSize.x / screenSize.y; 28 float imageRatio = imageSize.x / imageSize.y; 29 vec2 newSize = screenRatio < imageRatio 30 ? vec2(imageSize.x * (screenSize.y / imageSize.y), screenSize.y) 31 : vec2(screenSize.x, imageSize.y * (screenSize.x / imageSize.x)); 32 vec2 newOffset = (screenRatio < imageRatio 33 ? vec2((newSize.x - screenSize.x) / 2.0, 0.0) 34 : vec2(0.0, (newSize.y - screenSize.y) / 2.0)) / newSize; 35 return uv * screenSize / newSize + newOffset; 36 } 37 38 void main() { 39 vec2 uv = vUv; 40 41 vec4 disp = texture2D(disp, uv); 42 vec2 dispVec = vec2(disp.x, disp.y); 43 44 vec2 distPos1 = uv + (dispVec * intensity * dispPower); 45 vec2 distPos2 = uv + (dispVec * -(intensity * (1.0 - dispPower))); 46 47 vec4 _texture1 = texture2D(texture1, distPos1); 48 vec4 _texture2 = texture2D(texture2, distPos2); 49 50 gl_FragColor = mix(_texture1, _texture2, dispPower); 51 } 52 `; 53 54 this.el = document.querySelector('.js-slider'); 55 this.inner = this.el.querySelector('.js-slider__inner'); //()内クラスを呼び出し 56 this.slides = [...this.el.querySelectorAll('.js-slide')]; 57 this.bullets = [...this.el.querySelectorAll('.js-slider-bullet')]; 58 59 this.renderer = null; //初期値を空白の状態にする 60 this.scene = null; 61 this.clock = null; 62 this.camera = null; 63 64 65 66 //ここに希望画像を挿入する 67 this.images = [ 68 'img/eyecatch.jpg', 69 'img/1.jpg', 70 'img/2.jpg']; 71 72 73 this.data = { 74 current: 0, 75 next: 1, 76 total: this.images.length - 1, 77 delta: 0 }; 78 79 80 this.state = { 81 animating: false, 82 text: false, 83 initial: true }; 84 85 86 this.textures = null; 87 88 this.init(); 89 } 90 91 bindAll() { 92 ['render', 'nextSlide']. 93 forEach(fn => this[fn] = this[fn].bind(this)); 94 } 95 96 setStyles() { 97 this.slides.forEach((slide, index) => { //繰り返し作業をする forEach 98 if (index === 0) return; 99 100 TweenMax.set(slide, { autoAlpha: 0 }); //初期状態としてopacity: 0;とvisibility: hidden;が指定される 101 }); 102 103 this.bullets.forEach((bullet, index) => { 104 if (index === 0) return; 105 106 const txt = bullet.querySelector('.js-slider-bullet__text'); 107 const line = bullet.querySelector('.js-slider-bullet__line'); 108 109 TweenMax.set(txt, { 110 alpha: 0.25 }); 111 112 TweenMax.set(line, { 113 scaleX: 0, 114 transformOrigin: 'left' }); 115 116 }); 117 } 118 119 cameraSetup() { 120 this.camera = new THREE.OrthographicCamera( //平行投影を表現するカメラ設定 121 this.el.offsetWidth / -2, // new THREE.OrthographicCamera(left, right, top, bottom, near, far) 122 this.el.offsetWidth / 2, 123 this.el.offsetHeight / 2, 124 this.el.offsetHeight / -2, 125 1, 126 1000); 127 128 129 this.camera.lookAt(this.scene.position); //どの位置からでも指定した座標に強制的に向かせることができる  130 this.camera.position.z = 1; 131 } 132 133 setup() { 134 this.scene = new THREE.Scene(); 135 this.clock = new THREE.Clock(true); 136 137 this.renderer = new THREE.WebGLRenderer({ alpha: true }); 138 this.renderer.setPixelRatio(window.devicePixelRatio); 139 this.renderer.setSize(this.el.offsetWidth, this.el.offsetHeight); //サイズ設定 140 141 this.inner.appendChild(this.renderer.domElement); //子要素を追加 142 } 143 144 loadTextures() { 145 const loader = new THREE.TextureLoader(); 146 loader.crossOrigin = ''; 147 148 this.textures = []; 149 this.images.forEach((image, index) => { 150 const texture = loader.load(image + '?v=' + Date.now(), this.render); 151 152 texture.minFilter = THREE.LinearFilter; 153 texture.generateMipmaps = false; 154 155 if (index === 0 && this.mat) { 156 this.mat.uniforms.size.value = [ 157 texture.image.naturalWidth, 158 texture.image.naturalHeight]; 159 160 } 161 162 this.textures.push(texture); 163 }); 164 165 166 167 //ここにスライド時の模様を指定する 168 this.disp = loader.load('img/rock-_disp.png', this.render); 169 this.disp.magFilter = this.disp.minFilter = THREE.LinearFilter; 170 this.disp.wrapS = this.disp.wrapT = THREE.RepeatWrapping; 171 } 172 173 createMesh() { 174 this.mat = new THREE.ShaderMaterial({ 175 uniforms: { 176 dispPower: { type: 'f', value: 0.0 }, 177 intensity: { type: 'f', value: 0.5 }, 178 res: { value: new THREE.Vector2(window.innerWidth, window.innerHeight) }, 179 size: { value: new THREE.Vector2(1, 1) }, 180 texture1: { type: 't', value: this.textures[0] }, 181 texture2: { type: 't', value: this.textures[1] }, 182 disp: { type: 't', value: this.disp } }, 183 184 transparent: true, 185 vertexShader: this.vert, 186 fragmentShader: this.frag }); 187 188 189 const geometry = new THREE.PlaneBufferGeometry( 190 this.el.offsetWidth, 191 this.el.offsetHeight, 192 1); 193 194 195 const mesh = new THREE.Mesh(geometry, this.mat); 196 197 this.scene.add(mesh); 198 } 199 200 transitionNext() { 201 TweenMax.to(this.mat.uniforms.dispPower, 2.5, { 202 value: 1, 203 ease: Expo.easeInOut, 204 onUpdate: this.render, 205 onComplete: () => { 206 this.mat.uniforms.dispPower.value = 0.0; 207 this.changeTexture(); 208 this.render.bind(this); 209 this.state.animating = false; 210 } }); 211 212 213 const current = this.slides[this.data.current]; 214 const next = this.slides[this.data.next]; 215 216 const currentImages = current.querySelectorAll('.js-slide__img'); 217 const nextImages = next.querySelectorAll('.js-slide__img'); 218 219 const currentText = current.querySelectorAll('.js-slider__text-line div'); 220 const nextText = next.querySelectorAll('.js-slider__text-line div'); 221 222 const currentBullet = this.bullets[this.data.current]; 223 const nextBullet = this.bullets[this.data.next]; 224 225 const currentBulletTxt = currentBullet.querySelectorAll('.js-slider-bullet__text'); 226 const nextBulletTxt = nextBullet.querySelectorAll('.js-slider-bullet__text'); 227 228 const currentBulletLine = currentBullet.querySelectorAll('.js-slider-bullet__line'); 229 const nextBulletLine = nextBullet.querySelectorAll('.js-slider-bullet__line'); 230 231 const tl = new TimelineMax({ paused: true }); 232 233 if (this.state.initial) { 234 TweenMax.to('.js-scroll', 1.5, { 235 yPercent: 100, 236 alpha: 0, 237 ease: Power4.easeInOut }); 238 239 240 this.state.initial = false; 241 } 242 243 tl. 244 staggerFromTo(currentImages, 1.5, { 245 yPercent: 0, 246 scale: 1 }, 247 { 248 yPercent: -185, 249 scaleY: 1.5, 250 ease: Expo.easeInOut }, 251 0.075). 252 to(currentBulletTxt, 1.5, { 253 alpha: 0.25, 254 ease: Linear.easeNone }, 255 0). 256 set(currentBulletLine, { 257 transformOrigin: 'right' }, 258 0). 259 to(currentBulletLine, 1.5, { 260 scaleX: 0, 261 ease: Expo.easeInOut }, 262 0); 263 264 if (currentText) { 265 tl. 266 fromTo(currentText, 2, { 267 yPercent: 0 }, 268 { 269 yPercent: -100, 270 ease: Power4.easeInOut }, 271 0); 272 } 273 274 tl. 275 set(current, { 276 autoAlpha: 0 }). 277 278 set(next, { 279 autoAlpha: 1 }, 280 1); 281 282 if (nextText) { 283 tl. 284 fromTo(nextText, 2, { 285 yPercent: 100 }, 286 { 287 yPercent: 0, 288 ease: Power4.easeOut }, 289 1.5); 290 } 291 292 tl. 293 staggerFromTo(nextImages, 1.5, { 294 yPercent: 150, 295 scaleY: 1.5 }, 296 { 297 yPercent: 0, 298 scaleY: 1, 299 ease: Expo.easeInOut }, 300 0.075, 1). 301 to(nextBulletTxt, 1.5, { 302 alpha: 1, 303 ease: Linear.easeNone }, 304 1). 305 set(nextBulletLine, { 306 transformOrigin: 'left' }, 307 1). 308 to(nextBulletLine, 1.5, { 309 scaleX: 1, 310 ease: Expo.easeInOut }, 311 1); 312 313 tl.play(); 314 } 315 316 prevSlide() { 317 318 } 319 320 nextSlide() { 321 if (this.state.animating) return; 322 323 this.state.animating = true; 324 325 this.transitionNext(); 326 327 this.data.current = this.data.current === this.data.total ? 0 : this.data.current + 1; 328 this.data.next = this.data.current === this.data.total ? 0 : this.data.current + 1; 329 } 330 331 changeTexture() { 332 this.mat.uniforms.texture1.value = this.textures[this.data.current]; 333 this.mat.uniforms.texture2.value = this.textures[this.data.next]; 334 } 335 336 listeners() { 337 //window.addEventListener('wheel', this.nextSlide, { passive: true });  //マウスホイールでスライドを消す 338 setInterval(this.nextSlide, 1000, { passive: true }); //自動スライドに設定 1000=1秒ごとにスライド 339 } 340 341 render() { 342 this.renderer.render(this.scene, this.camera); 343 } 344 345 init() { 346 this.setup(); 347 this.cameraSetup(); 348 this.loadTextures(); 349 this.createMesh(); 350 this.setStyles(); 351 this.render(); 352 this.listeners(); 353 }} 354 355 356// Toggle active link 357const links = document.querySelectorAll('.js-nav a'); 358 359links.forEach(link => { 360 link.addEventListener('click', e => { 361 e.preventDefault(); 362 links.forEach(other => other.classList.remove('is-active')); 363 link.classList.add('is-active'); 364 }); 365}); 366 367// Init classes 368const slider = new Slider(); 369 370 371 372

canvansのサイズ変更の経験がある方、これに関して分かる方いらっしゃいましたら教えていただきたいです。
よろしくお願いいたします。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

renderer.setSize() でサイズ調整が行えたかと思います。

js

1function resize() { 2 let container = document.getElementById('container'); 3 width = container.offsetWidth; 4 height = container.offsetHeight; 5 6 camera.aspect = width / height; 7 camera.updateProjectionMatrix(); 8 9 renderer.setSize( width, height ); 10}

投稿2022/08/30 15:55

cx20

総合スコア4633

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

m_y_kameta

2022/08/31 02:06

ありがとうございます renderer.resize()を指定してみましたが、サイズ変更することができませんでした、、、。 jsのソース上にすでに、その設定がしてあり、それと何か関係があるのでしょうか? Three.jsを触るのが初めてで、なかなかソース自体を理解しきれておりません。申し訳ないです。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問