-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
181 lines (141 loc) · 3.82 KB
/
app.js
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
import Image1 from './sources/images/1.jpg'
import Image2 from './sources/images/2.jpg'
import Image3 from './sources/images/3.jpg'
import Image4 from './sources/images/4.jpg'
import Image5 from './sources/images/5.jpg'
import Image6 from './sources/images/6.jpg'
import './style.css'
import { Renderer, Camera, Transform, Plane } from 'ogl'
import Media from './sources/scripts/classes/Media'
export default class App {
constructor() {
this.scroll = {
ease: 0.05,
current: 0,
target: 0,
last: 0
}
this.createRenderer()
this.createCamera()
this.createScene()
this.onResize()
this.createGeometry()
this.createMedias()
this.update()
this.addListeners()
}
/*
* Creates
*/
createRenderer() {
this.renderer = new Renderer({
alpha: true,
antialias: true
})
this.gl = this.renderer.gl
this.gl.clearColor(0.79607843137, 0.79215686274, 0.74117647058, 1)
document.body.appendChild(this.gl.canvas)
}
createCamera() {
this.camera = new Camera(this.gl)
this.camera.fov = 45
this.camera.position.z = 20
this.camera.perspective({
aspect: this.gl.canvas.width / this.gl.canvas.height,
})
}
createScene() {
this.scene = new Transform()
}
createGeometry() {
this.planeGeometry = new Plane(this.gl, {
heightSegments: 50,
widthSegments: 100
})
}
createMedias() {
this.mediaImages = [
{ image: Image1, title: 'Image 1' },
{ image: Image2, title: 'Image 2' },
{ image: Image3, title: 'Image 3' },
{ image: Image4, title: 'Image 4' },
{ image: Image5, title: 'Image 5' },
{ image: Image6, title: 'Image 6' }
]
this.medias = this.mediaImages.map(({ image, title }, index) => {
const media = new Media({
geometry: this.planeGeometry,
gl: this.gl,
image,
index,
length: this.mediaImages.length,
scene: this.scene,
screen: this.screen,
title,
viewport: this.viewport
})
return media
})
}
/**
* Events
*/
onTouchDown (event) {
}
onTouchMove (event) {
}
onTouchUp (event) {
}
onWheel (event) {
}
onResize() {
// Get new screen sizes
this.screen = {
height: window.innerHeight,
width: window.innerWidth
}
this.renderer.setSize(this.screen.width, this.screen.height)
this.camera.perspective({
aspect: this.gl.canvas.width / this.gl.canvas.height
})
const fov = this.camera.fov * (Math.PI / 180)
const height = 2 * Math.tan(fov / 2) * this.camera.position.z
const width = height * this.camera.aspect
this.viewport = {
height,
width
}
// Update all meshes
if (this.medias) {
this.medias.forEach(media => media.onResize({
screen: this.screen,
viewport: this.viewport
}))
}
}
/**
* Update
*/
update() {
window.requestAnimationFrame(this.update.bind(this))
if (this.medias) {
this.medias.forEach(media => media.update(this.scroll, this.direction))
}
this.renderer.render({
scene: this.scene,
camera: this.camera
})
}
addListeners() {
window.addEventListener('resize', this.onResize.bind(this))
window.addEventListener('mousewheel', this.onWheel.bind(this))
window.addEventListener('wheel', this.onWheel.bind(this))
window.addEventListener('mousedown', this.onTouchDown.bind(this))
window.addEventListener('mousemove', this.onTouchMove.bind(this))
window.addEventListener('mouseup', this.onTouchUp.bind(this))
window.addEventListener('touchstart', this.onTouchDown.bind(this))
window.addEventListener('touchmove', this.onTouchMove.bind(this))
window.addEventListener('touchend', this.onTouchUp.bind(this))
}
}
new App()