前提
Wordpressでサイトを新規制作中です。
その中で変わったスライダーを設置したいと思い、webGLのスライダーをサイトに設置したいです。
実現したいこと
以下のサイトのスライダーを設置したい。
https://codepen.io/rzbrnl/pen/bGqXyav
発生している問題・エラーメッセージ
設置はできたもののパララックスのような動きをします。
リロードするたびにスライダーの位置が変動してしまうため、トップページからスライダーの箇所までスクロールした時にはスライダーが消えており、背景だけ残っている状況です。
試したこと
html,css,js全て読み込みは完了しており、jsはCDNで読み込んでいます。
エラーも特に出ておりません。
codepenの方で同じようにコードを書き換えたところ、同じ動きをしたのでjsが関係しているのかなと思っております。
初学者で未熟な私ですがヒントだけでも構いません。どうかお力をお貸しください。
Javascript
1console.clear(); 2const webGLCurtain = new Curtains({ container: "canvas" }); 3const planeContainer = document.querySelector(".planes"); 4const planeElements = planeContainer.querySelectorAll(".plane"); 5const draggableHidden = planeContainer.querySelector(".draggable-hidden"); 6const draggableVisible = planeContainer.querySelector(".draggable-visible"); 7const sliderNamesContainer = planeContainer.querySelector( 8 ".slider-names-container" 9); 10const sliderNames = planeContainer.querySelector(".slider-names"); 11const names = planeContainer.querySelectorAll(".slider__name"); 12const items = planeContainer.querySelectorAll(".plane"); 13const draggie = new Draggabilly(draggableHidden, { axis: "x" }); 14 15sliderNamesContainer.style.height = 16 names[0].getBoundingClientRect().height + "px"; 17 18const width = innerWidth; 19 20let mouse = { 21 currentPosX: 0, 22 previousPosX: 0, 23 minPosX: 0, 24 maxPosX: width - draggableHidden.getBoundingClientRect().width, 25 isMouseDown: false 26}; 27 28let sliderNamesProps = { 29 containerHeight: 30 sliderNamesContainer.getBoundingClientRect().height - 31 sliderNames.getBoundingClientRect().height 32}; 33 34let start = performance.now(); 35let velocity = 0; 36let lastVelocity = 0; 37let posNames = 0; 38let lastPosNames = 0; 39let amplitude = 0.1; 40let lastMouse = 0; 41let easing = 0.05; 42let cancelAnimation = true; 43let isOut = false; 44 45const planes = []; 46 47const MathUtils = { 48 lerp: (a, b, n) => (1 - n) * a + n * b, 49 map_range: (value, low1, high1, low2, high2) => { 50 return low2 + ((high2 - low2) * (value - low1)) / (high1 - low1); 51 } 52}; 53 54const createCanvas = () => { 55 const shader = { 56 vertex: ` 57 #ifdef GL_ES 58 precision mediump float; 59 #endif 60 61 #define PI 3.14159265359 62 63 // those are the mandatory attributes that the lib sets 64 attribute vec3 aVertexPosition; 65 attribute vec2 aTextureCoord; 66 67 // those are mandatory uniforms that the lib sets and that contain our model view and projection matrix 68 uniform mat4 uMVMatrix; 69 uniform mat4 uPMatrix; 70 71 uniform mat4 planeTextureMatrix; 72 73 // if you want to pass your vertex and texture coords to the fragment shader 74 varying vec3 vVertexPosition; 75 varying vec2 vTextureCoord; 76 77 varying float vDirection; 78 79 uniform float uDirection; 80 81 void main() { 82 vec3 position = aVertexPosition; 83 84 float x = sin((position.y * 0.5 - 0.5) * PI) * uDirection; 85 86 position.x -= x; 87 88 gl_Position = uPMatrix * uMVMatrix * vec4(position, 1.0); 89 90 // set the varyings 91 vTextureCoord = (planeTextureMatrix * vec4(aTextureCoord, 0., 1.)).xy; 92 vVertexPosition = position; 93 94 vDirection = uDirection; 95 }`, 96 fragment: ` 97 #ifdef GL_ES 98 precision mediump float; 99 #endif 100 101 #define PI2 6.28318530718 102 #define PI 3.14159265359 103 #define S(a,b,n) smoothstep(a,b,n) 104 105 // get our varyings 106 varying vec3 vVertexPosition; 107 varying vec2 vTextureCoord; 108 109 // the uniform we declared inside our javascript 110 uniform float uTime; 111 112 // our texture sampler (default name, to use a different name please refer to the documentation) 113 uniform sampler2D planeTexture; 114 115 varying float vDirection; 116 117 void main(){ 118 vec2 uv = vTextureCoord; 119 120 float scale = -abs(vDirection) * 0.8; 121 122 uv = (uv - 0.5) * scale + uv; 123 124 float r = texture2D(planeTexture, vec2(uv.x - vDirection * 0.1, uv.y)).r; 125 float g = texture2D(planeTexture, vec2(uv.x - vDirection * 0.4, uv.y)).g; 126 float b = texture2D(planeTexture, vec2(uv.x - vDirection * 0.4, uv.y)).b; 127 128 gl_FragColor = vec4(r, g, b, 1.0); 129 } 130 ` 131 }; 132 133 const params = { 134 vertexShader: shader.vertex, // our vertex shader ID 135 fragmentShader: shader.fragment, // our framgent shader ID 136 widthSegments: 40, 137 heightSegments: 40, // we now have 40*40*6 = 9600 vertices ! 138 uniforms: { 139 time: { 140 name: "uTime", 141 type: "1f", 142 value: 0 143 }, 144 direction: { 145 name: "uDirection", 146 type: "1f", 147 value: 0 148 } 149 } 150 }; 151 152 webGLCurtain.disableDrawing(); 153 154 // crear nuestros planos 155 for (let i = 0; i < planeElements.length; i++) { 156 const plane = webGLCurtain.addPlane(planeElements[i], params); 157 158 planes.push(plane); 159 startPlane(items[i], plane); 160 } 161 162 initEvents(); 163 requestAnimationFrame(onUpdate); 164}; 165 166const startPlane = (planeElement, plane) => { 167 plane.onLoading(() => { 168 plane.mouseOver = false; 169 170 planeElement.addEventListener("mouseenter", () => { 171 plane.mouseOver = true; 172 webGLCurtain.enableDrawing(); 173 }); 174 175 planeElement.addEventListener("mouseleave", () => { 176 plane.mouseOver = false; 177 }); 178 179 webGLCurtain.needRender(); 180 }); 181}; 182 183const onUpdate = () => { 184 if (!cancelAnimation) { 185 for (let i = 0, l = planes.length; i < l; i++) { 186 planes[i].uniforms.direction.value = lastVelocity; 187 planes[i].updatePosition(); 188 } 189 190 const { lerp } = MathUtils; 191 const now = performance.now(); 192 193 const velocity = 194 ((mouse.currentPosX - lastMouse) / (now - start)) * amplitude; 195 196 lastVelocity = lerp(lastVelocity, velocity, easing); 197 198 start = now; 199 lastMouse = mouse.currentPosX; 200 201 mouse.previousPosX = lerp(mouse.previousPosX, mouse.currentPosX, easing); 202 203 draggableVisible.style.transform = `translate3d(${mouse.previousPosX}px, 0, 0)`; 204 205 posNames = 206 (draggie.position.x / mouse.maxPosX) * sliderNamesProps.containerHeight; 207 208 lastPosNames = lerp(lastPosNames, posNames, easing); 209 210 sliderNames.style.transform = `translate3d(0,${lastPosNames}px,0)`; 211 212 if (Math.round(mouse.previousPosX) === mouse.currentPosX) { 213 webGLCurtain.disableDrawing(); 214 cancelAnimation = true; 215 } 216 } 217 218 requestAnimationFrame(onUpdate); 219}; 220 221// elemento arrastrar 222const onDragMove = () => { 223 cancelAnimation = false; 224 webGLCurtain.enableDrawing(); 225 226 // Si esta en el limite se trasladara la mitad del ancho del viewport 227 if (draggie.position.x > mouse.minPosX) { 228 mouse.currentPosX = MathUtils.map_range( 229 draggie.position.x, 230 0, 231 innerWidth, 232 0, 233 innerWidth / 3 234 ); 235 } else if (draggie.position.x < mouse.maxPosX) { 236 mouse.currentPosX = MathUtils.map_range( 237 draggie.position.x, 238 mouse.maxPosX, 239 mouse.maxPosX - innerWidth, 240 mouse.maxPosX, 241 mouse.maxPosX - innerWidth / 3 242 ); 243 } else { 244 mouse.currentPosX = draggie.position.x; 245 246 easing = 0.05; 247 } 248 249 amplitude = 0.1; 250}; 251 252const onDragStart = (e) => { 253 planeContainer.style.cursor = "grabbing"; // Aplica al cursor el icono de agarrando; 254}; 255 256const onDragEnd = () => { 257 planeContainer.style.cursor = "grab"; // Aplica al cursor el icono de agarrar 258 259 // Si esta en el limite volvera a su posicion inicial o final 260 if (draggie.position.x > mouse.minPosX) { 261 mouse.currentPosX = 0; 262 draggie.setPosition(mouse.currentPosX, draggie.position.y); 263 amplitude = 0; 264 easing = 0.07; 265 } else if (draggie.position.x < mouse.maxPosX) { 266 mouse.currentPosX = mouse.maxPosX; 267 draggie.setPosition(mouse.currentPosX, draggie.position.y); 268 amplitude = 0; 269 easing = 0.07; 270 } else { 271 draggie.setPosition(mouse.currentPosX, draggie.position.y); 272 } 273}; 274 275// Al reescalar calcula denuevo la posicion maxima 276const onResize = () => { 277 sliderNamesContainer.style.height = 278 names[0].getBoundingClientRect().height + "px"; 279 280 mouse.maxPosX = 281 window.innerWidth - draggableHidden.getBoundingClientRect().width; 282 283 sliderNamesProps = { 284 containerHeight: 285 sliderNamesContainer.getBoundingClientRect().height - 286 sliderNames.getBoundingClientRect().height 287 }; 288}; 289 290const initEvents = () => { 291 draggie.on("dragMove", () => { 292 if (isOut) return; 293 onDragMove(); 294 }); 295 draggie.on("pointerDown", () => { 296 isOut = false; 297 onDragStart(); 298 }); 299 draggie.on("pointerUp", onDragEnd); 300 301 planeContainer.addEventListener("mouseleave", () => { 302 isOut = true; 303 onDragEnd(); 304 }); 305 306 window.addEventListener("resize", onResize); 307}; 308 309window.addEventListener("load", createCanvas); 310

回答1件
あなたの回答
tips
プレビュー