-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCursor.svelte
199 lines (170 loc) · 3.89 KB
/
Cursor.svelte
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
<script lang="ts">
// Imports
import {Spring} from 'svelte/motion';
import type {TCursorOptions} from '../types/types';
import type {Snippet} from 'svelte';
// Props
type TCursorProps = {
children?: Snippet<[]>;
};
const {
children
}: TCursorProps = $props();
// Variables
const cursor_base = {size: 15, background: 'color-mix(in srgb, var(--text-color) 50%, transparent)'};
let scroll = $state<{
x: number;
y: number;
}>({x: 0, y: 0});
// Springs
const opacity = new Spring(0);
const coords = new Spring(
{
x: 0,
y: 0
},
{stiffness: 0.3, damping: 0.8}
);
const size = new Spring(cursor_base.size);
const blur = new Spring(0);
let background = $state(cursor_base.background as string);
let hasMoved = $state(false);
let innerSvg: string | null = $state(null);
let innerText: string | null = $state(null);
// Functions
export const setCursorParams = (params: TCursorOptions) => {
if (params.isHover) {
// params.svg can be undefined, a svg string or a boolean
if (params.svg) {
innerSvg = params.svg;
}
if (params.innerText) {
innerText = params.innerText;
}
opacity.target = params.opacity ?? 0.5;
} else {
if (params.svg === undefined || params.svg === null) {
innerSvg = null;
}
opacity.set(params.opacity ?? 1);
if (params.innerText === undefined || params.innerText === null) {
innerText = null;
}
}
blur.target = params.blur ?? 0;
background = params.backgroundColor ?? cursor_base.background;
size.target = params.scale ? cursor_base.size * params.scale : cursor_base.size;
};
// If hasMoved is false, then the cursor is not visible
$effect(() => {
if (!hasMoved) {
opacity.target = 0;
} else {
setTimeout(() => {
opacity.target = 1;
}, 150);
}
});
</script>
<!-- The window is used to get the mouse position -->
<svelte:window
onmousemove={(e) => {
!hasMoved && (hasMoved = true);
coords.target = {
x: e.clientX,
y: e.clientY
};
scroll = {
x: window.scrollX,
y: window.scrollY
};
}}
onmousedown={() => {
size.target = size.current * 1.5;
}}
onmouseup={() => {
size.target = size.current / 1.5;
}}
onscroll={() => {
scroll = {
x: window.scrollX,
y: window.scrollY
};
}}
/>
<!-- The svg is always displayed -->
<svg aria-hidden="true">
<circle
cx={coords.current.x}
cy={coords.current.y}
r={size.current}
style="
opacity: {opacity.current};
filter: blur({blur.current}px);
fill: {background};
"
/>
</svg>
<!-- If there is a svg, then we display it -->
{#if innerSvg}
<img
src={innerSvg}
alt="Github gif"
style="
height: {$size * 2}px;
width: {$size * 2}px;
transform: translate({$coords.x + scroll.x - $size}px, {$coords.y + scroll.y - $size}px);
"
/>
{/if}
<!-- If something is in the default slot, then we display it -->
{#if children || innerText}
<div
class="html-container"
style="
height: {$size * 2}px;
width: {$size * 2}px;
transform: translate({$coords.x - $size}px, {$coords.y - $size}px) scale({$size /
cursor_base.size});
pointer-events: none;
position: fixed;
"
>
{#if children}
{@render children()}
{:else}
{innerText}
{/if}
</div>
{/if}
<style lang="scss">
* {
pointer-events: none;
}
svg {
position: fixed;
top: 0;
left: 0;
pointer-events: none;
width: 100%;
height: 100%;
z-index: 99998;
}
img {
position: absolute;
top: 0;
left: 0;
pointer-events: none;
z-index: 99999;
}
.html-container {
position: absolute;
top: 0;
left: 0;
display: flex;
justify-content: center;
align-items: center;
z-index: 99998;
pointer-events: none;
}
</style>