anime-cursor 1.0.0 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +170 -170
- package/dist/anime-cursor.esm.js +355 -456
- package/dist/anime-cursor.umd.js +363 -464
- package/dist/anime-cursor.umd.min.js +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,14 +1,18 @@
|
|
|
1
|
-
# AnimeCursor
|
|
1
|
+
# AnimeCursor v2
|
|
2
2
|
|
|
3
3
|
<div align="center">
|
|
4
4
|
<img src="https://cdn.jsdelivr.net/gh/shuninyu/anime-cursor@main/title.gif" width="384px" alt="AnimeCursor"/>
|
|
5
5
|
</div>
|
|
6
6
|
|
|
7
|
+
|
|
8
|
+
|
|
7
9
|
[[简体中文]](#animecursorsc)
|
|
10
|
+
|
|
8
11
|
## [Visit the official website](https://shuninyu.github.io/anime-cursor/) for more informations
|
|
12
|
+
|
|
9
13
|
## [Read documents](https://shuninyu.github.io/anime-cursor/docs) to get started with AnimeCursor
|
|
10
14
|
|
|
11
|
-
AnimeCursor is a lightweight JavaScript library for frame
|
|
15
|
+
AnimeCursor is a lightweight JavaScript library for frame-by-frame animated custom cursors.
|
|
12
16
|
|
|
13
17
|
AnimeCursor has no dependencies on any frameworks, making it suitable for personal websites, creative portfolios, and experimental UI projects.
|
|
14
18
|
|
|
@@ -16,19 +20,20 @@ AnimeCursor has no dependencies on any frameworks, making it suitable for person
|
|
|
16
20
|
|
|
17
21
|
## ✨ Features
|
|
18
22
|
|
|
19
|
-
*
|
|
20
|
-
* Supports
|
|
21
|
-
*
|
|
22
|
-
*
|
|
23
|
-
*
|
|
24
|
-
*
|
|
23
|
+
* **Native CSS Cursor** – Uses the browser's native cursor, no simulated elements, high performance and accuracy.
|
|
24
|
+
* **Independent Frame Images** – Supports multi-frame animations using separate image files (PNG, SVG, etc.).
|
|
25
|
+
* **Smart Frame Detection** – Automatically detects numeric suffixes in image filenames (e.g., `cursor_01.png`, `cursor (02).png`, `cursor[3].png`). Just provide the first frame, and AnimeCursor will generate all frames.
|
|
26
|
+
* **Variable Speed Animation** – Use arrays for `frames` and `duration` to create complex timing patterns (e.g., `frames: [2, 3], duration: [0.2, 1]` means 2 frames in 0.2s, then 3 frames in 1s).
|
|
27
|
+
* **Static Cursor Support** – If `frames` or `duration` is missing or invalid, the cursor is treated as static (single image).
|
|
28
|
+
* **Automatic Cursor Switching** – Define cursor styles for specific HTML tags or via `data-cursor` attributes.
|
|
29
|
+
* **Lightweight & Zero Dependencies** – Built with vanilla JavaScript, no external libraries required.
|
|
25
30
|
|
|
26
31
|
## 📦 Installation
|
|
27
32
|
|
|
28
33
|
### CDN
|
|
29
34
|
|
|
30
35
|
```html
|
|
31
|
-
<script src="https://cdn.jsdelivr.net/npm/anime-cursor/dist/anime-cursor.umd.min.js"></script>
|
|
36
|
+
<script src="https://cdn.jsdelivr.net/npm/anime-cursor@v2.0.0/dist/anime-cursor.umd.min.js"></script>
|
|
32
37
|
```
|
|
33
38
|
|
|
34
39
|
### npm
|
|
@@ -36,6 +41,7 @@ AnimeCursor has no dependencies on any frameworks, making it suitable for person
|
|
|
36
41
|
```bash
|
|
37
42
|
npm i anime-cursor
|
|
38
43
|
```
|
|
44
|
+
|
|
39
45
|
```js
|
|
40
46
|
import AnimeCursor from 'anime-cursor';
|
|
41
47
|
new AnimeCursor({...});
|
|
@@ -51,130 +57,128 @@ new AnimeCursor({...});
|
|
|
51
57
|
|
|
52
58
|
Here is an example of how to use AnimeCursor:
|
|
53
59
|
|
|
54
|
-
|
|
60
|
+
**IMPORTANT**
|
|
61
|
+
|
|
55
62
|
- Ensure the initialization code is placed **within** the `<body>` tag of your HTML document.
|
|
56
|
-
- For optimal performance, it is recommended to initialize AnimeCursor **before
|
|
63
|
+
- For optimal performance, it is recommended to initialize AnimeCursor **before** the DOM has fully loaded, as certain features require execution prior to the completion of DOM loading.
|
|
57
64
|
|
|
58
65
|
```javascript
|
|
59
66
|
new AnimeCursor({
|
|
67
|
+
debug: true, // optional, shows debug overlay
|
|
68
|
+
enableTouch: false, // optional, enable on touch devices
|
|
69
|
+
fallbackCursor: 'auto', // optional, fallback cursor type
|
|
70
|
+
excludeSelectors: 'input, textarea, [contenteditable]', // optional, restore native text cursor
|
|
60
71
|
cursors: {
|
|
61
|
-
// each type
|
|
72
|
+
// each cursor type must have an image and frames/duration
|
|
62
73
|
idle: {
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
default: true // set this cursor as default cursor
|
|
74
|
+
image: 'https://example.com/cursor_default.png', // static cursor (no animation)
|
|
75
|
+
default: true // set as default cursor
|
|
66
76
|
},
|
|
67
|
-
//
|
|
77
|
+
// variable speed animation with independent frames
|
|
68
78
|
pointer: {
|
|
69
|
-
tags: ['a', 'button'], //
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
pingpong: true
|
|
75
|
-
offset: [10, 4] // if the pointing spot is not at the top left of the image, set offset
|
|
79
|
+
tags: ['a', 'button'], // apply to these HTML tags
|
|
80
|
+
image: 'https://example.com/pointer_001.png', // AnimeCursor will automatically find pointer_002.png, pointer_003.png, etc.
|
|
81
|
+
frames: 4, // total frames (uniform animation)
|
|
82
|
+
duration: 0.4, // total duration in seconds
|
|
83
|
+
offset: [15, 25], // hotspot offset (x, y) for url()
|
|
84
|
+
pingpong: true // enable alternate loop
|
|
76
85
|
},
|
|
77
|
-
//
|
|
86
|
+
// array-based variable speed animation
|
|
87
|
+
custom: {
|
|
88
|
+
image: 'https://example.com/custom_01.png',
|
|
89
|
+
frames: [2, 3, 1], // first 2 frames, then 3 frames, then 1 frame
|
|
90
|
+
duration: [0.2, 1.0, 0.5], // each segment duration
|
|
91
|
+
offset: [8, 8],
|
|
92
|
+
pingpong: false
|
|
93
|
+
},
|
|
94
|
+
// static cursor (no frames or duration)
|
|
78
95
|
text: {
|
|
79
96
|
tags: ['p', 'h1', 'h2', 'span'],
|
|
80
|
-
|
|
81
|
-
image: 'https://example.com/cursor_text.gif'
|
|
82
|
-
},
|
|
83
|
-
haha: {
|
|
84
|
-
size: [32,32],
|
|
85
|
-
image: 'https://example.com/cursor_haha.png',
|
|
86
|
-
frames: 12,
|
|
87
|
-
duration: 1,
|
|
88
|
-
pixel: true, // if the image is origin size pixel art, set pixel to true
|
|
89
|
-
scale: [2,2] // scale the cursor
|
|
97
|
+
image: 'https://example.com/cursor_text.png'
|
|
90
98
|
}
|
|
91
99
|
}
|
|
92
100
|
});
|
|
93
101
|
```
|
|
94
|
-
|
|
102
|
+
|
|
103
|
+
For non-default cursors, if you need a specific element to trigger the cursor, manually add the `data-cursor` attribute to the element. For example: `<div class="custom-div" data-cursor="pointer"></div>`.
|
|
95
104
|
|
|
96
105
|
## ⚙️ Configuration Options
|
|
97
106
|
|
|
98
107
|
### `cursors` (Required)
|
|
99
108
|
|
|
100
|
-
An object
|
|
101
|
-
|
|
102
|
-
Each key represents a cursor type (the name can be freely defined).
|
|
109
|
+
An object that defines all cursor types. Each key is a cursor name (any string).
|
|
103
110
|
|
|
104
111
|
#### Cursor Configuration Parameters
|
|
105
112
|
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
113
|
+
| Parameter | Type | Required | Description |
|
|
114
|
+
| ---------- | ------------------ | -------- | ------------------------------------------------------------ |
|
|
115
|
+
| `image` | string | **Yes** | URL of the first frame image. AnimeCursor will automatically detect numeric suffixes to generate subsequent frames (e.g., `image_001.png` → `image_002.png`, `image_003.png`). If you want a single static image, just provide one image. |
|
|
116
|
+
| `frames` | number \| number[] | **Yes** | Number of frames (for uniform animation) or an array of segment lengths (for variable speed). Total frames = sum of array elements. If not provided, the cursor is static. |
|
|
117
|
+
| `duration` | number \| number[] | **Yes** | Total duration in seconds (for uniform) or array of segment durations (must match `frames` array length). |
|
|
118
|
+
| `tags` | string[] | No | HTML tag names (e.g., `['a', 'button']`) that should use this cursor. If omitted, the cursor can only be applied via `data-cursor` attribute. |
|
|
119
|
+
| `offset` | [number, number] | No | Hotspot offset (x, y) in pixels. Default: `[0, 0]`. The offset defines where the actual click point is relative to the top-left of the image. |
|
|
120
|
+
| `pingpong` | boolean | No | If `true`, the animation plays forward then backward alternately. Default: `false`. |
|
|
121
|
+
| `fallback` | string | No | Fallback cursor type (e.g., `auto`, `pointer`). Default: value of `fallbackCursor` in root options. |
|
|
122
|
+
| `default` | boolean | No | Set this cursor as the default cursor (used when no other cursor applies). Only one cursor can be default. |
|
|
123
|
+
|
|
124
|
+
### Root Options
|
|
125
|
+
|
|
126
|
+
| Option | Type | Default | Description |
|
|
127
|
+
| ------------------ | ------- | -------------------------------------- | ------------------------------------------------------------ |
|
|
128
|
+
| `debug` | boolean | `false` | Enables a debug overlay showing current cursor type and coordinates. |
|
|
129
|
+
| `enableTouch` | boolean | `false` | Allow animated cursors on touch devices (detected automatically, disabled by default). |
|
|
130
|
+
| `fallbackCursor` | string | `'auto'` | Global fallback cursor for all animated cursors (e.g., `'auto'`, `'pointer'`). |
|
|
131
|
+
| `excludeSelectors` | string | `'input, textarea, [contenteditable]'` | CSS selectors that should always use the native text cursor. |
|
|
122
132
|
|
|
123
133
|
## 📝 Notes
|
|
124
134
|
|
|
125
|
-
###
|
|
126
|
-
|
|
127
|
-
* **For any sprite animation cursor, its sprite sheet should be arranged in a single horizontal row.** AnimeCursor will automatically generate the PNG sprite animation.
|
|
128
|
-
For example, if you set the `size` (width, height) for a `pointer` cursor to `[64px , 64px]` and `frames` to `3`, the prepared sprite sheet dimensions (width, height) should be: `[192px , 64px]`.
|
|
129
|
-
|
|
130
|
-
* For pixel art with a large number of frames, you can use the original image (whether GIF or sprite-sheet) to save storage space or bandwidth. Then, use the `scale` parameter in the configuration to resize the cursor and set `pixel` to `true` to avoid blurry scaling.
|
|
131
|
-
|
|
132
|
-
### 🧩 Tagging Mechanism
|
|
133
|
-
|
|
134
|
-
AnimeCursor automatically adds `data-cursor` attributes to page elements based on the configuration:
|
|
135
|
+
### 🖼️ Independent Frame Images
|
|
135
136
|
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
* The element already had a `data-cursor` attribute *before* AnimeCursor was initialized.
|
|
145
|
-
|
|
146
|
-
Therefore, to assign a specific animated cursor to a particular element, simply add the corresponding `data-cursor` attribute to that element. AnimeCursor will not overwrite pre-existing `data-cursor` tags.
|
|
137
|
+
- AnimeCursor expects the first frame file to contain a numeric suffix (e.g., `cursor_001.png`). It will automatically generate URLs for subsequent frames by incrementing the number while preserving formatting (leading zeros, parentheses, brackets).
|
|
138
|
+
- Supported patterns:
|
|
139
|
+
- `cursor_01.png` → `cursor_02.png`, `cursor_03.png`, …
|
|
140
|
+
- `cursor(01).png` → `cursor(02).png`, `cursor(03).png`, …
|
|
141
|
+
- `cursor[1].png` → `cursor[2].png`, `cursor[3].png`, …
|
|
142
|
+
- `cursor_1.png` → `cursor_2.png`, `cursor_3.png`, … (no padding)
|
|
143
|
+
- If no number is found, the library will append `_%d` (e.g., `cursor.png` → `cursor_1.png`, `cursor_2.png`).
|
|
144
|
+
- **Important**: All frames must be the same size. The actual cursor size is the natural size of the images; scale them beforehand if needed.
|
|
147
145
|
|
|
148
146
|
### 🎞️ Animation Rules
|
|
149
147
|
|
|
150
|
-
|
|
148
|
+
- **Uniform Animation**: `frames` and `duration` are both numbers. The animation cycles through all frames evenly over the total duration.
|
|
149
|
+
- **Variable Speed Animation**: `frames` and `duration` are arrays of equal length. Each segment defines a number of frames and the time to play them. The frames are evenly distributed within each segment.
|
|
150
|
+
- **Static Cursor**: If `frames` or `duration` is missing, or if they are invalid (e.g., non-positive numbers, mismatched array lengths), the cursor is treated as static (only the first image is used). A warning will be logged in the console.
|
|
151
151
|
|
|
152
|
-
|
|
152
|
+
### 🧩 Tagging Mechanism
|
|
153
153
|
|
|
154
|
-
|
|
155
|
-
* `duration` is set
|
|
154
|
+
AnimeCursor automatically adds the appropriate cursor style to elements based on `tags` and `data-cursor` attributes:
|
|
156
155
|
|
|
157
|
-
If
|
|
156
|
+
- If an element's tag name matches a `tags` list, the corresponding cursor will be applied.
|
|
157
|
+
- You can also manually set `data-cursor="cursorName"` on any element to force a specific cursor.
|
|
158
|
+
- The default cursor is used when no other rule matches.
|
|
158
159
|
|
|
159
|
-
|
|
160
|
+
### 🔧 Performance
|
|
160
161
|
|
|
161
|
-
|
|
162
|
-
* Animation is controlled by the GIF file itself
|
|
162
|
+
Because v2 uses native CSS `cursor` property and CSS animations, there is no JavaScript `mousemove` listener (except when `debug` mode is enabled). This yields optimal performance and smooth cursor movement.
|
|
163
163
|
|
|
164
164
|
## ❌ Error Handling
|
|
165
165
|
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
166
|
+
- Missing required configuration will throw an error and stop initialization.
|
|
167
|
+
- Invalid optional configuration (e.g., mismatched array lengths) will log a warning and treat the cursor as static.
|
|
168
|
+
- All errors and warnings are prefixed with `[AnimeCursor]`.
|
|
169
169
|
|
|
170
170
|
---
|
|
171
|
-
|
|
171
|
+
|
|
172
|
+
# AnimeCursor v2
|
|
172
173
|
|
|
173
174
|
<div align="center">
|
|
174
175
|
<img src="https://cdn.jsdelivr.net/gh/shuninyu/anime-cursor@main/title.gif" width="384px" alt="AnimeCursor"/>
|
|
175
176
|
</div>
|
|
176
177
|
|
|
178
|
+
|
|
179
|
+
|
|
177
180
|
## [访问官网](https://shuninyu.github.io/anime-cursor/)以获取更多信息
|
|
181
|
+
|
|
178
182
|
## [阅读文档](https://shuninyu.github.io/anime-cursor/docs/zh)快速上手 AnimeCursor
|
|
179
183
|
|
|
180
184
|
AnimeCursor 是一个轻量级自定义逐帧动画光标 JavaScript 库。
|
|
@@ -185,19 +189,20 @@ AnimeCursor 无需依赖任何框架,适合个人网站、创意作品集以
|
|
|
185
189
|
|
|
186
190
|
## ✨ 特性
|
|
187
191
|
|
|
188
|
-
*
|
|
189
|
-
*
|
|
190
|
-
*
|
|
191
|
-
*
|
|
192
|
-
*
|
|
193
|
-
*
|
|
192
|
+
* **原生 CSS 光标** – 使用浏览器原生光标,无需模拟元素,性能更高,定位更精准。
|
|
193
|
+
* **独立帧图片支持** – 支持使用多张独立图片(PNG、SVG 等)制作逐帧动画。
|
|
194
|
+
* **智能帧识别** – 自动识别文件名中的数字序号(如 `cursor_01.png`、`cursor (02).png`、`cursor[3].png`),只需提供第一帧,库会自动生成后续帧。
|
|
195
|
+
* **变速动画** – `frames` 和 `duration` 可设置为数组,实现复杂的时间节奏(例如 `frames: [2, 3], duration: [0.2, 1]` 表示前 2 帧 0.2 秒,后 3 帧 1 秒)。
|
|
196
|
+
* **静态光标** – 如果不设置 `frames` 或 `duration`,或设置无效,则视为静态光标(仅使用第一帧)。
|
|
197
|
+
* **自动光标切换** – 通过 HTML 标签或 `data-cursor` 属性自动切换光标样式。
|
|
198
|
+
* **轻量无依赖** – 原生 JavaScript 编写,不依赖任何第三方库。
|
|
194
199
|
|
|
195
200
|
## 📦 部署方法
|
|
196
201
|
|
|
197
202
|
### CDN
|
|
198
203
|
|
|
199
204
|
```html
|
|
200
|
-
<script src="https://cdn.jsdelivr.net/npm/anime-cursor/dist/anime-cursor.umd.min.js"></script>
|
|
205
|
+
<script src="https://cdn.jsdelivr.net/npm/anime-cursor@v2.0.0/dist/anime-cursor.umd.min.js"></script>
|
|
201
206
|
```
|
|
202
207
|
|
|
203
208
|
### npm
|
|
@@ -205,6 +210,7 @@ AnimeCursor 无需依赖任何框架,适合个人网站、创意作品集以
|
|
|
205
210
|
```bash
|
|
206
211
|
npm i anime-cursor
|
|
207
212
|
```
|
|
213
|
+
|
|
208
214
|
```js
|
|
209
215
|
import AnimeCursor from 'anime-cursor';
|
|
210
216
|
new AnimeCursor({...});
|
|
@@ -221,117 +227,111 @@ new AnimeCursor({...});
|
|
|
221
227
|
下面是一个 AnimeCursor 使用示例:
|
|
222
228
|
|
|
223
229
|
**重要提示**
|
|
230
|
+
|
|
224
231
|
- 请务必将初始化代码置于HTML文档的 **`<body>`** 标签内部。
|
|
225
232
|
- 为获得最佳性能,建议在DOM完全加载**之前**初始化AnimeCursor,因其部分功能需在DOM加载完成前执行。
|
|
226
233
|
|
|
227
234
|
```javascript
|
|
228
235
|
new AnimeCursor({
|
|
236
|
+
debug: true, // 可选,显示调试浮层
|
|
237
|
+
enableTouch: false, // 可选,在触屏设备上启用
|
|
238
|
+
fallbackCursor: 'auto', // 可选,备用光标类型
|
|
239
|
+
excludeSelectors: 'input, textarea, [contenteditable]', // 可选,恢复原生文本光标
|
|
229
240
|
cursors: {
|
|
230
|
-
//
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
default: true // 将此光标设为默认光标
|
|
241
|
+
// 每种光标必须有 image 和 frames/duration
|
|
242
|
+
idle: {
|
|
243
|
+
image: 'https://example.com/cursor_default.png', // 静态光标(无动画)
|
|
244
|
+
default: true // 设为默认光标
|
|
235
245
|
},
|
|
236
|
-
//
|
|
246
|
+
// 变速动画 + 独立帧图片
|
|
237
247
|
pointer: {
|
|
238
|
-
tags: ['a', 'button'], //
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
pingpong: true
|
|
244
|
-
|
|
248
|
+
tags: ['a', 'button'], // 应用到这些标签
|
|
249
|
+
image: 'https://example.com/pointer_001.png', // 自动识别后续帧
|
|
250
|
+
frames: 4, // 总帧数(匀速动画)
|
|
251
|
+
duration: 0.4, // 总时长(秒)
|
|
252
|
+
offset: [15, 25], // 热点偏移 (x, y)
|
|
253
|
+
pingpong: true // 开启乒乓循环
|
|
254
|
+
},
|
|
255
|
+
// 数组形式的变速动画
|
|
256
|
+
custom: {
|
|
257
|
+
image: 'https://example.com/custom_01.png',
|
|
258
|
+
frames: [2, 3, 1], // 前2帧,接着3帧,最后1帧
|
|
259
|
+
duration: [0.2, 1.0, 0.5], // 对应时长
|
|
260
|
+
offset: [8, 8],
|
|
261
|
+
pingpong: false
|
|
245
262
|
},
|
|
246
|
-
//
|
|
263
|
+
// 静态光标(无 frames/duration)
|
|
247
264
|
text: {
|
|
248
265
|
tags: ['p', 'h1', 'h2', 'span'],
|
|
249
|
-
|
|
250
|
-
image: 'https://example.com/cursor_text.gif'
|
|
251
|
-
},
|
|
252
|
-
haha: {
|
|
253
|
-
size: [32,32],
|
|
254
|
-
image: 'https://example.com/cursor_haha.png',
|
|
255
|
-
frames: 12,
|
|
256
|
-
duration: 1,
|
|
257
|
-
pixel: true, // 如果是原尺寸像素图,启用像素化渲染
|
|
258
|
-
scale: [2,2] // 缩放光标
|
|
266
|
+
image: 'https://example.com/cursor_text.png'
|
|
259
267
|
}
|
|
260
268
|
}
|
|
261
269
|
});
|
|
262
270
|
```
|
|
263
|
-
|
|
271
|
+
|
|
272
|
+
对于非默认光标,如果需要某元素触发该光标,请手动为该元素添加 `data-cursor`。例如:`<div class="custom-div" data-cursor="pointer"></div>`。
|
|
264
273
|
|
|
265
274
|
## ⚙️ 配置项说明
|
|
266
275
|
|
|
267
276
|
### `cursors`(必填)
|
|
268
277
|
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
每一个 key 代表一种光标类型(名称可以自由定义)。
|
|
278
|
+
用于定义所有光标类型的对象。每一个 key 代表一种光标类型(名称可以自由定义)。
|
|
272
279
|
|
|
273
280
|
#### 光标配置参数
|
|
274
281
|
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
282
|
+
| 参数 | 类型 | 必填 | 描述 |
|
|
283
|
+
| ---------- | ------------------ | ------ | ------------------------------------------------------------ |
|
|
284
|
+
| `image` | string | **是** | 第一帧图片的 URL。库会自动识别数字后缀生成后续帧(如 `image_001.png` → `image_002.png`、`image_003.png`)。如果只需要静态光标,则只需提供一张图片。 |
|
|
285
|
+
| `frames` | number \| number[] | **是** | 帧数(匀速动画时为一个数字)或帧段数组(变速动画)。总帧数 = 数组元素之和。若不设置,则视为静态光标。 |
|
|
286
|
+
| `duration` | number \| number[] | **是** | 总时长(秒)(匀速动画)或时长数组(必须与 `frames` 数组长度一致)。 |
|
|
287
|
+
| `tags` | string[] | 否 | HTML 标签名数组(如 `['a', 'button']`),应用该光标的标签。若不提供,则只能通过 `data-cursor` 属性应用。 |
|
|
288
|
+
| `offset` | [number, number] | 否 | 热点偏移 (x, y),单位像素。默认为 `[0, 0]`。偏移定义了相对于图片左上角的实际点击位置。 |
|
|
289
|
+
| `pingpong` | boolean | 否 | 若为 `true`,动画会正向播放再反向播放,循环交替。默认为 `false`。 |
|
|
290
|
+
| `fallback` | string | 否 | 备用光标类型(如 `auto`、`pointer`)。默认使用根选项中的 `fallbackCursor`。 |
|
|
291
|
+
| `default` | boolean | 否 | 将此光标设为默认光标(当没有其他光标匹配时使用)。只能有一个光标设为 `true`。 |
|
|
292
|
+
|
|
293
|
+
### 根选项
|
|
294
|
+
|
|
295
|
+
| 选项 | 类型 | 默认值 | 描述 |
|
|
296
|
+
| ------------------ | ------- | -------------------------------------- | ------------------------------------------------------------ |
|
|
297
|
+
| `debug` | boolean | `false` | 启用调试浮层,显示当前光标类型和坐标。 |
|
|
298
|
+
| `enableTouch` | boolean | `false` | 允许在触屏设备上显示动画光标(默认自动检测并禁用)。 |
|
|
299
|
+
| `fallbackCursor` | string | `'auto'` | 全局备用光标类型,用于所有动画光标(如 `'auto'`、`'pointer'`)。 |
|
|
300
|
+
| `excludeSelectors` | string | `'input, textarea, [contenteditable]'` | 始终使用原生文本光标的 CSS 选择器。 |
|
|
291
301
|
|
|
292
302
|
## 📝 注意事项
|
|
293
303
|
|
|
294
|
-
###
|
|
295
|
-
|
|
296
|
-
* **对于任何精灵图动画光标,它的精灵图表都应该为单行横向布局,** AnimeCursor 会自动生成 PNG 精灵图动画。
|
|
297
|
-
例如,你为 `pointer` 光标设置的`size`(长,宽)为 `[64px , 64px]` ,帧数为 `3` ,那么你准备的精灵图表尺寸(长,宽)应该为: `[192px , 64px]` 。
|
|
298
|
-
|
|
299
|
-
* 对于帧数特别多的像素图,你可以使用原尺寸图片(无论是gif还是精灵图表)以节省存储空间或带宽,并在参数中设置 `scale` 来缩放光标,并将 `pixel` 设置为 `true` 来避免缩放模糊。
|
|
300
|
-
|
|
301
|
-
### 🧩 标记机制
|
|
302
|
-
|
|
303
|
-
AnimeCursor 会根据配置自动为页面元素添加 `data-cursor`:
|
|
304
|
-
|
|
305
|
-
```html
|
|
306
|
-
<!-- 以上面的示例用法配置为例 -->
|
|
307
|
-
<button data-cursor="pointer"></button>
|
|
308
|
-
```
|
|
304
|
+
### 🖼️ 独立帧图片
|
|
309
305
|
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
306
|
+
- AnimeCursor 期望第一帧图片的文件名包含数字序号(如 `cursor_001.png`)。它会自动递增该数字并保留格式(前导零、括号、方括号等)来生成后续帧的 URL。
|
|
307
|
+
- 支持的格式示例:
|
|
308
|
+
- `cursor_01.png` → `cursor_02.png`、`cursor_03.png`……
|
|
309
|
+
- `cursor(01).png` → `cursor(02).png`、`cursor(03).png`……
|
|
310
|
+
- `cursor[1].png` → `cursor[2].png`、`cursor[3].png`……
|
|
311
|
+
- `cursor_1.png` → `cursor_2.png`、`cursor_3.png`……(无前导零)
|
|
312
|
+
- 如果没有找到数字,库会自动添加 `_%d`(例如 `cursor.png` → `cursor_1.png`、`cursor_2.png`)。
|
|
313
|
+
- **重要**:所有帧的图片尺寸必须相同。光标实际大小即为图片的原始尺寸,如需缩放请预先处理好图片。
|
|
316
314
|
|
|
317
315
|
### 🎞️ 动画判定
|
|
318
316
|
|
|
319
|
-
|
|
317
|
+
- **匀速动画**:`frames` 和 `duration` 均为数字。总帧数均匀分布到总时长中。
|
|
318
|
+
- **变速动画**:`frames` 和 `duration` 均为等长数组。每个片段指定帧数和该片段的时长,片段内的帧均匀分布。
|
|
319
|
+
- **静态光标**:如果缺少 `frames` 或 `duration`,或者设置无效(如非正数、数组长度不匹配),则光标被视为静态(仅使用第一帧图片)。控制台会输出警告。
|
|
320
320
|
|
|
321
|
-
|
|
321
|
+
### 🧩 标记机制
|
|
322
322
|
|
|
323
|
-
|
|
324
|
-
* 设置了 `duration`
|
|
323
|
+
AnimeCursor 会根据 `tags` 和 `data-cursor` 自动应用光标样式:
|
|
325
324
|
|
|
326
|
-
|
|
325
|
+
- 若元素标签名匹配某个 `tags`,则应用对应的光标。
|
|
326
|
+
- 你也可以手动给任意元素添加 `data-cursor="光标名称"` 来强制指定光标。
|
|
327
|
+
- 当没有其他规则匹配时,使用默认光标。
|
|
327
328
|
|
|
328
|
-
|
|
329
|
+
### 🔧 性能表现
|
|
329
330
|
|
|
330
|
-
|
|
331
|
-
* 动画由 GIF 自身控制
|
|
331
|
+
v2 版本使用原生 CSS `cursor` 属性和 CSS 动画,除了 `debug` 模式外,不再需要 JavaScript 监听鼠标移动。因此性能极佳,光标移动流畅。
|
|
332
332
|
|
|
333
333
|
## ❌ 错误处理
|
|
334
334
|
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
335
|
+
- 缺少必填配置会抛出错误并停止初始化。
|
|
336
|
+
- 无效的可选配置(如数组长度不匹配)会输出警告并将该光标视为静态。
|
|
337
|
+
- 所有错误和警告均以 `[AnimeCursor]` 前缀输出到控制台。
|