@cos-design/slot-machine 3.5.7 → 4.1.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 +115 -0
- package/dist/core/packages/shared/src/canvas-box.d.ts +18 -0
- package/dist/core/packages/shared/src/element-options.d.ts +19 -0
- package/dist/core/packages/shared/src/host-layout.d.ts +22 -0
- package/dist/core/packages/shared/src/index.d.ts +13 -0
- package/dist/{packages/shared/src/use-element-size.d.ts → core/packages/shared/src/react/index.d.ts} +4 -19
- package/dist/core/packages/slotMachine/src/core.d.ts +1 -0
- package/dist/core/packages/slotMachine/src/element.d.ts +1 -0
- package/dist/core/packages/slotMachine/src/react.d.ts +3 -0
- package/dist/core/packages/slotMachine/src/vue.d.ts +2 -0
- package/dist/core/src/components/slotMachine/core/engine.d.ts +2 -0
- package/dist/core/src/components/slotMachine/core/index.d.ts +2 -0
- package/dist/{src/components/slotMachine/index.d.ts → core/src/components/slotMachine/core/types.d.ts} +6 -4
- package/dist/core/src/components/slotMachine/element/index.d.ts +18 -0
- package/dist/core/src/components/slotMachine/index.d.ts +3 -0
- package/dist/core/src/components/slotMachine/react/index.d.ts +5 -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 +167 -0
- package/dist/element/packages/shared/src/canvas-box.d.ts +18 -0
- package/dist/element/packages/shared/src/element-options.d.ts +19 -0
- package/dist/element/packages/shared/src/host-layout.d.ts +22 -0
- package/dist/element/packages/shared/src/index.d.ts +13 -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/slotMachine/src/core.d.ts +1 -0
- package/dist/element/packages/slotMachine/src/element.d.ts +1 -0
- package/dist/element/packages/slotMachine/src/index.d.ts +3 -0
- package/dist/element/packages/slotMachine/src/react.d.ts +3 -0
- package/dist/element/packages/slotMachine/src/vue.d.ts +2 -0
- package/dist/element/src/components/slotMachine/core/engine.d.ts +2 -0
- package/dist/element/src/components/slotMachine/core/index.d.ts +2 -0
- package/dist/element/src/components/slotMachine/core/types.d.ts +31 -0
- package/dist/element/src/components/slotMachine/element/index.d.ts +18 -0
- package/dist/element/src/components/slotMachine/index.d.ts +3 -0
- package/dist/element/src/components/slotMachine/react/index.d.ts +5 -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 +141 -0
- package/dist/react/packages/shared/src/canvas-box.d.ts +18 -0
- package/dist/react/packages/shared/src/element-options.d.ts +19 -0
- package/dist/react/packages/shared/src/host-layout.d.ts +22 -0
- package/dist/react/packages/shared/src/index.d.ts +13 -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/slotMachine/src/core.d.ts +1 -0
- package/dist/react/packages/slotMachine/src/element.d.ts +1 -0
- package/dist/react/packages/slotMachine/src/index.d.ts +3 -0
- package/dist/react/packages/slotMachine/src/react.d.ts +3 -0
- package/dist/react/packages/slotMachine/src/vue.d.ts +2 -0
- package/dist/react/react.css +1 -0
- package/dist/react/src/components/slotMachine/core/engine.d.ts +2 -0
- package/dist/react/src/components/slotMachine/core/index.d.ts +2 -0
- package/dist/react/src/components/slotMachine/core/types.d.ts +31 -0
- package/dist/react/src/components/slotMachine/element/index.d.ts +18 -0
- package/dist/react/src/components/slotMachine/index.d.ts +3 -0
- package/dist/react/src/components/slotMachine/react/index.d.ts +5 -0
- package/dist/react/src/vite-env.d.ts +45 -0
- package/dist/vue/index.d.ts +2 -0
- package/dist/vue/index.js +151 -0
- package/dist/vue/packages/shared/src/canvas-box.d.ts +18 -0
- package/dist/vue/packages/shared/src/element-options.d.ts +19 -0
- package/dist/vue/packages/shared/src/host-layout.d.ts +22 -0
- package/dist/vue/packages/shared/src/index.d.ts +13 -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/slotMachine/src/core.d.ts +1 -0
- package/dist/vue/packages/slotMachine/src/element.d.ts +1 -0
- package/dist/vue/packages/slotMachine/src/index.d.ts +3 -0
- package/dist/vue/packages/slotMachine/src/react.d.ts +3 -0
- package/dist/vue/packages/slotMachine/src/vue.d.ts +2 -0
- package/dist/vue/src/components/slotMachine/core/engine.d.ts +2 -0
- package/dist/vue/src/components/slotMachine/core/index.d.ts +2 -0
- package/dist/vue/src/components/slotMachine/core/types.d.ts +31 -0
- package/dist/vue/src/components/slotMachine/element/index.d.ts +18 -0
- package/dist/vue/src/components/slotMachine/index.d.ts +3 -0
- package/dist/vue/src/components/slotMachine/react/index.d.ts +5 -0
- package/dist/vue/src/vite-env.d.ts +45 -0
- package/dist/vue/vue.css +1 -0
- package/package.json +44 -12
- package/dist/index.cjs +0 -1
- package/dist/index.css +0 -1
- package/dist/index.d.ts +0 -3
- package/dist/index.js +0 -130
- package/dist/packages/shared/src/index.d.ts +0 -10
- /package/dist/{packages → core/packages}/shared/src/math.d.ts +0 -0
- /package/dist/{packages → core/packages}/shared/src/photo.d.ts +0 -0
- /package/dist/{packages → core/packages}/shared/src/pointer.d.ts +0 -0
- /package/dist/{packages → core/packages}/shared/src/reduced-motion.d.ts +0 -0
- /package/dist/{packages → core/packages}/shared/src/size.d.ts +0 -0
- /package/dist/{packages → core/packages}/shared/src/visibility.d.ts +0 -0
- /package/dist/{packages → core/packages}/slotMachine/src/index.d.ts +0 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './src/components/slotMachine/core';
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import { prefersReducedMotion as e } from "@cos-design/shared";
|
|
2
|
+
//#region src/components/slotMachine/core/engine.ts
|
|
3
|
+
var t = "cos-slot-machine", n = [
|
|
4
|
+
"🍒",
|
|
5
|
+
"🍋",
|
|
6
|
+
"🍊",
|
|
7
|
+
"🍇",
|
|
8
|
+
"⭐",
|
|
9
|
+
"7️⃣"
|
|
10
|
+
], r = 30, i = 8, a = [
|
|
11
|
+
0,
|
|
12
|
+
350,
|
|
13
|
+
700
|
|
14
|
+
], o = 80, s = 3, c = (e) => 1 - (1 - e) ** 3, l = (e, t) => {
|
|
15
|
+
if (t === void 0) return Math.floor(Math.random() * e.length);
|
|
16
|
+
let n = e.indexOf(t);
|
|
17
|
+
return n >= 0 ? n : Math.floor(Math.random() * e.length);
|
|
18
|
+
};
|
|
19
|
+
function u(u, d = {}) {
|
|
20
|
+
let f = {
|
|
21
|
+
symbols: n,
|
|
22
|
+
spinDuration: 3e3,
|
|
23
|
+
spinningText: "旋转中...",
|
|
24
|
+
jackpotText: "🎰 大奖!",
|
|
25
|
+
resultPrefix: "结果:",
|
|
26
|
+
...d
|
|
27
|
+
}, p = !1, m = !1, h = [
|
|
28
|
+
0,
|
|
29
|
+
0,
|
|
30
|
+
0
|
|
31
|
+
], g = [], _ = 0, v = null, y = document.createElement("div"), b = document.createElement("div");
|
|
32
|
+
b.className = `${t}__machine`;
|
|
33
|
+
let x = [], S = [];
|
|
34
|
+
for (let e = 0; e < 3; e++) {
|
|
35
|
+
let e = document.createElement("div");
|
|
36
|
+
e.className = `${t}__reel`;
|
|
37
|
+
let n = document.createElement("div");
|
|
38
|
+
n.className = `${t}__strip`;
|
|
39
|
+
let r = document.createElement("div");
|
|
40
|
+
r.className = `${t}__mask`, e.append(n, r), b.appendChild(e), x.push(e), S.push(n);
|
|
41
|
+
}
|
|
42
|
+
let C = document.createElement("button");
|
|
43
|
+
C.type = "button", C.className = `${t}__spin-btn`, C.dataset.testid = "slot-machine-spin";
|
|
44
|
+
let w = document.createElement("p");
|
|
45
|
+
w.className = `${t}__result`, w.hidden = !0, y.append(b, C, w), u.appendChild(y);
|
|
46
|
+
let T = () => f.symbols?.length ? f.symbols : n, E = () => T().length * o, D = () => Math.floor(r / 2) * E(), O = (e, t) => -(e - 1 + t * T().length) * o, k = () => f.buttonText ?? f.startText ?? "开始", A = () => {
|
|
47
|
+
let e = T(), n = [];
|
|
48
|
+
for (let t = 0; t < r; t++) n.push(...e);
|
|
49
|
+
for (let e of S) {
|
|
50
|
+
e.replaceChildren();
|
|
51
|
+
for (let r of n) {
|
|
52
|
+
let n = document.createElement("div");
|
|
53
|
+
n.className = `${t}__item`, n.style.height = `${o}px`, n.textContent = r, e.appendChild(n);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
let i = o * s;
|
|
57
|
+
for (let e of x) e.style.height = `${i}px`;
|
|
58
|
+
}, j = () => {
|
|
59
|
+
S.forEach((e, t) => {
|
|
60
|
+
e.style.transform = `translateY(${h[t]}px)`;
|
|
61
|
+
});
|
|
62
|
+
}, M = () => {
|
|
63
|
+
y.className = t, C.disabled = m, C.setAttribute("aria-busy", String(m)), C.textContent = m ? f.spinningText ?? "旋转中..." : k(), g.length > 0 && !m ? (w.hidden = !1, w.textContent = g[0] === g[1] && g[1] === g[2] ? f.jackpotText ?? "🎰 大奖!" : `${f.resultPrefix ?? "结果:"} ${g.join(" ")}`) : (w.hidden = !0, w.textContent = "");
|
|
64
|
+
}, N = (t) => {
|
|
65
|
+
let n = T();
|
|
66
|
+
if (m || n.length === 0 || p) return;
|
|
67
|
+
m = !0, g = [], M();
|
|
68
|
+
let r = t ?? f.targetResults, s = [
|
|
69
|
+
0,
|
|
70
|
+
1,
|
|
71
|
+
2
|
|
72
|
+
].map((e) => l(n, r?.[e])), u = [...h], d = n.length * o, y = s.map((e, t) => {
|
|
73
|
+
let n = u[t], r = i * d, a = Math.ceil((-n + r) / d) + 1, o = O(e, a);
|
|
74
|
+
for (; n - o < r;) a += 1, o = O(e, a);
|
|
75
|
+
return o;
|
|
76
|
+
}), b = Math.max(f.spinDuration ?? 3e3, 16), x = performance.now(), S = { cancelled: !1 };
|
|
77
|
+
v = S;
|
|
78
|
+
let C = () => {
|
|
79
|
+
if (S.cancelled || p) return;
|
|
80
|
+
let e = s.map((e) => n[e]);
|
|
81
|
+
h = s.map((e) => O(e, Math.round(D() / d))), g = e, m = !1, j(), M(), f.onSpinEnd?.(e);
|
|
82
|
+
};
|
|
83
|
+
if (e()) {
|
|
84
|
+
C();
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
let w = (e) => {
|
|
88
|
+
S.cancelled || p || (h = u.map((t, n) => {
|
|
89
|
+
let r = e - x - a[n];
|
|
90
|
+
if (r < 0) return t;
|
|
91
|
+
let i = Math.min(r / b, 1);
|
|
92
|
+
return t + (y[n] - t) * c(i);
|
|
93
|
+
}), j(), a.every((t) => e - x - t >= b) ? C() : _ = requestAnimationFrame(w));
|
|
94
|
+
};
|
|
95
|
+
_ = requestAnimationFrame(w);
|
|
96
|
+
};
|
|
97
|
+
return C.addEventListener("click", () => N()), A(), j(), M(), {
|
|
98
|
+
update(e) {
|
|
99
|
+
let t = T().join("\0");
|
|
100
|
+
f = {
|
|
101
|
+
...f,
|
|
102
|
+
...e
|
|
103
|
+
}, T().join("\0") !== t && A(), M();
|
|
104
|
+
},
|
|
105
|
+
spin: N,
|
|
106
|
+
reset: () => {
|
|
107
|
+
m || (g = [], M());
|
|
108
|
+
},
|
|
109
|
+
destroy() {
|
|
110
|
+
p || (p = !0, v && (v.cancelled = !0), cancelAnimationFrame(_), y.remove());
|
|
111
|
+
}
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
//#endregion
|
|
115
|
+
export { u as createSlotMachine };
|
|
@@ -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,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Web Component attribute → Core options parsing (v4 Element adapters).
|
|
3
|
+
* Keeps behavior consistent across 91 components.
|
|
4
|
+
*/
|
|
5
|
+
export type BooleanAttrMode =
|
|
6
|
+
/** `options.key = el.hasAttribute(attr)` — always written */
|
|
7
|
+
'presence'
|
|
8
|
+
/** Only when attribute present: `raw !== 'false' && raw !== '0'` (omit when absent) */
|
|
9
|
+
| 'optional-true'
|
|
10
|
+
/** Only when attribute present; when absent leave `undefined` (controlled props like `flipped`) */
|
|
11
|
+
| 'optional-undefined';
|
|
12
|
+
export declare const camelToKebabAttr: (name: string) => string;
|
|
13
|
+
export declare function readStringAttr<T extends object>(options: T, el: HTMLElement, key: keyof T & string, attr?: string): void;
|
|
14
|
+
export declare function readNumberAttr<T extends object>(options: T, el: HTMLElement, key: keyof T & string, attr?: string): void;
|
|
15
|
+
export declare function readBooleanAttr<T extends object>(options: T, el: HTMLElement, key: keyof T & string, attr?: string, mode?: BooleanAttrMode): void;
|
|
16
|
+
export declare function readJsonAttr<T extends object, K extends keyof T & string>(options: T, el: HTMLElement, key: K, attr?: string): void;
|
|
17
|
+
/** Property setters on the custom element class (`_prizes`, `_items`, …) override attributes. */
|
|
18
|
+
export declare function readPropertyOverride<T extends object, K extends keyof T & string>(options: T, el: HTMLElement, key: K, backingKey: string): void;
|
|
19
|
+
export declare function optionCallbackDispatcher(el: HTMLElement, eventName: string): (...args: unknown[]) => void;
|
|
@@ -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,13 @@
|
|
|
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';
|
|
12
|
+
export type { BooleanAttrMode } from './element-options';
|
|
13
|
+
export { camelToKebabAttr, optionCallbackDispatcher, readBooleanAttr, readJsonAttr, readNumberAttr, readPropertyOverride, readStringAttr, } from './element-options';
|
package/dist/{packages/shared/src/use-element-size.d.ts → core/packages/shared/src/react/index.d.ts}
RENAMED
|
@@ -1,28 +1,13 @@
|
|
|
1
1
|
import { CSSProperties, RefObject } from 'react';
|
|
2
|
-
import { ElementSize } from '
|
|
2
|
+
import { ElementSize } from '../size';
|
|
3
|
+
export type { CanvasBoxSize, CanvasBoxSizeInput } from '../canvas-box';
|
|
4
|
+
export { resolveCanvasBoxSize } from '../canvas-box';
|
|
3
5
|
export interface UseElementSizeOptions {
|
|
4
6
|
/** 为 false 时不观察 */
|
|
5
7
|
enabled?: boolean;
|
|
6
8
|
}
|
|
7
9
|
/** 观察元素尺寸(clientWidth / clientHeight) */
|
|
8
10
|
export declare const useElementSize: <T extends Element>(ref: RefObject<T | null>, options?: UseElementSizeOptions) => ElementSize;
|
|
9
|
-
export interface CanvasBoxSizeInput {
|
|
10
|
-
/** 为 true 时跟随父容器尺寸(需父级有明确高度) */
|
|
11
|
-
fill?: boolean;
|
|
12
|
-
width?: number;
|
|
13
|
-
height?: number;
|
|
14
|
-
defaultWidth: number;
|
|
15
|
-
defaultHeight: number;
|
|
16
|
-
measured: ElementSize;
|
|
17
|
-
}
|
|
18
|
-
export interface CanvasBoxSize {
|
|
19
|
-
width: number;
|
|
20
|
-
height: number;
|
|
21
|
-
/** 是否处于 fill 模式且尚未量到有效尺寸 */
|
|
22
|
-
pending: boolean;
|
|
23
|
-
}
|
|
24
|
-
/** 根据 fill / 显式宽高 / 测量结果解析画布逻辑尺寸 */
|
|
25
|
-
export declare const resolveCanvasBoxSize: ({ fill, width, height, defaultWidth, defaultHeight, measured }: CanvasBoxSizeInput) => CanvasBoxSize;
|
|
26
11
|
export interface UseCanvasBoxOptions {
|
|
27
12
|
/** 为 true 时铺满父容器(父级需有明确高度) */
|
|
28
13
|
fill?: boolean;
|
|
@@ -42,4 +27,4 @@ export interface UseCanvasBoxResult {
|
|
|
42
27
|
pending: boolean;
|
|
43
28
|
}
|
|
44
29
|
/** 画布/效果宿主尺寸:fill 时跟随父容器,否则用显式宽高或默认值 */
|
|
45
|
-
export declare const useCanvasBox: ({ fill, width: widthProp, height: heightProp, defaultWidth, defaultHeight }: UseCanvasBoxOptions) => UseCanvasBoxResult;
|
|
30
|
+
export declare const useCanvasBox: ({ fill, width: widthProp, height: heightProp, defaultWidth, defaultHeight, }: UseCanvasBoxOptions) => UseCanvasBoxResult;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from '../../../src/components/slotMachine/core';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from '../../../src/components/slotMachine/element';
|
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
export interface SlotMachineProps {
|
|
1
|
+
export interface SlotMachineOptions {
|
|
3
2
|
/** 符号列表 */
|
|
4
3
|
symbols?: string[];
|
|
5
4
|
/** 单列旋转时长(毫秒),默认 3000;列之间仍有错峰停轮 */
|
|
@@ -25,5 +24,8 @@ export interface SlotMachineHandle {
|
|
|
25
24
|
/** 清除结果文案 */
|
|
26
25
|
reset: () => void;
|
|
27
26
|
}
|
|
28
|
-
|
|
29
|
-
|
|
27
|
+
export interface SlotMachineController extends SlotMachineHandle {
|
|
28
|
+
update(options: Partial<SlotMachineOptions>): void;
|
|
29
|
+
destroy(): void;
|
|
30
|
+
}
|
|
31
|
+
export type SlotMachineProps = SlotMachineOptions;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { SlotMachineOptions } from '../core';
|
|
2
|
+
declare const TAG = "cos-slot-machine";
|
|
3
|
+
declare class CosSlotMachineElement extends HTMLElement {
|
|
4
|
+
private ctrl;
|
|
5
|
+
_symbols?: SlotMachineOptions['symbols'];
|
|
6
|
+
get symbols(): SlotMachineOptions['symbols'] | undefined;
|
|
7
|
+
set symbols(value: SlotMachineOptions['symbols']);
|
|
8
|
+
_targetResults?: SlotMachineOptions['targetResults'];
|
|
9
|
+
get targetResults(): SlotMachineOptions['targetResults'] | undefined;
|
|
10
|
+
set targetResults(value: SlotMachineOptions['targetResults']);
|
|
11
|
+
static get observedAttributes(): string[];
|
|
12
|
+
connectedCallback(): void;
|
|
13
|
+
disconnectedCallback(): void;
|
|
14
|
+
attributeChangedCallback(): void;
|
|
15
|
+
spin(results?: string[]): void | undefined;
|
|
16
|
+
reset(): void | undefined;
|
|
17
|
+
}
|
|
18
|
+
export { CosSlotMachineElement, TAG };
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { SlotMachineOptions } from '../core';
|
|
2
|
+
import { SlotMachineHandle } from '../core/types';
|
|
3
|
+
export type { SlotMachineOptions, SlotMachineHandle } from '../core/types';
|
|
4
|
+
declare const SlotMachine: import('react').ForwardRefExoticComponent<SlotMachineOptions & import('react').RefAttributes<SlotMachineHandle>>;
|
|
5
|
+
export default SlotMachine;
|
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
/// <reference types="vite/client" />
|
|
2
2
|
|
|
3
|
+
declare module '*.vue' {
|
|
4
|
+
import type { DefineComponent } from 'vue';
|
|
5
|
+
const component: DefineComponent<object, object, unknown>;
|
|
6
|
+
export default component;
|
|
7
|
+
}
|
|
8
|
+
|
|
3
9
|
declare module '*.module.less' {
|
|
4
10
|
const classes: { readonly [key: string]: string };
|
|
5
11
|
export default classes;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
.cos-slot-machine{flex-direction:column;align-items:center;gap:20px;padding:24px;display:flex}.cos-slot-machine__machine{background:linear-gradient(#1e293b,#0f172a);border:3px solid #fbbf24;border-radius:16px;gap:8px;padding:16px;display:flex;box-shadow:0 8px 32px #0000004d,inset 0 2px #ffffff1a}.cos-slot-machine__reel{background:#fff;border-radius:8px;width:80px;position:relative;overflow:hidden}.cos-slot-machine__strip{transition:none}.cos-slot-machine__item{justify-content:center;align-items:center;font-size:36px;display:flex}.cos-slot-machine__mask{pointer-events:none;position:absolute;inset:0;box-shadow:inset 0 30px 20px #00000026,inset 0 -30px 20px #00000026}.cos-slot-machine__spin-btn{color:#78350f;cursor:pointer;background:linear-gradient(#fbbf24,#f59e0b);border:none;border-radius:50px;padding:12px 40px;font-size:16px;font-weight:700;box-shadow:0 4px 16px #fbbf2466}.cos-slot-machine__spin-btn:disabled{opacity:.6;cursor:not-allowed}.cos-slot-machine__result{color:#fbbf24;margin:0;font-size:16px;font-weight:600}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './src/components/slotMachine/element';
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
import { optionCallbackDispatcher as e, prefersReducedMotion as t, readJsonAttr as n, readNumberAttr as r, readPropertyOverride as i, readStringAttr as a } from "@cos-design/shared";
|
|
2
|
+
import './element.css';//#region src/components/slotMachine/core/engine.ts
|
|
3
|
+
var o = "cos-slot-machine", s = [
|
|
4
|
+
"🍒",
|
|
5
|
+
"🍋",
|
|
6
|
+
"🍊",
|
|
7
|
+
"🍇",
|
|
8
|
+
"⭐",
|
|
9
|
+
"7️⃣"
|
|
10
|
+
], c = 30, l = 8, u = [
|
|
11
|
+
0,
|
|
12
|
+
350,
|
|
13
|
+
700
|
|
14
|
+
], d = 80, f = 3, p = (e) => 1 - (1 - e) ** 3, m = (e, t) => {
|
|
15
|
+
if (t === void 0) return Math.floor(Math.random() * e.length);
|
|
16
|
+
let n = e.indexOf(t);
|
|
17
|
+
return n >= 0 ? n : Math.floor(Math.random() * e.length);
|
|
18
|
+
};
|
|
19
|
+
function h(e, n = {}) {
|
|
20
|
+
let r = {
|
|
21
|
+
symbols: s,
|
|
22
|
+
spinDuration: 3e3,
|
|
23
|
+
spinningText: "旋转中...",
|
|
24
|
+
jackpotText: "🎰 大奖!",
|
|
25
|
+
resultPrefix: "结果:",
|
|
26
|
+
...n
|
|
27
|
+
}, i = !1, a = !1, h = [
|
|
28
|
+
0,
|
|
29
|
+
0,
|
|
30
|
+
0
|
|
31
|
+
], g = [], _ = 0, v = null, y = document.createElement("div"), b = document.createElement("div");
|
|
32
|
+
b.className = `${o}__machine`;
|
|
33
|
+
let x = [], S = [];
|
|
34
|
+
for (let e = 0; e < 3; e++) {
|
|
35
|
+
let e = document.createElement("div");
|
|
36
|
+
e.className = `${o}__reel`;
|
|
37
|
+
let t = document.createElement("div");
|
|
38
|
+
t.className = `${o}__strip`;
|
|
39
|
+
let n = document.createElement("div");
|
|
40
|
+
n.className = `${o}__mask`, e.append(t, n), b.appendChild(e), x.push(e), S.push(t);
|
|
41
|
+
}
|
|
42
|
+
let C = document.createElement("button");
|
|
43
|
+
C.type = "button", C.className = `${o}__spin-btn`, C.dataset.testid = "slot-machine-spin";
|
|
44
|
+
let w = document.createElement("p");
|
|
45
|
+
w.className = `${o}__result`, w.hidden = !0, y.append(b, C, w), e.appendChild(y);
|
|
46
|
+
let T = () => r.symbols?.length ? r.symbols : s, E = () => T().length * d, D = () => Math.floor(c / 2) * E(), O = (e, t) => -(e - 1 + t * T().length) * d, k = () => r.buttonText ?? r.startText ?? "开始", A = () => {
|
|
47
|
+
let e = T(), t = [];
|
|
48
|
+
for (let n = 0; n < c; n++) t.push(...e);
|
|
49
|
+
for (let e of S) {
|
|
50
|
+
e.replaceChildren();
|
|
51
|
+
for (let n of t) {
|
|
52
|
+
let t = document.createElement("div");
|
|
53
|
+
t.className = `${o}__item`, t.style.height = `${d}px`, t.textContent = n, e.appendChild(t);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
let n = d * f;
|
|
57
|
+
for (let e of x) e.style.height = `${n}px`;
|
|
58
|
+
}, j = () => {
|
|
59
|
+
S.forEach((e, t) => {
|
|
60
|
+
e.style.transform = `translateY(${h[t]}px)`;
|
|
61
|
+
});
|
|
62
|
+
}, M = () => {
|
|
63
|
+
y.className = o, C.disabled = a, C.setAttribute("aria-busy", String(a)), C.textContent = a ? r.spinningText ?? "旋转中..." : k(), g.length > 0 && !a ? (w.hidden = !1, w.textContent = g[0] === g[1] && g[1] === g[2] ? r.jackpotText ?? "🎰 大奖!" : `${r.resultPrefix ?? "结果:"} ${g.join(" ")}`) : (w.hidden = !0, w.textContent = "");
|
|
64
|
+
}, N = (e) => {
|
|
65
|
+
let n = T();
|
|
66
|
+
if (a || n.length === 0 || i) return;
|
|
67
|
+
a = !0, g = [], M();
|
|
68
|
+
let o = e ?? r.targetResults, s = [
|
|
69
|
+
0,
|
|
70
|
+
1,
|
|
71
|
+
2
|
|
72
|
+
].map((e) => m(n, o?.[e])), c = [...h], f = n.length * d, y = s.map((e, t) => {
|
|
73
|
+
let n = c[t], r = l * f, i = Math.ceil((-n + r) / f) + 1, a = O(e, i);
|
|
74
|
+
for (; n - a < r;) i += 1, a = O(e, i);
|
|
75
|
+
return a;
|
|
76
|
+
}), b = Math.max(r.spinDuration ?? 3e3, 16), x = performance.now(), S = { cancelled: !1 };
|
|
77
|
+
v = S;
|
|
78
|
+
let C = () => {
|
|
79
|
+
if (S.cancelled || i) return;
|
|
80
|
+
let e = s.map((e) => n[e]);
|
|
81
|
+
h = s.map((e) => O(e, Math.round(D() / f))), g = e, a = !1, j(), M(), r.onSpinEnd?.(e);
|
|
82
|
+
};
|
|
83
|
+
if (t()) {
|
|
84
|
+
C();
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
let w = (e) => {
|
|
88
|
+
S.cancelled || i || (h = c.map((t, n) => {
|
|
89
|
+
let r = e - x - u[n];
|
|
90
|
+
if (r < 0) return t;
|
|
91
|
+
let i = Math.min(r / b, 1);
|
|
92
|
+
return t + (y[n] - t) * p(i);
|
|
93
|
+
}), j(), u.every((t) => e - x - t >= b) ? C() : _ = requestAnimationFrame(w));
|
|
94
|
+
};
|
|
95
|
+
_ = requestAnimationFrame(w);
|
|
96
|
+
};
|
|
97
|
+
return C.addEventListener("click", () => N()), A(), j(), M(), {
|
|
98
|
+
update(e) {
|
|
99
|
+
let t = T().join("\0");
|
|
100
|
+
r = {
|
|
101
|
+
...r,
|
|
102
|
+
...e
|
|
103
|
+
}, T().join("\0") !== t && A(), M();
|
|
104
|
+
},
|
|
105
|
+
spin: N,
|
|
106
|
+
reset: () => {
|
|
107
|
+
a || (g = [], M());
|
|
108
|
+
},
|
|
109
|
+
destroy() {
|
|
110
|
+
i || (i = !0, v && (v.cancelled = !0), cancelAnimationFrame(_), y.remove());
|
|
111
|
+
}
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
//#endregion
|
|
115
|
+
//#region src/components/slotMachine/element/index.ts
|
|
116
|
+
var g = "cos-slot-machine";
|
|
117
|
+
function _(t) {
|
|
118
|
+
let o = {};
|
|
119
|
+
return a(o, t, "startText", "start-text"), a(o, t, "buttonText", "button-text"), a(o, t, "spinningText", "spinning-text"), a(o, t, "jackpotText", "jackpot-text"), a(o, t, "resultPrefix", "result-prefix"), r(o, t, "spinDuration", "spin-duration"), n(o, t, "symbols", "symbols"), i(o, t, "symbols", "_symbols"), n(o, t, "targetResults", "target-results"), i(o, t, "targetResults", "_targetResults"), o.onSpinEnd = e(t, "spin-end"), o;
|
|
120
|
+
}
|
|
121
|
+
var v = class extends HTMLElement {
|
|
122
|
+
ctrl = null;
|
|
123
|
+
_symbols;
|
|
124
|
+
get symbols() {
|
|
125
|
+
return this._symbols;
|
|
126
|
+
}
|
|
127
|
+
set symbols(e) {
|
|
128
|
+
this._symbols = e, this.ctrl?.update(_(this));
|
|
129
|
+
}
|
|
130
|
+
_targetResults;
|
|
131
|
+
get targetResults() {
|
|
132
|
+
return this._targetResults;
|
|
133
|
+
}
|
|
134
|
+
set targetResults(e) {
|
|
135
|
+
this._targetResults = e, this.ctrl?.update(_(this));
|
|
136
|
+
}
|
|
137
|
+
static get observedAttributes() {
|
|
138
|
+
return [
|
|
139
|
+
"symbols",
|
|
140
|
+
"spin-duration",
|
|
141
|
+
"target-results",
|
|
142
|
+
"start-text",
|
|
143
|
+
"button-text",
|
|
144
|
+
"spinning-text",
|
|
145
|
+
"jackpot-text",
|
|
146
|
+
"result-prefix"
|
|
147
|
+
];
|
|
148
|
+
}
|
|
149
|
+
connectedCallback() {
|
|
150
|
+
this.ctrl = h(this, _(this));
|
|
151
|
+
}
|
|
152
|
+
disconnectedCallback() {
|
|
153
|
+
this.ctrl?.destroy(), this.ctrl = null;
|
|
154
|
+
}
|
|
155
|
+
attributeChangedCallback() {
|
|
156
|
+
this.ctrl?.update(_(this));
|
|
157
|
+
}
|
|
158
|
+
spin(e) {
|
|
159
|
+
return this.ctrl?.spin(e);
|
|
160
|
+
}
|
|
161
|
+
reset() {
|
|
162
|
+
return this.ctrl?.reset();
|
|
163
|
+
}
|
|
164
|
+
};
|
|
165
|
+
typeof customElements < "u" && !customElements.get("cos-slot-machine") && customElements.define(g, v);
|
|
166
|
+
//#endregion
|
|
167
|
+
export { v as CosSlotMachineElement, g as TAG };
|
|
@@ -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,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Web Component attribute → Core options parsing (v4 Element adapters).
|
|
3
|
+
* Keeps behavior consistent across 91 components.
|
|
4
|
+
*/
|
|
5
|
+
export type BooleanAttrMode =
|
|
6
|
+
/** `options.key = el.hasAttribute(attr)` — always written */
|
|
7
|
+
'presence'
|
|
8
|
+
/** Only when attribute present: `raw !== 'false' && raw !== '0'` (omit when absent) */
|
|
9
|
+
| 'optional-true'
|
|
10
|
+
/** Only when attribute present; when absent leave `undefined` (controlled props like `flipped`) */
|
|
11
|
+
| 'optional-undefined';
|
|
12
|
+
export declare const camelToKebabAttr: (name: string) => string;
|
|
13
|
+
export declare function readStringAttr<T extends object>(options: T, el: HTMLElement, key: keyof T & string, attr?: string): void;
|
|
14
|
+
export declare function readNumberAttr<T extends object>(options: T, el: HTMLElement, key: keyof T & string, attr?: string): void;
|
|
15
|
+
export declare function readBooleanAttr<T extends object>(options: T, el: HTMLElement, key: keyof T & string, attr?: string, mode?: BooleanAttrMode): void;
|
|
16
|
+
export declare function readJsonAttr<T extends object, K extends keyof T & string>(options: T, el: HTMLElement, key: K, attr?: string): void;
|
|
17
|
+
/** Property setters on the custom element class (`_prizes`, `_items`, …) override attributes. */
|
|
18
|
+
export declare function readPropertyOverride<T extends object, K extends keyof T & string>(options: T, el: HTMLElement, key: K, backingKey: string): void;
|
|
19
|
+
export declare function optionCallbackDispatcher(el: HTMLElement, eventName: string): (...args: unknown[]) => void;
|
|
@@ -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,13 @@
|
|
|
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';
|
|
12
|
+
export type { BooleanAttrMode } from './element-options';
|
|
13
|
+
export { camelToKebabAttr, optionCallbackDispatcher, readBooleanAttr, readJsonAttr, readNumberAttr, readPropertyOverride, readStringAttr, } from './element-options';
|
|
@@ -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;
|