-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3D Image Transition.html
More file actions
523 lines (438 loc) · 15 KB
/
3D Image Transition.html
File metadata and controls
523 lines (438 loc) · 15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Three Image Transition</title>
<style>
body {
margin: 0;
overflow: hidden;
}
canvas {
background-image: radial-gradient(#666, #333);
}
#instructions {
position: absolute;
color: #fff;
bottom: 0;
padding-bottom: 6px;
font-family: sans-serif;
width: 100%;
text-align: center;
pointer-events: none;
}
</style>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r79/three.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/1.19.0/TweenMax.min.js"></script>
<script src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/175711/bas.js"></script>
</head>
<body>
<div id="three-container"></div>
<div id="instructions">click and drag to control the animation</div>
<script>
window.onload = init
console.ward = function () {} // what warnings?
function init() {
var root = new THREERoot({
createCameraControls: !true,
antialias: window.devicePixelRatio === 1,
fov: 80,
})
root.renderer.setClearColor(0x000000, 0)
root.renderer.setPixelRatio(window.devicePixelRatio || 1)
root.camera.position.set(0, 0, 60)
var width = 100
var height = 60
var slide = new Slide(width, height, "out")
var l1 = new THREE.ImageLoader()
l1.setCrossOrigin("Anonymous")
l1.load(
"https://s3-us-west-2.amazonaws.com/s.cdpn.io/175711/winter.jpg",
function (img) {
slide.setImage(img)
},
)
root.scene.add(slide)
var slide2 = new Slide(width, height, "in")
var l2 = new THREE.ImageLoader()
l2.setCrossOrigin("Anonymous")
l2.load(
"https://s3-us-west-2.amazonaws.com/s.cdpn.io/175711/spring.jpg",
function (img) {
slide2.setImage(img)
},
)
root.scene.add(slide2)
var tl = new TimelineMax({ repeat: -1, repeatDelay: 1.0, yoyo: true })
tl.add(slide.transition(), 0)
tl.add(slide2.transition(), 0)
createTweenScrubber(tl)
window.addEventListener("keyup", function (e) {
if (e.keyCode === 80) {
tl.paused(!tl.paused())
}
})
}
////////////////////
// CLASSES
////////////////////
function Slide(width, height, animationPhase) {
var plane = new THREE.PlaneGeometry(
width,
height,
width * 2,
height * 2,
)
THREE.BAS.Utils.separateFaces(plane)
var geometry = new SlideGeometry(plane)
geometry.bufferUVs()
var aAnimation = geometry.createAttribute("aAnimation", 2)
var aStartPosition = geometry.createAttribute("aStartPosition", 3)
var aControl0 = geometry.createAttribute("aControl0", 3)
var aControl1 = geometry.createAttribute("aControl1", 3)
var aEndPosition = geometry.createAttribute("aEndPosition", 3)
var i, i2, i3, i4, v
var minDuration = 0.8
var maxDuration = 1.2
var maxDelayX = 0.9
var maxDelayY = 0.125
var stretch = 0.11
this.totalDuration = maxDuration + maxDelayX + maxDelayY + stretch
var startPosition = new THREE.Vector3()
var control0 = new THREE.Vector3()
var control1 = new THREE.Vector3()
var endPosition = new THREE.Vector3()
var tempPoint = new THREE.Vector3()
function getControlPoint0(centroid) {
var signY = Math.sign(centroid.y)
tempPoint.x = THREE.Math.randFloat(0.1, 0.3) * 50
tempPoint.y = signY * THREE.Math.randFloat(0.1, 0.3) * 70
tempPoint.z = THREE.Math.randFloatSpread(20)
return tempPoint
}
function getControlPoint1(centroid) {
var signY = Math.sign(centroid.y)
tempPoint.x = THREE.Math.randFloat(0.3, 0.6) * 50
tempPoint.y = -signY * THREE.Math.randFloat(0.3, 0.6) * 70
tempPoint.z = THREE.Math.randFloatSpread(20)
return tempPoint
}
for (
i = 0, i2 = 0, i3 = 0, i4 = 0;
i < geometry.faceCount;
i++, i2 += 6, i3 += 9, i4 += 12
) {
var face = plane.faces[i]
var centroid = THREE.BAS.Utils.computeCentroid(plane, face)
// animation
var duration = THREE.Math.randFloat(minDuration, maxDuration)
var delayX = THREE.Math.mapLinear(
centroid.x,
-width * 0.5,
width * 0.5,
0.0,
maxDelayX,
)
var delayY
if (animationPhase === "in") {
delayY = THREE.Math.mapLinear(
Math.abs(centroid.y),
0,
height * 0.5,
0.0,
maxDelayY,
)
} else {
delayY = THREE.Math.mapLinear(
Math.abs(centroid.y),
0,
height * 0.5,
maxDelayY,
0.0,
)
}
for (v = 0; v < 6; v += 2) {
aAnimation.array[i2 + v] =
delayX + delayY + Math.random() * stretch * duration
aAnimation.array[i2 + v + 1] = duration
}
// positions
endPosition.copy(centroid)
startPosition.copy(centroid)
if (animationPhase === "in") {
control0.copy(centroid).sub(getControlPoint0(centroid))
control1.copy(centroid).sub(getControlPoint1(centroid))
} else {
// out
control0.copy(centroid).add(getControlPoint0(centroid))
control1.copy(centroid).add(getControlPoint1(centroid))
}
for (v = 0; v < 9; v += 3) {
aStartPosition.array[i3 + v] = startPosition.x
aStartPosition.array[i3 + v + 1] = startPosition.y
aStartPosition.array[i3 + v + 2] = startPosition.z
aControl0.array[i3 + v] = control0.x
aControl0.array[i3 + v + 1] = control0.y
aControl0.array[i3 + v + 2] = control0.z
aControl1.array[i3 + v] = control1.x
aControl1.array[i3 + v + 1] = control1.y
aControl1.array[i3 + v + 2] = control1.z
aEndPosition.array[i3 + v] = endPosition.x
aEndPosition.array[i3 + v + 1] = endPosition.y
aEndPosition.array[i3 + v + 2] = endPosition.z
}
}
var material = new THREE.BAS.BasicAnimationMaterial(
{
shading: THREE.FlatShading,
side: THREE.DoubleSide,
uniforms: {
uTime: { type: "f", value: 0 },
},
shaderFunctions: [
THREE.BAS.ShaderChunk["cubic_bezier"],
//THREE.BAS.ShaderChunk[(animationPhase === 'in' ? 'ease_out_cubic' : 'ease_in_cubic')],
THREE.BAS.ShaderChunk["ease_in_out_cubic"],
THREE.BAS.ShaderChunk["quaternion_rotation"],
],
shaderParameters: [
"uniform float uTime;",
"attribute vec2 aAnimation;",
"attribute vec3 aStartPosition;",
"attribute vec3 aControl0;",
"attribute vec3 aControl1;",
"attribute vec3 aEndPosition;",
],
shaderVertexInit: [
"float tDelay = aAnimation.x;",
"float tDuration = aAnimation.y;",
"float tTime = clamp(uTime - tDelay, 0.0, tDuration);",
"float tProgress = ease(tTime, 0.0, 1.0, tDuration);",
//'float tProgress = tTime / tDuration;'
],
shaderTransformPosition: [
animationPhase === "in"
? "transformed *= tProgress;"
: "transformed *= 1.0 - tProgress;",
"transformed += cubicBezier(aStartPosition, aControl0, aControl1, aEndPosition, tProgress);",
],
},
{
map: new THREE.Texture(),
},
)
THREE.Mesh.call(this, geometry, material)
this.frustumCulled = false
}
Slide.prototype = Object.create(THREE.Mesh.prototype)
Slide.prototype.constructor = Slide
Object.defineProperty(Slide.prototype, "time", {
get: function () {
return this.material.uniforms["uTime"].value
},
set: function (v) {
this.material.uniforms["uTime"].value = v
},
})
Slide.prototype.setImage = function (image) {
this.material.uniforms.map.value.image = image
this.material.uniforms.map.value.needsUpdate = true
}
Slide.prototype.transition = function () {
return TweenMax.fromTo(
this,
3.0,
{ time: 0.0 },
{ time: this.totalDuration, ease: Power0.easeInOut },
)
}
function SlideGeometry(model) {
THREE.BAS.ModelBufferGeometry.call(this, model)
}
SlideGeometry.prototype = Object.create(
THREE.BAS.ModelBufferGeometry.prototype,
)
SlideGeometry.prototype.constructor = SlideGeometry
SlideGeometry.prototype.bufferPositions = function () {
var positionBuffer = this.createAttribute("position", 3).array
for (var i = 0; i < this.faceCount; i++) {
var face = this.modelGeometry.faces[i]
var centroid = THREE.BAS.Utils.computeCentroid(
this.modelGeometry,
face,
)
var a = this.modelGeometry.vertices[face.a]
var b = this.modelGeometry.vertices[face.b]
var c = this.modelGeometry.vertices[face.c]
positionBuffer[face.a * 3] = a.x - centroid.x
positionBuffer[face.a * 3 + 1] = a.y - centroid.y
positionBuffer[face.a * 3 + 2] = a.z - centroid.z
positionBuffer[face.b * 3] = b.x - centroid.x
positionBuffer[face.b * 3 + 1] = b.y - centroid.y
positionBuffer[face.b * 3 + 2] = b.z - centroid.z
positionBuffer[face.c * 3] = c.x - centroid.x
positionBuffer[face.c * 3 + 1] = c.y - centroid.y
positionBuffer[face.c * 3 + 2] = c.z - centroid.z
}
}
function THREERoot(params) {
params = utils.extend(
{
fov: 60,
zNear: 10,
zFar: 100000,
createCameraControls: true,
},
params,
)
this.renderer = new THREE.WebGLRenderer({
antialias: params.antialias,
alpha: true,
})
this.renderer.setPixelRatio(Math.min(2, window.devicePixelRatio || 1))
document
.getElementById("three-container")
.appendChild(this.renderer.domElement)
this.camera = new THREE.PerspectiveCamera(
params.fov,
window.innerWidth / window.innerHeight,
params.zNear,
params.zfar,
)
this.scene = new THREE.Scene()
if (params.createCameraControls) {
this.controls = new THREE.OrbitControls(
this.camera,
this.renderer.domElement,
)
}
this.resize = this.resize.bind(this)
this.tick = this.tick.bind(this)
this.resize()
this.tick()
window.addEventListener("resize", this.resize, false)
}
THREERoot.prototype = {
tick: function () {
this.update()
this.render()
requestAnimationFrame(this.tick)
},
update: function () {
this.controls && this.controls.update()
},
render: function () {
this.renderer.render(this.scene, this.camera)
},
resize: function () {
this.camera.aspect = window.innerWidth / window.innerHeight
this.camera.updateProjectionMatrix()
this.renderer.setSize(window.innerWidth, window.innerHeight)
},
}
////////////////////
// UTILS
////////////////////
var utils = {
extend: function (dst, src) {
for (var key in src) {
dst[key] = src[key]
}
return dst
},
randSign: function () {
return Math.random() > 0.5 ? 1 : -1
},
ease: function (ease, t, b, c, d) {
return b + ease.getRatio(t / d) * c
},
fibSpherePoint: (function () {
var vec = { x: 0, y: 0, z: 0 }
var G = Math.PI * (3 - Math.sqrt(5))
return function (i, n, radius) {
var step = 2.0 / n
var r, phi
vec.y = i * step - 1 + step * 0.5
r = Math.sqrt(1 - vec.y * vec.y)
phi = i * G
vec.x = Math.cos(phi) * r
vec.z = Math.sin(phi) * r
radius = radius || 1
vec.x *= radius
vec.y *= radius
vec.z *= radius
return vec
}
})(),
spherePoint: (function () {
return function (u, v) {
u === undefined && (u = Math.random())
v === undefined && (v = Math.random())
var theta = 2 * Math.PI * u
var phi = Math.acos(2 * v - 1)
var vec = {}
vec.x = Math.sin(phi) * Math.cos(theta)
vec.y = Math.sin(phi) * Math.sin(theta)
vec.z = Math.cos(phi)
return vec
}
})(),
}
function createTweenScrubber(tween, seekSpeed) {
seekSpeed = seekSpeed || 0.001
function stop() {
TweenMax.to(tween, 1, { timeScale: 0 })
}
function resume() {
TweenMax.to(tween, 1, { timeScale: 1 })
}
function seek(dx) {
var progress = tween.progress()
var p = THREE.Math.clamp(progress + dx * seekSpeed, 0, 1)
tween.progress(p)
}
var _cx = 0
// desktop
var mouseDown = false
document.body.style.cursor = "pointer"
window.addEventListener("mousedown", function (e) {
mouseDown = true
document.body.style.cursor = "ew-resize"
_cx = e.clientX
stop()
})
window.addEventListener("mouseup", function (e) {
mouseDown = false
document.body.style.cursor = "pointer"
resume()
})
window.addEventListener("mousemove", function (e) {
if (mouseDown === true) {
var cx = e.clientX
var dx = cx - _cx
_cx = cx
seek(dx)
}
})
// mobile
window.addEventListener("touchstart", function (e) {
_cx = e.touches[0].clientX
stop()
e.preventDefault()
})
window.addEventListener("touchend", function (e) {
resume()
e.preventDefault()
})
window.addEventListener("touchmove", function (e) {
var cx = e.touches[0].clientX
var dx = cx - _cx
_cx = cx
seek(dx)
e.preventDefault()
})
}
</script>
</body>
</html>