Skip to content
This repository was archived by the owner on May 29, 2025. It is now read-only.

Commit 081834c

Browse files
authored
Merge pull request #347 from nova1751/dev
perf: optimize the performance when the cursor is still
2 parents 31bbcc3 + 29062f2 commit 081834c

File tree

3 files changed

+19
-5
lines changed

3 files changed

+19
-5
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"dayjs": "^1.11.10",
2121
"element-plus": "^2.7.1",
2222
"fetch-jsonp": "^1.3.0",
23+
"lodash-es": "^4.17.21",
2324
"pinia": "^2.1.7",
2425
"pinia-plugin-persistedstate": "^3.2.1",
2526
"swiper": "^11.1.1",

pnpm-lock.yaml

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/utils/cursor.js

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
1+
import { isEqual } from "lodash-es";
2+
13
let mainCursor;
24

3-
Math.lerp = (a, b, n) => (1 - n) * a + n * b;
5+
const lerp = (a, b, n) => {
6+
if (Math.round(a) === b) {
7+
return b;
8+
}
9+
return (1 - n) * a + n * b;
10+
};
411

512
const getStyle = (el, attr) => {
613
try {
@@ -49,7 +56,6 @@ class Cursor {
4956
document.body.appendChild((this.scr = document.createElement("style")));
5057
this.scr.innerHTML = `* {cursor: url("data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 8 8' width='10px' height='10px'><circle cx='4' cy='4' r='4' fill='white' /></svg>") 4 4, auto !important}`;
5158
}
52-
5359
refresh() {
5460
this.scr.remove();
5561
this.cursor.classList.remove("active");
@@ -72,6 +78,7 @@ class Cursor {
7278
y: e.clientY - 8,
7379
};
7480
this.cursor.classList.remove("hidden");
81+
this.render();
7582
};
7683
document.onmouseenter = () => this.cursor.classList.remove("hidden");
7784
document.onmouseleave = () => this.cursor.classList.add("hidden");
@@ -81,13 +88,15 @@ class Cursor {
8188

8289
render() {
8390
if (this.pos.prev) {
84-
this.pos.prev.x = Math.lerp(this.pos.prev.x, this.pos.curr.x, 0.35);
85-
this.pos.prev.y = Math.lerp(this.pos.prev.y, this.pos.curr.y, 0.35);
91+
this.pos.prev.x = lerp(this.pos.prev.x, this.pos.curr.x, 0.35);
92+
this.pos.prev.y = lerp(this.pos.prev.y, this.pos.curr.y, 0.35);
8693
this.move(this.pos.prev.x, this.pos.prev.y);
8794
} else {
8895
this.pos.prev = this.pos.curr;
8996
}
90-
requestAnimationFrame(() => this.render());
97+
if (!isEqual(this.pos.curr, this.pos.prev)) {
98+
requestAnimationFrame(() => this.render());
99+
}
91100
}
92101
}
93102

0 commit comments

Comments
 (0)