-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
139 lines (116 loc) · 3.4 KB
/
main.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
import "./style.css";
import * as THREE from "three";
//canvas
const canvas = document.querySelector("#webgl");
//シーン
const scene = new THREE.Scene();
const textureLoader = new THREE.TextureLoader();
const bgTexture = textureLoader.load("/bg.jpg");
scene.background = bgTexture;
//サイズ
const sizes = {
width: innerWidth,
height: innerHeight,
};
//カメラ
const camera = new THREE.PerspectiveCamera(
75,
sizes.width / sizes.height,
0.1,
1000
);
//レンダラー
const renderer = new THREE.WebGLRenderer({
canvas: canvas,
});
renderer.setSize(sizes.width, sizes.height);
renderer.setPixelRatio(window.devicePixelRatio);
// オブジェクトの作成
const boxGeometry = new THREE.BoxGeometry(5, 5, 5, 10);
const boxMaterial = new THREE.MeshNormalMaterial();
const box = new THREE.Mesh(boxGeometry, boxMaterial);
box.position.set(0, 0, -15);
box.rotation.set(1, 1, 0);
const torusGeometry = new THREE.TorusGeometry(8, 2, 16, 100);
const torusMaterial = new THREE.MeshNormalMaterial();
const torus = new THREE.Mesh(torusGeometry, torusMaterial);
torus.position.set(0, 1, 10);
scene.add(box, torus);
// 移動用の関数
function lerp(start, end, t) {
return start * (1 - t) + end * t;
}
function scalePercent(start, end) {
return (scrollPercent - start) / (end - start);
}
// スクロールアニメーション
const animationScripts = [];
animationScripts.push({
start: 0,
end: 40,
function() {
camera.lookAt(box.position);
camera.position.set(0, 1, 10);
box.position.z = lerp(-15, 2, scalePercent(0, 40));
torus.position.z = lerp(10, -20, scalePercent(0, 40));
},
});
animationScripts.push({
start: 40,
end: 60,
function() {
camera.lookAt(box.position);
camera.position.set(0, 1, 10);
box.rotation.z = lerp(1, Math.PI, scalePercent(40, 60));
},
});
animationScripts.push({
start: 60,
end: 80,
function() {
camera.lookAt(box.position);
camera.position.x = lerp(0, -15, scalePercent(60, 80));
camera.position.y = lerp(1, 15, scalePercent(60, 80));
camera.position.z = lerp(10, 25, scalePercent(60, 80));
},
});
animationScripts.push({
start: 80,
end: 101,
function() {
camera.lookAt(box.position);
box.rotation.x += 0.02;
box.rotation.y += 0.02;
},
});
// スクロールアニメーションを実行する関数
const playScrollAnimation = () => {
animationScripts.forEach((script) => {
if (scrollPercent >= script.start && scrollPercent <= script.end) {
script.function();
}
});
};
// ブラウザのスクロール率を取得
let scrollPercent = 0;
document.body.onscroll = () => {
const scroll = document.documentElement.scrollTop;
const height = document.documentElement.scrollHeight - document.documentElement.clientHeight;
scrollPercent = (scroll / height) * 100;
};
//アニメーション
const tick = () => {
window.requestAnimationFrame(tick);
playScrollAnimation();
renderer.render(scene, camera);
};
tick();
//ブラウザのリサイズ操作
window.addEventListener("resize", () => {
sizes.width = window.innerWidth;
sizes.height = window.innerHeight;
camera.aspect = sizes.width / sizes.height;
camera.updateProjectionMatrix();
renderer.setSize(sizes.width, sizes.height);
renderer.setPixelRatio(window.devicePixelRatio);
});