@cos-design/spring-mass 3.6.0 → 4.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/dist/core/index.d.ts +1 -0
- package/dist/core/index.js +154 -0
- package/dist/core/packages/shared/src/canvas-box.d.ts +18 -0
- package/dist/core/packages/shared/src/host-layout.d.ts +22 -0
- package/dist/core/packages/shared/src/index.d.ts +11 -0
- package/dist/core/packages/shared/src/math.d.ts +2 -0
- package/dist/core/packages/shared/src/photo.d.ts +15 -0
- package/dist/core/packages/shared/src/pointer.d.ts +14 -0
- package/dist/core/packages/shared/src/react/index.d.ts +30 -0
- package/dist/core/packages/shared/src/reduced-motion.d.ts +4 -0
- package/dist/core/packages/shared/src/size.d.ts +8 -0
- package/dist/{packages/shared/src/index.d.ts → core/packages/shared/src/visibility.d.ts} +0 -1
- package/dist/core/packages/springMass/src/core.d.ts +1 -0
- package/dist/core/packages/springMass/src/element.d.ts +1 -0
- package/dist/core/packages/springMass/src/react.d.ts +3 -0
- package/dist/core/packages/springMass/src/vue.d.ts +2 -0
- package/dist/core/src/components/springMass/core/engine.d.ts +2 -0
- package/dist/core/src/components/springMass/core/index.d.ts +2 -0
- package/dist/{src/components/springMass/index.d.ts → core/src/components/springMass/core/types.d.ts} +8 -4
- package/dist/core/src/components/springMass/element/index.d.ts +9 -0
- package/dist/core/src/components/springMass/index.d.ts +3 -0
- package/dist/core/src/components/springMass/react/index.d.ts +4 -0
- package/dist/{src → core/src}/vite-env.d.ts +6 -0
- package/dist/element/element.css +1 -0
- package/dist/element/index.d.ts +1 -0
- package/dist/element/index.js +186 -0
- package/dist/element/packages/shared/src/canvas-box.d.ts +18 -0
- package/dist/element/packages/shared/src/host-layout.d.ts +22 -0
- package/dist/element/packages/shared/src/index.d.ts +11 -0
- package/dist/element/packages/shared/src/math.d.ts +2 -0
- package/dist/element/packages/shared/src/photo.d.ts +15 -0
- package/dist/element/packages/shared/src/pointer.d.ts +14 -0
- package/dist/element/packages/shared/src/react/index.d.ts +30 -0
- package/dist/element/packages/shared/src/reduced-motion.d.ts +4 -0
- package/dist/element/packages/shared/src/size.d.ts +8 -0
- package/dist/element/packages/shared/src/visibility.d.ts +2 -0
- package/dist/element/packages/springMass/src/core.d.ts +1 -0
- package/dist/element/packages/springMass/src/element.d.ts +1 -0
- package/dist/element/packages/springMass/src/index.d.ts +3 -0
- package/dist/element/packages/springMass/src/react.d.ts +3 -0
- package/dist/element/packages/springMass/src/vue.d.ts +2 -0
- package/dist/element/src/components/springMass/core/engine.d.ts +2 -0
- package/dist/element/src/components/springMass/core/index.d.ts +2 -0
- package/dist/element/src/components/springMass/core/types.d.ts +21 -0
- package/dist/element/src/components/springMass/element/index.d.ts +9 -0
- package/dist/element/src/components/springMass/index.d.ts +3 -0
- package/dist/element/src/components/springMass/react/index.d.ts +4 -0
- package/dist/element/src/vite-env.d.ts +45 -0
- package/dist/react/index.cjs +1 -0
- package/dist/react/index.d.ts +3 -0
- package/dist/react/index.js +177 -0
- package/dist/react/packages/shared/src/canvas-box.d.ts +18 -0
- package/dist/react/packages/shared/src/host-layout.d.ts +22 -0
- package/dist/react/packages/shared/src/index.d.ts +11 -0
- package/dist/react/packages/shared/src/math.d.ts +2 -0
- package/dist/react/packages/shared/src/photo.d.ts +15 -0
- package/dist/react/packages/shared/src/pointer.d.ts +14 -0
- package/dist/react/packages/shared/src/react/index.d.ts +30 -0
- package/dist/react/packages/shared/src/reduced-motion.d.ts +4 -0
- package/dist/react/packages/shared/src/size.d.ts +8 -0
- package/dist/react/packages/shared/src/visibility.d.ts +2 -0
- package/dist/react/packages/springMass/src/core.d.ts +1 -0
- package/dist/react/packages/springMass/src/element.d.ts +1 -0
- package/dist/react/packages/springMass/src/index.d.ts +3 -0
- package/dist/react/packages/springMass/src/react.d.ts +3 -0
- package/dist/react/packages/springMass/src/vue.d.ts +2 -0
- package/dist/react/react.css +1 -0
- package/dist/react/src/components/springMass/core/engine.d.ts +2 -0
- package/dist/react/src/components/springMass/core/index.d.ts +2 -0
- package/dist/react/src/components/springMass/core/types.d.ts +21 -0
- package/dist/react/src/components/springMass/element/index.d.ts +9 -0
- package/dist/react/src/components/springMass/index.d.ts +3 -0
- package/dist/react/src/components/springMass/react/index.d.ts +4 -0
- package/dist/react/src/vite-env.d.ts +45 -0
- package/dist/vue/index.d.ts +2 -0
- package/dist/vue/index.js +182 -0
- package/dist/vue/packages/shared/src/canvas-box.d.ts +18 -0
- package/dist/vue/packages/shared/src/host-layout.d.ts +22 -0
- package/dist/vue/packages/shared/src/index.d.ts +11 -0
- package/dist/vue/packages/shared/src/math.d.ts +2 -0
- package/dist/vue/packages/shared/src/photo.d.ts +15 -0
- package/dist/vue/packages/shared/src/pointer.d.ts +14 -0
- package/dist/vue/packages/shared/src/react/index.d.ts +30 -0
- package/dist/vue/packages/shared/src/reduced-motion.d.ts +4 -0
- package/dist/vue/packages/shared/src/size.d.ts +8 -0
- package/dist/vue/packages/shared/src/visibility.d.ts +2 -0
- package/dist/vue/packages/springMass/src/core.d.ts +1 -0
- package/dist/vue/packages/springMass/src/element.d.ts +1 -0
- package/dist/vue/packages/springMass/src/index.d.ts +3 -0
- package/dist/vue/packages/springMass/src/react.d.ts +3 -0
- package/dist/vue/packages/springMass/src/vue.d.ts +2 -0
- package/dist/vue/src/components/springMass/core/engine.d.ts +2 -0
- package/dist/vue/src/components/springMass/core/index.d.ts +2 -0
- package/dist/vue/src/components/springMass/core/types.d.ts +21 -0
- package/dist/vue/src/components/springMass/element/index.d.ts +9 -0
- package/dist/vue/src/components/springMass/index.d.ts +3 -0
- package/dist/vue/src/components/springMass/react/index.d.ts +4 -0
- package/dist/vue/src/vite-env.d.ts +45 -0
- package/dist/vue/vue.css +1 -0
- package/package.json +43 -11
- package/dist/index.cjs +0 -1
- package/dist/index.css +0 -1
- package/dist/index.d.ts +0 -3
- package/dist/index.js +0 -172
- /package/dist/{packages → core/packages}/springMass/src/index.d.ts +0 -0
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/// <reference types="vite/client" />
|
|
2
|
+
|
|
3
|
+
declare module '*.vue' {
|
|
4
|
+
import type { DefineComponent } from 'vue';
|
|
5
|
+
const component: DefineComponent<object, object, unknown>;
|
|
6
|
+
export default component;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
declare module '*.module.less' {
|
|
10
|
+
const classes: { readonly [key: string]: string };
|
|
11
|
+
export default classes;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
declare module '*.less' {
|
|
15
|
+
const classes: { readonly [key: string]: string };
|
|
16
|
+
export default classes;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
declare module '*.png' {
|
|
20
|
+
const src: string;
|
|
21
|
+
export default src;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
declare module '*.jpg' {
|
|
25
|
+
const src: string;
|
|
26
|
+
export default src;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
declare module '*.mp4' {
|
|
30
|
+
const src: string;
|
|
31
|
+
export default src;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
declare module '*.mp3' {
|
|
35
|
+
const src: string;
|
|
36
|
+
export default src;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
declare module '*.svg' {
|
|
40
|
+
import type { FC, SVGProps } from 'react';
|
|
41
|
+
|
|
42
|
+
export const ReactComponent: FC<SVGProps<SVGSVGElement>>;
|
|
43
|
+
const src: string;
|
|
44
|
+
export default src;
|
|
45
|
+
}
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
import { createElementBlock as e, defineComponent as t, onMounted as n, onUnmounted as r, openBlock as i, ref as a, watch as o } from "vue";
|
|
2
|
+
import { bindVisibilityPause as s, optionsFingerprint as c } from "@cos-design/shared";
|
|
3
|
+
import './vue.css';//#region src/components/springMass/core/engine.ts
|
|
4
|
+
var l = "cos-spring-mass", u = (e, t, n, r) => {
|
|
5
|
+
let i = Math.max(3, Math.min(n, 10)), a = Math.max(3, Math.min(r, 8)), o = e * .16, s = t * .18, c = (e - o * 2) / (i - 1), l = (t - s * 2) / (a - 1), u = [];
|
|
6
|
+
for (let e = 0; e < a; e++) for (let t = 0; t < i; t++) {
|
|
7
|
+
let n = e === 0 && (t === 0 || t === i - 1) || e === a - 1 && (t === 0 || t === i - 1);
|
|
8
|
+
u.push({
|
|
9
|
+
x: o + t * c,
|
|
10
|
+
y: s + e * l,
|
|
11
|
+
vx: 0,
|
|
12
|
+
vy: 0,
|
|
13
|
+
pinned: n
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
let d = [], f = (e, t) => t * i + e;
|
|
17
|
+
for (let e = 0; e < a; e++) for (let t = 0; t < i; t++) if (t < i - 1 && d.push({
|
|
18
|
+
a: f(t, e),
|
|
19
|
+
b: f(t + 1, e),
|
|
20
|
+
rest: c
|
|
21
|
+
}), e < a - 1 && d.push({
|
|
22
|
+
a: f(t, e),
|
|
23
|
+
b: f(t, e + 1),
|
|
24
|
+
rest: l
|
|
25
|
+
}), t < i - 1 && e < a - 1) {
|
|
26
|
+
let n = Math.hypot(c, l);
|
|
27
|
+
d.push({
|
|
28
|
+
a: f(t, e),
|
|
29
|
+
b: f(t + 1, e + 1),
|
|
30
|
+
rest: n
|
|
31
|
+
}), d.push({
|
|
32
|
+
a: f(t + 1, e),
|
|
33
|
+
b: f(t, e + 1),
|
|
34
|
+
rest: n
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
return {
|
|
38
|
+
nodes: u,
|
|
39
|
+
springs: d
|
|
40
|
+
};
|
|
41
|
+
};
|
|
42
|
+
function d(e, t = {}) {
|
|
43
|
+
let n = {
|
|
44
|
+
width: 560,
|
|
45
|
+
height: 400,
|
|
46
|
+
cols: 6,
|
|
47
|
+
rows: 5,
|
|
48
|
+
stiffness: .22,
|
|
49
|
+
damping: .9,
|
|
50
|
+
color: "#a78bfa",
|
|
51
|
+
hint: "拖拽网格质点,观察弹簧回弹",
|
|
52
|
+
...t
|
|
53
|
+
}, r = !1, i = 0, a = null, o = u(n.width ?? 560, n.height ?? 400, n.cols ?? 6, n.rows ?? 5), c = null, d = {
|
|
54
|
+
x: 0,
|
|
55
|
+
y: 0
|
|
56
|
+
}, f = document.createElement("div"), p = document.createElement("canvas");
|
|
57
|
+
p.className = `${l}__canvas`;
|
|
58
|
+
let m = document.createElement("div");
|
|
59
|
+
m.className = `${l}__hint`, f.append(p, m), e.appendChild(f);
|
|
60
|
+
let h = () => {
|
|
61
|
+
o = u(n.width ?? 560, n.height ?? 400, n.cols ?? 6, n.rows ?? 5), c = null;
|
|
62
|
+
}, g = (e, t) => {
|
|
63
|
+
let n = -1, r = 20;
|
|
64
|
+
return o.nodes.forEach((i, a) => {
|
|
65
|
+
if (i.pinned) return;
|
|
66
|
+
let o = Math.hypot(i.x - e, i.y - t);
|
|
67
|
+
o < r && (r = o, n = a);
|
|
68
|
+
}), n;
|
|
69
|
+
}, _ = (e, t) => {
|
|
70
|
+
let n = p.getBoundingClientRect(), r = e - n.left, i = t - n.top;
|
|
71
|
+
d.x = r, d.y = i;
|
|
72
|
+
let a = g(r, i);
|
|
73
|
+
if (a < 0) return;
|
|
74
|
+
let s = o.nodes[a];
|
|
75
|
+
c = {
|
|
76
|
+
index: a,
|
|
77
|
+
ox: s.x - r,
|
|
78
|
+
oy: s.y - i
|
|
79
|
+
};
|
|
80
|
+
}, v = (e, t) => {
|
|
81
|
+
let n = p.getBoundingClientRect();
|
|
82
|
+
d.x = e - n.left, d.y = t - n.top;
|
|
83
|
+
}, y = () => {
|
|
84
|
+
c = null;
|
|
85
|
+
}, b = (e) => _(e.clientX, e.clientY), x = (e) => v(e.clientX, e.clientY), S = (e) => {
|
|
86
|
+
let t = e.touches[0];
|
|
87
|
+
t && _(t.clientX, t.clientY);
|
|
88
|
+
}, C = (e) => {
|
|
89
|
+
let t = e.touches[0];
|
|
90
|
+
t && v(t.clientX, t.clientY);
|
|
91
|
+
};
|
|
92
|
+
p.addEventListener("mousedown", b), p.addEventListener("mousemove", x), p.addEventListener("mouseup", y), p.addEventListener("mouseleave", y), p.addEventListener("touchstart", S, { passive: !0 }), p.addEventListener("touchmove", C, { passive: !0 }), p.addEventListener("touchend", y);
|
|
93
|
+
let w = () => {
|
|
94
|
+
i && cancelAnimationFrame(i), i = 0, a?.(), a = null;
|
|
95
|
+
}, T = () => {
|
|
96
|
+
if (w(), r) return;
|
|
97
|
+
let e = n.width ?? 560, t = n.height ?? 400, u = n.color ?? "#a78bfa", h = n.damping ?? .9, g = Math.max(.04, Math.min(n.stiffness ?? .22, .8));
|
|
98
|
+
f.className = l, f.style.width = `${e}px`, f.style.height = `${t}px`, p.style.width = `${e}px`, p.style.height = `${t}px`, m.textContent = n.hint ?? "拖拽网格质点,观察弹簧回弹";
|
|
99
|
+
let _ = window.devicePixelRatio || 1;
|
|
100
|
+
p.width = e * _, p.height = t * _;
|
|
101
|
+
let v = p.getContext("2d");
|
|
102
|
+
if (!v) return;
|
|
103
|
+
v.setTransform(_, 0, 0, _, 0, 0);
|
|
104
|
+
let y = typeof document < "u" ? document.hidden : !1;
|
|
105
|
+
a = s((e) => {
|
|
106
|
+
y = e;
|
|
107
|
+
});
|
|
108
|
+
let b = () => {
|
|
109
|
+
let { nodes: n, springs: r } = o;
|
|
110
|
+
if (c) {
|
|
111
|
+
let e = n[c.index];
|
|
112
|
+
e.x = d.x + c.ox, e.y = d.y + c.oy, e.vx = 0, e.vy = 0;
|
|
113
|
+
}
|
|
114
|
+
for (let e of r) {
|
|
115
|
+
let t = n[e.a], r = n[e.b], i = r.x - t.x, a = r.y - t.y, o = Math.hypot(i, a) || .001, s = (o - e.rest) * g, l = i / o * s, u = a / o * s, d = c?.index === e.a, f = c?.index === e.b;
|
|
116
|
+
!t.pinned && !d && (t.vx += l, t.vy += u), !r.pinned && !f && (r.vx -= l, r.vy -= u);
|
|
117
|
+
}
|
|
118
|
+
for (let r = 0; r < n.length; r++) {
|
|
119
|
+
let i = n[r];
|
|
120
|
+
if (i.pinned || c?.index === r) {
|
|
121
|
+
i.vx = 0, i.vy = 0;
|
|
122
|
+
continue;
|
|
123
|
+
}
|
|
124
|
+
i.vx *= h, i.vy *= h, i.x += i.vx, i.y += i.vy, i.x < 10 ? (i.x = 10, i.vx *= -.4) : i.x > e - 10 && (i.x = e - 10, i.vx *= -.4), i.y < 10 ? (i.y = 10, i.vy *= -.4) : i.y > t - 10 && (i.y = t - 10, i.vy *= -.4);
|
|
125
|
+
}
|
|
126
|
+
}, x = () => {
|
|
127
|
+
let { nodes: n, springs: r } = o;
|
|
128
|
+
v.fillStyle = "#0f172a", v.fillRect(0, 0, e, t);
|
|
129
|
+
for (let i of r) {
|
|
130
|
+
let r = n[i.a], a = n[i.b], o = Math.hypot(a.x - r.x, a.y - r.y) || 1, s = Math.min(1, Math.abs(o - i.rest) / i.rest);
|
|
131
|
+
v.beginPath(), v.moveTo(r.x, r.y), v.lineTo(a.x, a.y), v.strokeStyle = u, v.globalAlpha = .22 + s * .55, v.lineWidth = i.rest > Math.max(e, t) * .2 ? 1 : 1.6, v.stroke(), v.globalAlpha = 1;
|
|
132
|
+
}
|
|
133
|
+
n.forEach((e, t) => {
|
|
134
|
+
v.beginPath(), v.arc(e.x, e.y, e.pinned ? 6 : 8, 0, Math.PI * 2), v.fillStyle = e.pinned ? "#64748b" : u, v.fill(), v.strokeStyle = "rgb(255 255 255 / 28%)", v.lineWidth = 1, v.stroke(), c?.index === t && (v.beginPath(), v.arc(e.x, e.y, 13, 0, Math.PI * 2), v.strokeStyle = u, v.globalAlpha = .45, v.lineWidth = 2, v.stroke(), v.globalAlpha = 1);
|
|
135
|
+
});
|
|
136
|
+
}, S = () => {
|
|
137
|
+
i = requestAnimationFrame(S), !(y || r) && (b(), x());
|
|
138
|
+
};
|
|
139
|
+
i = requestAnimationFrame(S);
|
|
140
|
+
};
|
|
141
|
+
return h(), T(), {
|
|
142
|
+
update(e) {
|
|
143
|
+
let t = n.width, r = n.height, i = n.cols, a = n.rows;
|
|
144
|
+
n = {
|
|
145
|
+
...n,
|
|
146
|
+
...e
|
|
147
|
+
}, (n.width !== t || n.height !== r || n.cols !== i || n.rows !== a) && h(), T();
|
|
148
|
+
},
|
|
149
|
+
destroy() {
|
|
150
|
+
r || (r = !0, w(), p.removeEventListener("mousedown", b), p.removeEventListener("mousemove", x), p.removeEventListener("mouseup", y), p.removeEventListener("mouseleave", y), p.removeEventListener("touchstart", S), p.removeEventListener("touchmove", C), p.removeEventListener("touchend", y), f.remove());
|
|
151
|
+
}
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
//#endregion
|
|
155
|
+
//#region src/components/springMass/vue/SpringMass.vue
|
|
156
|
+
var f = /* @__PURE__ */ t({
|
|
157
|
+
__name: "SpringMass",
|
|
158
|
+
props: {
|
|
159
|
+
width: {},
|
|
160
|
+
height: {},
|
|
161
|
+
cols: {},
|
|
162
|
+
rows: {},
|
|
163
|
+
stiffness: {},
|
|
164
|
+
damping: {},
|
|
165
|
+
color: {},
|
|
166
|
+
hint: {}
|
|
167
|
+
},
|
|
168
|
+
setup(t, { expose: s, emit: l }) {
|
|
169
|
+
let u = t, f = a(), p = null, m = () => ({ ...u });
|
|
170
|
+
return n(() => {
|
|
171
|
+
f.value && (p = d(f.value, m()));
|
|
172
|
+
}), o(() => c(u), () => p?.update(m())), r(() => {
|
|
173
|
+
p?.destroy(), p = null;
|
|
174
|
+
}), s({}), (t, n) => (i(), e("div", {
|
|
175
|
+
ref_key: "hostRef",
|
|
176
|
+
ref: f,
|
|
177
|
+
class: "cos-springMass-host"
|
|
178
|
+
}, null, 512));
|
|
179
|
+
}
|
|
180
|
+
});
|
|
181
|
+
//#endregion
|
|
182
|
+
export { f as SpringMass };
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { ElementSize } from './size';
|
|
2
|
+
export interface CanvasBoxSizeInput {
|
|
3
|
+
/** 为 true 时跟随父容器尺寸(需父级有明确高度) */
|
|
4
|
+
fill?: boolean;
|
|
5
|
+
width?: number;
|
|
6
|
+
height?: number;
|
|
7
|
+
defaultWidth: number;
|
|
8
|
+
defaultHeight: number;
|
|
9
|
+
measured: ElementSize;
|
|
10
|
+
}
|
|
11
|
+
export interface CanvasBoxSize {
|
|
12
|
+
width: number;
|
|
13
|
+
height: number;
|
|
14
|
+
/** 是否处于 fill 模式且尚未量到有效尺寸 */
|
|
15
|
+
pending: boolean;
|
|
16
|
+
}
|
|
17
|
+
/** 根据 fill / 显式宽高 / 测量结果解析画布逻辑尺寸 */
|
|
18
|
+
export declare const resolveCanvasBoxSize: ({ fill, width, height, defaultWidth, defaultHeight, measured, }: CanvasBoxSizeInput) => CanvasBoxSize;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/** Apply fill / explicit size to the adapter host and make the engine root fill it. */
|
|
2
|
+
export declare function applyCanvasHostBox(container: HTMLElement, root: HTMLElement, opts: {
|
|
3
|
+
fill: boolean;
|
|
4
|
+
width: number;
|
|
5
|
+
height: number;
|
|
6
|
+
}): void;
|
|
7
|
+
/**
|
|
8
|
+
* Size the adapter host (React/Vue div or custom element) so percentage widths
|
|
9
|
+
* don't collapse when the host is a shrink-wrapped flex child. Root fills the host
|
|
10
|
+
* unless `rootFillsHost` is false (e.g. PhotoAlbum sizes itself via CSS variables).
|
|
11
|
+
*/
|
|
12
|
+
export declare function applyBlockHostBox(container: HTMLElement, root: HTMLElement, size: {
|
|
13
|
+
width?: number | string;
|
|
14
|
+
height?: number | string;
|
|
15
|
+
rootFillsHost?: boolean;
|
|
16
|
+
}): void;
|
|
17
|
+
/** Toggle empty-state nodes; author `display:` rules must not override `[hidden]`. */
|
|
18
|
+
export declare function setHidden(el: HTMLElement, hidden: boolean): void;
|
|
19
|
+
/** Stable JSON key for options, ignoring functions (callbacks). */
|
|
20
|
+
export declare function optionsFingerprint(value: unknown): string;
|
|
21
|
+
/** True when non-function fields differ (shallow). */
|
|
22
|
+
export declare function optionsVisualChanged(prev: Record<string, unknown>, next: Record<string, unknown>): boolean;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export { clamp, lerp } from './math';
|
|
2
|
+
export { bindVisibilityPause } from './visibility';
|
|
3
|
+
export { prefersReducedMotion, bindPrefersReducedMotion } from './reduced-motion';
|
|
4
|
+
export { observeElementSize } from './size';
|
|
5
|
+
export type { ElementSize } from './size';
|
|
6
|
+
export { getRelativePointerPosition } from './pointer';
|
|
7
|
+
export type { PointerPosition } from './pointer';
|
|
8
|
+
export type { PhotoItem, PhotoIndexChangeHandler, PhotoFaceChangeHandler } from './photo';
|
|
9
|
+
export { resolveCanvasBoxSize } from './canvas-box';
|
|
10
|
+
export type { CanvasBoxSizeInput, CanvasBoxSize } from './canvas-box';
|
|
11
|
+
export { applyCanvasHostBox, applyBlockHostBox, setHidden, optionsFingerprint, optionsVisualChanged, } from './host-layout';
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/** 图片预览组件通用照片项 */
|
|
2
|
+
export interface PhotoItem {
|
|
3
|
+
/** 图片地址 */
|
|
4
|
+
src: string;
|
|
5
|
+
/** 图片替代文本 */
|
|
6
|
+
alt?: string;
|
|
7
|
+
/** 照片标题 */
|
|
8
|
+
title?: string;
|
|
9
|
+
/** 照片说明 */
|
|
10
|
+
description?: string;
|
|
11
|
+
}
|
|
12
|
+
/** 线性浏览:当前索引变化(相册 / 胶卷 / 晾绳等) */
|
|
13
|
+
export type PhotoIndexChangeHandler<T = PhotoItem> = (index: number, photo: T) => void;
|
|
14
|
+
/** 环形浏览:正对镜头的面变化(走马灯 / 旋转木马等) */
|
|
15
|
+
export type PhotoFaceChangeHandler<T = PhotoItem> = (index: number, photo?: T) => void;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export interface PointerPosition {
|
|
2
|
+
x: number;
|
|
3
|
+
y: number;
|
|
4
|
+
}
|
|
5
|
+
type PointLike = {
|
|
6
|
+
clientX: number;
|
|
7
|
+
clientY: number;
|
|
8
|
+
};
|
|
9
|
+
/** 相对元素左上角的指针坐标(兼容 mouse / touch / pointer) */
|
|
10
|
+
export declare const getRelativePointerPosition: (element: Element, event: PointLike | {
|
|
11
|
+
touches?: ArrayLike<PointLike>;
|
|
12
|
+
changedTouches?: ArrayLike<PointLike>;
|
|
13
|
+
}) => PointerPosition | null;
|
|
14
|
+
export {};
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { CSSProperties, RefObject } from 'react';
|
|
2
|
+
import { ElementSize } from '../size';
|
|
3
|
+
export type { CanvasBoxSize, CanvasBoxSizeInput } from '../canvas-box';
|
|
4
|
+
export { resolveCanvasBoxSize } from '../canvas-box';
|
|
5
|
+
export interface UseElementSizeOptions {
|
|
6
|
+
/** 为 false 时不观察 */
|
|
7
|
+
enabled?: boolean;
|
|
8
|
+
}
|
|
9
|
+
/** 观察元素尺寸(clientWidth / clientHeight) */
|
|
10
|
+
export declare const useElementSize: <T extends Element>(ref: RefObject<T | null>, options?: UseElementSizeOptions) => ElementSize;
|
|
11
|
+
export interface UseCanvasBoxOptions {
|
|
12
|
+
/** 为 true 时铺满父容器(父级需有明确高度) */
|
|
13
|
+
fill?: boolean;
|
|
14
|
+
width?: number;
|
|
15
|
+
height?: number;
|
|
16
|
+
defaultWidth: number;
|
|
17
|
+
defaultHeight: number;
|
|
18
|
+
}
|
|
19
|
+
export interface UseCanvasBoxResult {
|
|
20
|
+
hostRef: RefObject<HTMLDivElement | null>;
|
|
21
|
+
width: number;
|
|
22
|
+
height: number;
|
|
23
|
+
/** 挂到宿主容器的样式(fill 时为 100%×100%) */
|
|
24
|
+
hostStyle: CSSProperties;
|
|
25
|
+
fill: boolean;
|
|
26
|
+
/** 是否处于 fill 模式且尚未量到有效尺寸 */
|
|
27
|
+
pending: boolean;
|
|
28
|
+
}
|
|
29
|
+
/** 画布/效果宿主尺寸:fill 时跟随父容器,否则用显式宽高或默认值 */
|
|
30
|
+
export declare const useCanvasBox: ({ fill, width: widthProp, height: heightProp, defaultWidth, defaultHeight, }: UseCanvasBoxOptions) => UseCanvasBoxResult;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from '../../../src/components/springMass/core';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from '../../../src/components/springMass/element';
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export interface SpringMassOptions {
|
|
2
|
+
width?: number;
|
|
3
|
+
height?: number;
|
|
4
|
+
/** 横向质点数量 */
|
|
5
|
+
cols?: number;
|
|
6
|
+
/** 纵向质点数量 */
|
|
7
|
+
rows?: number;
|
|
8
|
+
/** 弹簧刚度 0~1 */
|
|
9
|
+
stiffness?: number;
|
|
10
|
+
/** 速度阻尼 0~1 */
|
|
11
|
+
damping?: number;
|
|
12
|
+
/** 主色 */
|
|
13
|
+
color?: string;
|
|
14
|
+
/** 操作提示 */
|
|
15
|
+
hint?: string;
|
|
16
|
+
}
|
|
17
|
+
export interface SpringMassController {
|
|
18
|
+
update(options: Partial<SpringMassOptions>): void;
|
|
19
|
+
destroy(): void;
|
|
20
|
+
}
|
|
21
|
+
export type SpringMassProps = SpringMassOptions;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
declare const TAG = "cos-spring-mass";
|
|
2
|
+
declare class CosSpringMassElement extends HTMLElement {
|
|
3
|
+
private ctrl;
|
|
4
|
+
static get observedAttributes(): string[];
|
|
5
|
+
connectedCallback(): void;
|
|
6
|
+
disconnectedCallback(): void;
|
|
7
|
+
attributeChangedCallback(): void;
|
|
8
|
+
}
|
|
9
|
+
export { CosSpringMassElement, TAG };
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/// <reference types="vite/client" />
|
|
2
|
+
|
|
3
|
+
declare module '*.vue' {
|
|
4
|
+
import type { DefineComponent } from 'vue';
|
|
5
|
+
const component: DefineComponent<object, object, unknown>;
|
|
6
|
+
export default component;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
declare module '*.module.less' {
|
|
10
|
+
const classes: { readonly [key: string]: string };
|
|
11
|
+
export default classes;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
declare module '*.less' {
|
|
15
|
+
const classes: { readonly [key: string]: string };
|
|
16
|
+
export default classes;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
declare module '*.png' {
|
|
20
|
+
const src: string;
|
|
21
|
+
export default src;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
declare module '*.jpg' {
|
|
25
|
+
const src: string;
|
|
26
|
+
export default src;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
declare module '*.mp4' {
|
|
30
|
+
const src: string;
|
|
31
|
+
export default src;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
declare module '*.mp3' {
|
|
35
|
+
const src: string;
|
|
36
|
+
export default src;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
declare module '*.svg' {
|
|
40
|
+
import type { FC, SVGProps } from 'react';
|
|
41
|
+
|
|
42
|
+
export const ReactComponent: FC<SVGProps<SVGSVGElement>>;
|
|
43
|
+
const src: string;
|
|
44
|
+
export default src;
|
|
45
|
+
}
|
package/dist/vue/vue.css
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
.cos-spring-mass{background:linear-gradient(#0f172a,#020617);border:1px solid #94a3b824;border-radius:18px;position:relative;overflow:hidden;box-shadow:0 18px 42px #0206174d}.cos-spring-mass__canvas{cursor:grab;display:block}.cos-spring-mass__canvas:active{cursor:grabbing}.cos-spring-mass__hint{color:#e2e8f0d9;letter-spacing:.06em;-webkit-backdrop-filter:blur(10px);backdrop-filter:blur(10px);background:#0f172a73;border:1px solid #ffffff14;border-radius:999px;padding:6px 10px;font-size:12px;position:absolute;bottom:12px;right:14px}
|
package/package.json
CHANGED
|
@@ -1,20 +1,38 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cos-design/spring-mass",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "4.0.0",
|
|
4
4
|
"description": "SpringMass component from cos-design",
|
|
5
5
|
"type": "module",
|
|
6
|
-
"main": "./dist/index.cjs",
|
|
7
|
-
"module": "./dist/index.js",
|
|
8
|
-
"types": "./dist/index.d.ts",
|
|
6
|
+
"main": "./dist/react/index.cjs",
|
|
7
|
+
"module": "./dist/react/index.js",
|
|
8
|
+
"types": "./dist/react/index.d.ts",
|
|
9
9
|
"exports": {
|
|
10
10
|
".": {
|
|
11
11
|
"import": {
|
|
12
|
-
"types": "./dist/index.d.ts",
|
|
13
|
-
"default": "./dist/index.js"
|
|
12
|
+
"types": "./dist/react/index.d.ts",
|
|
13
|
+
"default": "./dist/react/index.js"
|
|
14
14
|
},
|
|
15
15
|
"require": {
|
|
16
|
-
"types": "./dist/index.d.ts",
|
|
17
|
-
"default": "./dist/index.cjs"
|
|
16
|
+
"types": "./dist/react/index.d.ts",
|
|
17
|
+
"default": "./dist/react/index.cjs"
|
|
18
|
+
}
|
|
19
|
+
},
|
|
20
|
+
"./vue": {
|
|
21
|
+
"import": {
|
|
22
|
+
"types": "./dist/vue/index.d.ts",
|
|
23
|
+
"default": "./dist/vue/index.js"
|
|
24
|
+
}
|
|
25
|
+
},
|
|
26
|
+
"./core": {
|
|
27
|
+
"import": {
|
|
28
|
+
"types": "./dist/core/index.d.ts",
|
|
29
|
+
"default": "./dist/core/index.js"
|
|
30
|
+
}
|
|
31
|
+
},
|
|
32
|
+
"./element": {
|
|
33
|
+
"import": {
|
|
34
|
+
"types": "./dist/element/index.d.ts",
|
|
35
|
+
"default": "./dist/element/index.js"
|
|
18
36
|
}
|
|
19
37
|
}
|
|
20
38
|
},
|
|
@@ -24,11 +42,13 @@
|
|
|
24
42
|
],
|
|
25
43
|
"sideEffects": [
|
|
26
44
|
"**/*.css",
|
|
27
|
-
"**/*.less"
|
|
45
|
+
"**/*.less",
|
|
46
|
+
"./dist/element/index.js"
|
|
28
47
|
],
|
|
29
48
|
"keywords": [
|
|
30
49
|
"cos-design",
|
|
31
50
|
"react",
|
|
51
|
+
"vue",
|
|
32
52
|
"springMass",
|
|
33
53
|
"SpringMass"
|
|
34
54
|
],
|
|
@@ -45,13 +65,25 @@
|
|
|
45
65
|
"license": "MIT",
|
|
46
66
|
"peerDependencies": {
|
|
47
67
|
"react": ">=18.0.0",
|
|
48
|
-
"react-dom": ">=18.0.0"
|
|
68
|
+
"react-dom": ">=18.0.0",
|
|
69
|
+
"vue": ">=3.4.0"
|
|
49
70
|
},
|
|
50
71
|
"publishConfig": {
|
|
51
72
|
"access": "public",
|
|
52
73
|
"registry": "https://registry.npmjs.org"
|
|
53
74
|
},
|
|
75
|
+
"peerDependenciesMeta": {
|
|
76
|
+
"react": {
|
|
77
|
+
"optional": true
|
|
78
|
+
},
|
|
79
|
+
"react-dom": {
|
|
80
|
+
"optional": true
|
|
81
|
+
},
|
|
82
|
+
"vue": {
|
|
83
|
+
"optional": true
|
|
84
|
+
}
|
|
85
|
+
},
|
|
54
86
|
"dependencies": {
|
|
55
|
-
"@cos-design/shared": "
|
|
87
|
+
"@cos-design/shared": "4.0.0"
|
|
56
88
|
}
|
|
57
89
|
}
|
package/dist/index.cjs
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:`Module`}});require('./index.css');var e=Object.create,t=Object.defineProperty,n=Object.getOwnPropertyDescriptor,r=Object.getOwnPropertyNames,i=Object.getPrototypeOf,a=Object.prototype.hasOwnProperty,o=(e,i,o,s)=>{if(i&&typeof i==`object`||typeof i==`function`)for(var c=r(i),l=0,u=c.length,d;l<u;l++)d=c[l],!a.call(e,d)&&d!==o&&t(e,d,{get:(e=>i[e]).bind(null,d),enumerable:!(s=n(i,d))||s.enumerable});return e},s=(n,r,a)=>(a=n==null?{}:e(i(n)),o(r||!n||!n.__esModule?t(a,`default`,{value:n,enumerable:!0}):a,n));let c=require("react");c=s(c,1);let l=require("@cos-design/shared"),u=require("react/jsx-runtime");var d={springMass:`cos-springMass-cYEt6`,canvas:`cos-canvas-mwL9l`,hint:`cos-hint-TKLm3`},f=(e,t,n,r)=>{let i=Math.max(3,Math.min(n,10)),a=Math.max(3,Math.min(r,8)),o=e*.16,s=t*.18,c=(e-o*2)/(i-1),l=(t-s*2)/(a-1),u=[];for(let e=0;e<a;e++)for(let t=0;t<i;t++){let n=e===0&&(t===0||t===i-1)||e===a-1&&(t===0||t===i-1);u.push({x:o+t*c,y:s+e*l,vx:0,vy:0,pinned:n})}let d=[],f=(e,t)=>t*i+e;for(let e=0;e<a;e++)for(let t=0;t<i;t++)if(t<i-1&&d.push({a:f(t,e),b:f(t+1,e),rest:c}),e<a-1&&d.push({a:f(t,e),b:f(t,e+1),rest:l}),t<i-1&&e<a-1){let n=Math.hypot(c,l);d.push({a:f(t,e),b:f(t+1,e+1),rest:n}),d.push({a:f(t+1,e),b:f(t,e+1),rest:n})}return{nodes:u,springs:d,cols:i,rows:a}},p=({width:e=560,height:t=400,cols:n=6,rows:r=5,stiffness:i=.22,damping:a=.9,color:o=`#a78bfa`})=>{let s=(0,c.useRef)(null),p=(0,c.useRef)(f(e,t,n,r)),m=(0,c.useRef)(null),h=(0,c.useRef)({x:0,y:0});(0,c.useEffect)(()=>{p.current=f(e,t,n,r),m.current=null},[n,t,r,e]),(0,c.useEffect)(()=>{let n=s.current;if(!n)return;let r=window.devicePixelRatio||1;n.width=e*r,n.height=t*r;let c=n.getContext(`2d`);if(!c)return;c.setTransform(r,0,0,r,0,0);let u=0,d=document.hidden,f=(0,l.bindVisibilityPause)(e=>{d=e}),g=Math.max(.04,Math.min(i,.8)),_=()=>{let{nodes:n,springs:r}=p.current,i=m.current;if(i){let e=n[i.index];e.x=h.current.x+i.ox,e.y=h.current.y+i.oy,e.vx=0,e.vy=0}for(let e of r){let t=n[e.a],r=n[e.b],a=r.x-t.x,o=r.y-t.y,s=Math.hypot(a,o)||.001,c=(s-e.rest)*g,l=a/s*c,u=o/s*c,d=i?.index===e.a,f=i?.index===e.b;!t.pinned&&!d&&(t.vx+=l,t.vy+=u),!r.pinned&&!f&&(r.vx-=l,r.vy-=u)}for(let r=0;r<n.length;r++){let o=n[r];if(o.pinned||i?.index===r){o.vx=0,o.vy=0;continue}o.vx*=a,o.vy*=a,o.x+=o.vx,o.y+=o.vy,o.x<10?(o.x=10,o.vx*=-.4):o.x>e-10&&(o.x=e-10,o.vx*=-.4),o.y<10?(o.y=10,o.vy*=-.4):o.y>t-10&&(o.y=t-10,o.vy*=-.4)}},v=()=>{let{nodes:n,springs:r}=p.current;c.fillStyle=`#0f172a`,c.fillRect(0,0,e,t);for(let i of r){let r=n[i.a],a=n[i.b],s=Math.hypot(a.x-r.x,a.y-r.y)||1,l=Math.min(1,Math.abs(s-i.rest)/i.rest);c.beginPath(),c.moveTo(r.x,r.y),c.lineTo(a.x,a.y),c.strokeStyle=o,c.globalAlpha=.22+l*.55,c.lineWidth=i.rest>Math.max(e,t)*.2?1:1.6,c.stroke(),c.globalAlpha=1}n.forEach((e,t)=>{c.beginPath(),c.arc(e.x,e.y,e.pinned?6:8,0,Math.PI*2),c.fillStyle=e.pinned?`#64748b`:o,c.fill(),c.strokeStyle=`rgb(255 255 255 / 28%)`,c.lineWidth=1,c.stroke(),m.current?.index===t&&(c.beginPath(),c.arc(e.x,e.y,13,0,Math.PI*2),c.strokeStyle=o,c.globalAlpha=.45,c.lineWidth=2,c.stroke(),c.globalAlpha=1)})},y=()=>{u=requestAnimationFrame(y),!d&&(_(),v())};return u=requestAnimationFrame(y),()=>{cancelAnimationFrame(u),f()}},[o,a,t,i,e]);let g=(e,t)=>{let{nodes:n}=p.current,r=-1,i=20;return n.forEach((n,a)=>{if(n.pinned)return;let o=Math.hypot(n.x-e,n.y-t);o<i&&(i=o,r=a)}),r},_=(e,t,n)=>{let r=e-n.left,i=t-n.top;h.current={x:r,y:i};let a=g(r,i);if(a<0)return;let o=p.current.nodes[a];m.current={index:a,ox:o.x-r,oy:o.y-i}},v=(e,t,n)=>{h.current={x:e-n.left,y:t-n.top}},y=()=>{m.current=null};return(0,u.jsxs)(`div`,{className:d.springMass,style:{width:e,height:t},children:[(0,u.jsx)(`canvas`,{ref:s,className:d.canvas,style:{width:e,height:t},onMouseDown:e=>_(e.clientX,e.clientY,e.currentTarget.getBoundingClientRect()),onMouseMove:e=>v(e.clientX,e.clientY,e.currentTarget.getBoundingClientRect()),onMouseUp:y,onMouseLeave:y,onTouchStart:e=>{let t=e.touches[0];t&&_(t.clientX,t.clientY,e.currentTarget.getBoundingClientRect())},onTouchMove:e=>{let t=e.touches[0];t&&v(t.clientX,t.clientY,e.currentTarget.getBoundingClientRect())},onTouchEnd:y}),(0,u.jsx)(`div`,{className:d.hint,children:`拖拽网格质点,观察弹簧回弹`})]})};exports.SpringMass=p,exports.default=p;
|
package/dist/index.css
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
.cos-springMass-cYEt6{background:linear-gradient(#0f172a,#020617);border:1px solid #94a3b824;border-radius:18px;position:relative;overflow:hidden;box-shadow:0 18px 42px #0206174d}.cos-canvas-mwL9l{cursor:grab;display:block}.cos-canvas-mwL9l:active{cursor:grabbing}.cos-hint-TKLm3{color:#e2e8f0d9;letter-spacing:.06em;-webkit-backdrop-filter:blur(10px);backdrop-filter:blur(10px);background:#0f172a73;border:1px solid #ffffff14;border-radius:999px;padding:6px 10px;font-size:12px;position:absolute;bottom:12px;right:14px}
|
package/dist/index.d.ts
DELETED