Skip to content

Commit

Permalink
feat: optimized login background
Browse files Browse the repository at this point in the history
  • Loading branch information
tikazyq committed Jun 25, 2024
1 parent 771f1e2 commit 138655d
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 12 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "crawlab-ui",
"version": "0.6.3-13",
"version": "0.6.3-14",
"private": false,
"author": {
"name": "Marvin Zhang",
Expand Down
44 changes: 38 additions & 6 deletions public/js/login-canvas-three.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
class ThreeJSApp {
constructor() {
this.frameId = null;
this.fps = 60;
this.fpsInterval = 1000 / this.fps;
this.then = Date.now();
this.cameraRange = 3;
this.scene = new THREE.Scene();
this.renderer = new THREE.WebGLRenderer({ antialias: true });
Expand Down Expand Up @@ -208,7 +212,7 @@ class ThreeJSApp {

animate() {
const time = performance.now() * 0.0003;
requestAnimationFrame(() => this.animate());
this.frameId = requestAnimationFrame(() => this.animate());

for (const object of this.particularGroup.children) {
object.rotation.x += object.speedValue / 10;
Expand All @@ -233,17 +237,45 @@ class ThreeJSApp {
this.modularGroup.rotation.x -=
(-this.mouse.y * 4 + this.modularGroup.rotation.x) * this.uSpeed * ratio;
this.camera.lookAt(this.scene.position);
this.renderer.render(this.scene, this.camera);

const now = Date.now();
const elapsed = now - this.then;
if (elapsed > this.fpsInterval) {
this.then = now - (elapsed % this.fpsInterval);
this.renderer.render(this.scene, this.camera);
}
}

stopAnimation() {
if (this.frameId) {
cancelAnimationFrame(this.frameId); // 使用frameId来停止动画
this.frameId = null; // 清空frameId
}
}

dispose() {
this.stopAnimation(); // 停止动画
this.scene.traverse(object => {
if (object.material) {
object.material.dispose();
}
if (object.geometry) {
object.geometry.dispose();
}
});
this.renderer.dispose();
document
.getElementById('login-canvas')
.removeChild(this.renderer.domElement);
}
}

window.initCanvas = function () {
window.threeJSApp = new ThreeJSApp();
};
window.resetCanvas = function () {
window.threeJSApp = null;
const el = document.querySelector('#login-canvas');
if (el) {
el.innerHTML = '';
if (window.threeJSApp) {
window.threeJSApp.dispose();
window.threeJSApp = null;
}
};
2 changes: 1 addition & 1 deletion public/js/login-canvas.js
Original file line number Diff line number Diff line change
Expand Up @@ -288,5 +288,5 @@ function initCanvas() {

init();

window.resetCanvas = reset;
window.disposeCanvas = reset;
}
2 changes: 1 addition & 1 deletion src/interfaces/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ declare global {
VUE_APP_INIT_BAIDU_TONGJI?: string;
threeJSApp?: any;
initCanvas?: () => void;
resetCanvas?: () => void;
disposeCanvas?: () => void;
_hmt?: Array;
'vue3-sfc-loader'?: { loadModule };
aplus_queue: { action: string; arguments: any[] }[];
Expand Down
6 changes: 3 additions & 3 deletions src/views/login/Login.vue
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,9 @@ onMounted(() => {
}
});
onUnmounted(() => {
// reset canvas
if (window.resetCanvas) {
window.resetCanvas();
// dispose canvas
if (window.disposeCanvas) {
window.disposeCanvas();
}
});
</script>
Expand Down

0 comments on commit 138655d

Please sign in to comment.