Skip to content

Commit 223809d

Browse files
committed
refactor: migrate to Pointer Events API for improved interaction handling
- Updated event handling in index.html to use pointer events instead of mouse/touch events. - Added a new event testing section in index.html to demonstrate pointer event logging. - Modified click, dblClick, long, and allowEvent functions to accept PointerEvent. - Refactored down, drag, move, resize, and scale functions to utilize PointerEvent. - Updated utility functions to support PointerEvent and removed legacy touch/mouse checks. - Enhanced scale functionality to handle multi-pointer interactions more effectively. - Adjusted types in types.ts to reflect changes in event handling.
1 parent 40e0881 commit 223809d

File tree

16 files changed

+549
-513
lines changed

16 files changed

+549
-513
lines changed

README.md

Lines changed: 62 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@
1515
</a>
1616
</p>
1717

18-
A lightweight pointer event library for handling mouse and touch interactions in browsers. Provides unified APIs for down, move, click, long, drag, resize and more.
18+
A lightweight pointer event library for handling mouse, touch, and pen interactions in browsers. Provides unified APIs for down, move, click, long, drag, resize and more.
1919

2020
## Features
2121

22-
- 🖱️ Unified mouse and touch event handling
22+
- 🖱️ Unified pointer event handling (mouse, touch, pen)
2323
- 📱 Mobile-friendly with touch support
2424
- 🎯 Click, double-click, and long-press detection
2525
- 🔄 Drag and drop with customizable constraints
@@ -54,7 +54,7 @@ npm install @litert/pointer
5454
```typescript
5555
import * as pointer from '@litert/pointer';
5656

57-
element.addEventListener('mousedown', (e) => {
57+
element.addEventListener('pointerdown', (e) => {
5858
pointer.move(e, {
5959
move: (e, detail) => {
6060
console.log('Moving:', detail.ox, detail.oy);
@@ -68,7 +68,7 @@ element.addEventListener('mousedown', (e) => {
6868
```html
6969
<script src="https://unpkg.com/@litert/pointer/dist/index.umd.min.js"></script>
7070
<script>
71-
element.addEventListener('mousedown', function(e) {
71+
element.addEventListener('pointerdown', function(e) {
7272
pointer.move(e, {
7373
move: function(e, detail) {
7474
console.log('Moving:', detail.ox, detail.oy);
@@ -78,13 +78,11 @@ element.addEventListener('mousedown', function(e) {
7878
</script>
7979
```
8080

81-
## API Reference
82-
8381
### Core Functions
8482

8583
#### `down(e, options)`
8684

87-
Down and up events, bindto one of touch or mouse.
85+
Down and up events, bind to pointer events.
8886

8987
```typescript
9088
pointer.down(e, {
@@ -98,10 +96,10 @@ pointer.down(e, {
9896

9997
#### `click(e, handler)`
10098

101-
Click takes effect only when the mouse/finger does not move and the time is less than 250ms.
99+
Click takes effect only when the pointer does not move and the time is less than 250ms.
102100

103101
```typescript
104-
element.addEventListener('mousedown', (e) => {
102+
element.addEventListener('pointerdown', (e) => {
105103
pointer.click(e, (e, x, y) => {
106104
console.log('Clicked at:', x, y);
107105
});
@@ -113,7 +111,7 @@ element.addEventListener('mousedown', (e) => {
113111
Double-click event, the interval between two clicks is less than 300ms and the position difference is less than 10px.
114112

115113
```typescript
116-
element.addEventListener('mousedown', (e) => {
114+
element.addEventListener('pointerdown', (e) => {
117115
pointer.dblClick(e, (e, x, y) => {
118116
console.log('Double clicked at:', x, y);
119117
});
@@ -125,7 +123,7 @@ element.addEventListener('mousedown', (e) => {
125123
Long press event, default 500ms.
126124

127125
```typescript
128-
element.addEventListener('mousedown', (e) => {
126+
element.addEventListener('pointerdown', (e) => {
129127
pointer.long(e, (e) => {
130128
console.log('Long press detected!');
131129
}, 500);
@@ -137,7 +135,7 @@ element.addEventListener('mousedown', (e) => {
137135
Drag event, supports boundary detection and constraints.
138136

139137
```typescript
140-
element.addEventListener('mousedown', (e) => {
138+
element.addEventListener('pointerdown', (e) => {
141139
pointer.move(e, {
142140
// --- Constraint area ---
143141
left: 0,
@@ -179,7 +177,7 @@ element.addEventListener('mousedown', (e) => {
179177
Resize event.
180178

181179
```typescript
182-
element.addEventListener('mousedown', (e) => {
180+
element.addEventListener('pointerdown', (e) => {
183181
pointer.resize(e, {
184182
object: element,
185183
border: 'rb', // lt, t, tr, r, rb, b, bl, l
@@ -209,7 +207,7 @@ Drag event, supports drag and drop to target elements.
209207

210208
```typescript
211209
// --- Set drag source ---
212-
dragSource.addEventListener('mousedown', (e) => {
210+
dragSource.addEventListener('pointerdown', (e) => {
213211
pointer.drag(e, dragSource, {
214212
data: { id: 1, name: 'item' },
215213
start: (x, y) => {
@@ -238,6 +236,45 @@ dropTarget.addEventListener('drop', (e) => {
238236
});
239237
```
240238

239+
#### `scale(e, handler)`
240+
241+
Scale/zoom event, supports pinch-to-zoom on touch devices and mouse wheel.
242+
243+
```typescript
244+
element.addEventListener('pointerdown', (e) => {
245+
pointer.scale(e, (e, scale, cpos) => {
246+
console.log('Scale:', scale, 'Center:', cpos.x, cpos.y);
247+
});
248+
});
249+
element.addEventListener('wheel', (e) => {
250+
pointer.scale(e, (e, scale, cpos) => {
251+
console.log('Scale:', scale, 'Center:', cpos.x, cpos.y);
252+
});
253+
});
254+
```
255+
256+
#### `gesture(e, before, handler)`
257+
258+
Gesture event for swipe actions (up, down, left, right).
259+
260+
```typescript
261+
element.addEventListener('pointerdown', (e) => {
262+
pointer.gesture(e, (e, dir) => {
263+
// Return 1 to show gesture indicator, 0 to ignore, -1 to stop propagation
264+
return 1;
265+
}, (dir) => {
266+
console.log('Gesture completed:', dir);
267+
});
268+
});
269+
element.addEventListener('wheel', (e) => {
270+
pointer.gesture(e, (e, dir) => {
271+
return 1;
272+
}, (dir) => {
273+
console.log('Wheel gesture:', dir);
274+
});
275+
});
276+
```
277+
241278
### Utility Functions
242279

243280
#### `setCursor(type?)`
@@ -250,14 +287,13 @@ pointer.setCursor('move');
250287
pointer.setCursor(); // Cancel
251288
```
252289

253-
#### `hasTouchButMouse(e)`
290+
#### `isTouch(e)`
254291

255-
Determine whether the current event is a mouse event triggered by a touch device.
292+
Check if the pointer event is from a touch device.
256293

257294
```typescript
258-
if (pointer.hasTouchButMouse(e)) {
259-
return; // Ignore mouse events on touch devices
260-
}
295+
const isTouch = pointer.isTouch(e);
296+
console.log('Is touch device:', isTouch);
261297
```
262298

263299
## Types
@@ -314,11 +350,13 @@ Then open `dist/test/index.html` in your browser.
314350

315351
## Browser Support
316352

317-
- Chrome (latest)
318-
- Firefox (latest)
319-
- Safari (latest)
320-
- Edge (latest)
321-
- Mobile browsers (iOS Safari, Chrome for Android)
353+
- Chrome 55+
354+
- Firefox 59+
355+
- Safari 13+
356+
- Edge 79+
357+
- Mobile browsers (iOS Safari 13+, Chrome for Android)
358+
359+
Requires Pointer Events API support. For older browsers, consider using a polyfill.
322360

323361
## License
324362

0 commit comments

Comments
 (0)