use-canvas-drag 0.2.0 → 0.3.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 +2 -2
- package/dist/index.esm.js +92 -0
- package/dist/index.umd.js +1 -1
- package/dist/src/index.d.ts +11 -0
- package/dist/src/useCanvasDrag.d.ts +2 -0
- package/package.json +10 -7
package/README.md
CHANGED
|
@@ -147,8 +147,8 @@ const { isPanning } = useCanvasDrag({
|
|
|
147
147
|
|------|------|------|
|
|
148
148
|
| `x` | `number` | 相对于起始位置的 X 轴偏移 |
|
|
149
149
|
| `y` | `number` | 相对于起始位置的 Y 轴偏移 |
|
|
150
|
-
| `deltaX` | `number` |
|
|
151
|
-
| `deltaY` | `number` |
|
|
150
|
+
| `deltaX` | `number` | **帧间增量**:相对于上一帧的 X 轴偏移 |
|
|
151
|
+
| `deltaY` | `number` | **帧间增量**:相对于上一帧的 Y 轴偏移 |
|
|
152
152
|
|
|
153
153
|
### 返回值
|
|
154
154
|
|
package/dist/index.esm.js
CHANGED
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { computed as e, onUnmounted as t, ref as n, watch as r } from "vue";
|
|
2
|
+
//#region src/useCanvasDrag.ts
|
|
3
|
+
var i = ["right"], a = {
|
|
4
|
+
left: { button: 0 },
|
|
5
|
+
right: { button: 2 },
|
|
6
|
+
middle: { button: 1 },
|
|
7
|
+
"shift-left": {
|
|
8
|
+
button: 0,
|
|
9
|
+
modifiers: { shift: !0 }
|
|
10
|
+
},
|
|
11
|
+
"ctrl-left": {
|
|
12
|
+
button: 0,
|
|
13
|
+
modifiers: { ctrl: !0 }
|
|
14
|
+
},
|
|
15
|
+
"alt-left": {
|
|
16
|
+
button: 0,
|
|
17
|
+
modifiers: { alt: !0 }
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
function o(e) {
|
|
21
|
+
return e ? e.map((e) => typeof e == "string" ? a[e] ?? { button: -1 } : e) : i.map((e) => a[e]);
|
|
22
|
+
}
|
|
23
|
+
function s(i) {
|
|
24
|
+
let { container: a, enabled: s = !0, onStartDrag: c, onDrag: l, onEndDrag: u, mode: d } = i, f = n(!1), p = n({
|
|
25
|
+
x: 0,
|
|
26
|
+
y: 0,
|
|
27
|
+
scrollLeft: 0,
|
|
28
|
+
scrollTop: 0
|
|
29
|
+
}), m = n({
|
|
30
|
+
x: 0,
|
|
31
|
+
y: 0
|
|
32
|
+
}), h = e(() => o(Array.isArray(d) ? d : d ? [d] : void 0)), g = () => typeof a == "string" ? document.querySelector(a) : typeof a == "function" ? a() : a instanceof HTMLElement ? a : null, _ = (e) => h.value.some((t) => {
|
|
33
|
+
if (t.button !== void 0 && t.button !== e.button) return !1;
|
|
34
|
+
if (t.modifiers) {
|
|
35
|
+
let n = t.modifiers;
|
|
36
|
+
if (n.shift && !e.shiftKey || n.ctrl && !e.ctrlKey || n.alt && !e.altKey || n.meta && !e.metaKey) return !1;
|
|
37
|
+
}
|
|
38
|
+
return !0;
|
|
39
|
+
}), v = (e) => {
|
|
40
|
+
if (!s || !_(e)) return;
|
|
41
|
+
let t = g();
|
|
42
|
+
t && (p.value = {
|
|
43
|
+
x: e.clientX,
|
|
44
|
+
y: e.clientY,
|
|
45
|
+
scrollLeft: t.scrollLeft,
|
|
46
|
+
scrollTop: t.scrollTop
|
|
47
|
+
}, m.value = {
|
|
48
|
+
x: e.clientX,
|
|
49
|
+
y: e.clientY
|
|
50
|
+
}, f.value = !0, t.style.cursor = "grabbing", t.style.userSelect = "none", e.preventDefault(), c?.(e), document.addEventListener("mousemove", y), document.addEventListener("mouseup", b));
|
|
51
|
+
}, y = (e) => {
|
|
52
|
+
if (!f.value) return;
|
|
53
|
+
let t = g();
|
|
54
|
+
if (!t) return;
|
|
55
|
+
let n = e.clientX - p.value.x, r = e.clientY - p.value.y, i = e.clientX - m.value.x, a = e.clientY - m.value.y;
|
|
56
|
+
m.value = {
|
|
57
|
+
x: e.clientX,
|
|
58
|
+
y: e.clientY
|
|
59
|
+
}, t.scrollLeft = p.value.scrollLeft - n, t.scrollTop = p.value.scrollTop - r, l?.({
|
|
60
|
+
x: n,
|
|
61
|
+
y: r,
|
|
62
|
+
deltaX: i,
|
|
63
|
+
deltaY: a
|
|
64
|
+
});
|
|
65
|
+
}, b = (e) => {
|
|
66
|
+
if (!f.value) return;
|
|
67
|
+
f.value = !1;
|
|
68
|
+
let t = g();
|
|
69
|
+
t && (t.style.cursor = "auto", t.style.userSelect = ""), document.removeEventListener("mousemove", y), document.removeEventListener("mouseup", b), u?.();
|
|
70
|
+
}, x = (e) => {
|
|
71
|
+
!s || !_(e) || e.preventDefault();
|
|
72
|
+
}, S = () => {
|
|
73
|
+
if (!f.value) return;
|
|
74
|
+
f.value = !1;
|
|
75
|
+
let e = g();
|
|
76
|
+
e && (e.style.cursor = "auto", e.style.userSelect = ""), document.removeEventListener("mousemove", y), document.removeEventListener("mouseup", b), u?.();
|
|
77
|
+
};
|
|
78
|
+
return r(() => s, (e) => {
|
|
79
|
+
!e && f.value && S();
|
|
80
|
+
}), t(() => {
|
|
81
|
+
document.removeEventListener("mousemove", y), document.removeEventListener("mouseup", b);
|
|
82
|
+
}), {
|
|
83
|
+
isPanning: f,
|
|
84
|
+
handlers: {
|
|
85
|
+
onMouseDown: v,
|
|
86
|
+
onContextMenu: x
|
|
87
|
+
},
|
|
88
|
+
stopPan: S
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
//#endregion
|
|
92
|
+
export { s as useCanvasDrag };
|
package/dist/index.umd.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(e){typeof define==`function`&&define.amd?define([],e):e()})(function(){});
|
|
1
|
+
(function(e,t){typeof exports==`object`&&typeof module<`u`?t(exports,require(`vue`)):typeof define==`function`&&define.amd?define([`exports`,`vue`],t):(e=typeof globalThis<`u`?globalThis:e||self,t(e.UseCanvasDrag={},e.Vue))})(this,function(e,t){Object.defineProperty(e,Symbol.toStringTag,{value:`Module`});var n=[`right`],r={left:{button:0},right:{button:2},middle:{button:1},"shift-left":{button:0,modifiers:{shift:!0}},"ctrl-left":{button:0,modifiers:{ctrl:!0}},"alt-left":{button:0,modifiers:{alt:!0}}};function i(e){return e?e.map(e=>typeof e==`string`?r[e]??{button:-1}:e):n.map(e=>r[e])}function a(e){let{container:n,enabled:r=!0,onStartDrag:a,onDrag:o,onEndDrag:s,mode:c}=e,l=(0,t.ref)(!1),u=(0,t.ref)({x:0,y:0,scrollLeft:0,scrollTop:0}),d=(0,t.ref)({x:0,y:0}),f=(0,t.computed)(()=>i(Array.isArray(c)?c:c?[c]:void 0)),p=()=>typeof n==`string`?document.querySelector(n):typeof n==`function`?n():n instanceof HTMLElement?n:null,m=e=>f.value.some(t=>{if(t.button!==void 0&&t.button!==e.button)return!1;if(t.modifiers){let n=t.modifiers;if(n.shift&&!e.shiftKey||n.ctrl&&!e.ctrlKey||n.alt&&!e.altKey||n.meta&&!e.metaKey)return!1}return!0}),h=e=>{if(!r||!m(e))return;let t=p();t&&(u.value={x:e.clientX,y:e.clientY,scrollLeft:t.scrollLeft,scrollTop:t.scrollTop},d.value={x:e.clientX,y:e.clientY},l.value=!0,t.style.cursor=`grabbing`,t.style.userSelect=`none`,e.preventDefault(),a?.(e),document.addEventListener(`mousemove`,g),document.addEventListener(`mouseup`,_))},g=e=>{if(!l.value)return;let t=p();if(!t)return;let n=e.clientX-u.value.x,r=e.clientY-u.value.y,i=e.clientX-d.value.x,a=e.clientY-d.value.y;d.value={x:e.clientX,y:e.clientY},t.scrollLeft=u.value.scrollLeft-n,t.scrollTop=u.value.scrollTop-r,o?.({x:n,y:r,deltaX:i,deltaY:a})},_=e=>{if(!l.value)return;l.value=!1;let t=p();t&&(t.style.cursor=`auto`,t.style.userSelect=``),document.removeEventListener(`mousemove`,g),document.removeEventListener(`mouseup`,_),s?.()},v=e=>{!r||!m(e)||e.preventDefault()},y=()=>{if(!l.value)return;l.value=!1;let e=p();e&&(e.style.cursor=`auto`,e.style.userSelect=``),document.removeEventListener(`mousemove`,g),document.removeEventListener(`mouseup`,_),s?.()};return(0,t.watch)(()=>r,e=>{!e&&l.value&&y()}),(0,t.onUnmounted)(()=>{document.removeEventListener(`mousemove`,g),document.removeEventListener(`mouseup`,_)}),{isPanning:l,handlers:{onMouseDown:h,onContextMenu:v},stopPan:y}}e.useCanvasDrag=a});
|
package/dist/src/index.d.ts
CHANGED
|
@@ -1 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @description 主入口文件
|
|
3
|
+
* @author lrb
|
|
4
|
+
* @date 2026-05-11 23:21:24
|
|
5
|
+
* @lastModifyTime 2026-05-11 23:21:24
|
|
6
|
+
* @lastModifyAuthor lrb
|
|
7
|
+
* @license MIT
|
|
8
|
+
* @copyright lrb
|
|
9
|
+
* @version 1.0.0
|
|
10
|
+
*/
|
|
11
|
+
export { useCanvasDrag } from './useCanvasDrag';
|
|
1
12
|
export type { DragButtonConfig, CanvasDragOptions, DragTrigger } from './useCanvasDrag';
|
package/package.json
CHANGED
|
@@ -1,17 +1,17 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "use-canvas-drag",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "Vue3 画布拖拽组合式函数,支持多种触发方式",
|
|
5
5
|
"main": "./dist/index.umd.js",
|
|
6
6
|
"module": "./dist/index.esm.js",
|
|
7
7
|
"types": "./dist/index.d.ts",
|
|
8
8
|
"exports": {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
},
|
|
9
|
+
".": {
|
|
10
|
+
"import": "./dist/index.esm.js",
|
|
11
|
+
"require": "./dist/index.umd.js",
|
|
12
|
+
"types": "./dist/index.d.ts"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
15
|
"files": [
|
|
16
16
|
"dist"
|
|
17
17
|
],
|
|
@@ -38,5 +38,8 @@
|
|
|
38
38
|
"vite-plugin-dts": "^4.5.0",
|
|
39
39
|
"vue": "^3.5.34",
|
|
40
40
|
"vue-tsc": "^3.2.8"
|
|
41
|
+
},
|
|
42
|
+
"dependencies": {
|
|
43
|
+
"use-canvas-drag": "^0.2.1"
|
|
41
44
|
}
|
|
42
45
|
}
|