質問編集履歴

1

コードを追加しました。よろしくお願いいたします。

2022/07/22 10:31

投稿

hiro_0000
hiro_0000

スコア22

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