2023-12-05 03:17:38 +03:00
|
|
|
import * as THREE from "three";
|
2023-12-01 12:57:41 +03:00
|
|
|
|
|
|
|
let isMousePressed = false
|
|
|
|
|
2023-12-05 03:17:38 +03:00
|
|
|
export class SpiralCamera {
|
|
|
|
constructor(canvas, segmentHeight) {
|
|
|
|
// this.camera = new THREE.PerspectiveCamera(75, canvas.clientWidth / canvas.clientHeight, 0.1, 1000);
|
|
|
|
this.camera = new THREE.PerspectiveCamera(75, canvas.clientWidth / canvas.clientHeight, 0.1, 1000);
|
2023-12-07 13:00:47 +03:00
|
|
|
this.angle = 90;
|
2024-01-24 20:03:41 +03:00
|
|
|
this.distance = 10;
|
2023-12-05 03:17:38 +03:00
|
|
|
this.canvas = canvas
|
|
|
|
this.offset = 0;
|
|
|
|
this.segmentHeight = segmentHeight;
|
|
|
|
this.viewingYear = new Date().getFullYear();
|
2023-12-01 12:57:41 +03:00
|
|
|
|
2023-12-05 03:17:38 +03:00
|
|
|
console.log(this.canvas)
|
2023-12-01 12:57:41 +03:00
|
|
|
|
2023-12-05 03:17:38 +03:00
|
|
|
this.camera.position.z = this.distance;
|
2023-12-01 12:57:41 +03:00
|
|
|
|
2023-12-05 03:17:38 +03:00
|
|
|
addEventListener('mousedown', (event) => {
|
|
|
|
if (event.button == 0) {
|
|
|
|
isMousePressed = true;
|
|
|
|
}
|
|
|
|
});
|
2023-12-01 12:57:41 +03:00
|
|
|
|
2023-12-05 03:17:38 +03:00
|
|
|
addEventListener('mouseup', (event) => {
|
|
|
|
if (event.button == 0) {
|
|
|
|
isMousePressed = false;
|
|
|
|
}
|
|
|
|
});
|
2023-12-01 12:57:41 +03:00
|
|
|
|
2023-12-05 03:17:38 +03:00
|
|
|
addEventListener('mousemove', (event) => {
|
|
|
|
if (isMousePressed) {
|
|
|
|
let horizontal = event.movementX;
|
|
|
|
let vertical = event.movementY;
|
2024-01-24 20:03:41 +03:00
|
|
|
Math.abs(horizontal) > Math.abs(vertical)? vertical = 0 : horizontal = 0;
|
2023-12-05 03:17:38 +03:00
|
|
|
this.updateCameraPosition(horizontal, vertical);
|
|
|
|
}
|
|
|
|
});
|
2023-12-01 12:57:41 +03:00
|
|
|
|
2023-12-05 03:17:38 +03:00
|
|
|
addEventListener('wheel', (event) => {
|
|
|
|
this.distance += this.distance / (event.deltaY / 60);
|
|
|
|
this.updateCameraPosition(0, 0);
|
|
|
|
});
|
|
|
|
|
|
|
|
this.changedViewingYear = new CustomEvent("changedViewYear", {});
|
2023-12-01 12:57:41 +03:00
|
|
|
}
|
2023-12-05 03:17:38 +03:00
|
|
|
|
|
|
|
updateCameraPosition = (horizontal, vertical) => {
|
|
|
|
let ratio = 4 * Math.PI / this.canvas.width;
|
|
|
|
|
|
|
|
let alpha = (360 * (horizontal * ratio / (2 * Math.PI)));
|
|
|
|
let betta = THREE.MathUtils.degToRad(this.angle);
|
|
|
|
|
|
|
|
let z = Math.sin(betta) * this.distance;
|
|
|
|
let x = Math.cos(betta) * this.distance;
|
|
|
|
|
|
|
|
|
|
|
|
this.offset -= vertical * ratio
|
|
|
|
if (this.offset > this.segmentHeight) {
|
|
|
|
this.offset = 0;
|
|
|
|
this.viewingYear--;
|
|
|
|
dispatchEvent(this.changedViewingYear)
|
|
|
|
}
|
|
|
|
if (this.offset < 0) {
|
|
|
|
this.offset = this.segmentHeight;
|
|
|
|
this.viewingYear++;
|
|
|
|
dispatchEvent(this.changedViewingYear)
|
|
|
|
}
|
|
|
|
|
|
|
|
this.camera.position.x = x;
|
|
|
|
this.camera.position.z = z;
|
|
|
|
|
|
|
|
this.angle += alpha;
|
|
|
|
};
|
|
|
|
|
|
|
|
getCamera = () => {
|
|
|
|
return this.camera;
|
|
|
|
};
|
|
|
|
|
|
|
|
getOffset = () => {
|
|
|
|
return this.offset;
|
|
|
|
};
|
|
|
|
|
|
|
|
getViewingYear = () => {
|
|
|
|
return this.viewingYear;
|
|
|
|
};
|
|
|
|
|
2023-12-07 12:56:12 +03:00
|
|
|
getAngle = () => {
|
|
|
|
return this.angle;
|
|
|
|
}
|
|
|
|
|
2023-12-05 03:17:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|