@cos-design/slot-machine 3.5.5 → 3.5.7
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/index.cjs +1 -1
- package/dist/index.js +78 -72
- package/dist/packages/shared/src/index.d.ts +10 -0
- package/dist/packages/shared/src/math.d.ts +2 -0
- package/dist/packages/shared/src/photo.d.ts +15 -0
- package/dist/packages/shared/src/pointer.d.ts +14 -0
- package/dist/packages/shared/src/reduced-motion.d.ts +4 -0
- package/dist/packages/shared/src/size.d.ts +8 -0
- package/dist/packages/shared/src/use-element-size.d.ts +45 -0
- package/dist/packages/shared/src/visibility.d.ts +2 -0
- package/package.json +4 -1
package/dist/index.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
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("react/jsx-runtime");var
|
|
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={slotMachine:`cos-slotMachine-P6FyZ`,machine:`cos-machine-B066r`,reel:`cos-reel-eEpJ3`,strip:`cos-strip-L5Z-a`,item:`cos-item-WYJ6q`,mask:`cos-mask-jdrj3`,spinBtn:`cos-spinBtn-DvDE7`,result:`cos-result-dLN5b`},f=[`🍒`,`🍋`,`🍊`,`🍇`,`⭐`,`7️⃣`],p=30,m=8,h=[0,350,700],g=e=>1-(1-e)**3,_=(e,t)=>{if(t===void 0)return Math.floor(Math.random()*e.length);let n=e.indexOf(t);return n>=0?n:Math.floor(Math.random()*e.length)},v=(0,c.forwardRef)(({symbols:e=f,spinDuration:t=3e3,targetResults:n,onSpinEnd:r,startText:i,buttonText:a,spinningText:o=`旋转中...`,jackpotText:s=`🎰 大奖!`,resultPrefix:v=`结果:`},y)=>{let b=a??i??`开始`,[x,S]=(0,c.useState)(!1),[C,w]=(0,c.useState)([0,0,0]),[T,E]=(0,c.useState)([]),D=(0,c.useRef)(0),O=(0,c.useRef)(!1),k=(0,c.useRef)(C),A=(0,c.useRef)(r),j=(0,c.useRef)(e),M=(0,c.useRef)(n),N=(0,c.useRef)(null);(0,c.useEffect)(()=>{A.current=r},[r]),(0,c.useEffect)(()=>{j.current=e},[e]),(0,c.useEffect)(()=>{M.current=n},[n]),(0,c.useEffect)(()=>{O.current=x},[x]),(0,c.useEffect)(()=>{k.current=C},[C]);let P=e.length*80,F=Math.floor(p/2)*P,I=(0,c.useCallback)(()=>{let t=[];for(let n=0;n<p;n++)t.push(...e);return t},[e])(),L=(e,t)=>-(e-1+t*j.current.length)*80,R=(0,c.useCallback)(e=>{let n=j.current;if(O.current||n.length===0)return;S(!0),E([]);let r=e??M.current,i=[0,1,2].map(e=>_(n,r?.[e])),a=[...k.current],o=n.length*80,s=i.map((e,t)=>{let n=a[t],r=m*o,i=Math.ceil((-n+r)/o)+1,s=L(e,i);for(;n-s<r;)i+=1,s=L(e,i);return s}),c=Math.max(t,16),u=performance.now(),d={cancelled:!1};N.current=d;let f=()=>{if(d.cancelled)return;let e=i.map(e=>n[e]);w(i.map(e=>L(e,Math.round(F/o)))),E(e),S(!1),A.current?.(e)};if((0,l.prefersReducedMotion)()){f();return}let p=e=>{d.cancelled||(w(a.map((t,n)=>{let r=e-u-h[n];if(r<0)return t;let i=g(Math.min(r/c,1));return t+(s[n]-t)*i})),h.every(t=>e-u-t>=c)?f():D.current=requestAnimationFrame(p))};D.current=requestAnimationFrame(p)},[F,t]),z=(0,c.useCallback)(()=>{O.current||E([])},[]);return(0,c.useImperativeHandle)(y,()=>({spin:R,reset:z}),[R,z]),(0,c.useEffect)(()=>()=>{N.current&&(N.current.cancelled=!0),cancelAnimationFrame(D.current)},[]),(0,u.jsxs)(`div`,{className:d.slotMachine,children:[(0,u.jsx)(`div`,{className:d.machine,children:[0,1,2].map(e=>(0,u.jsxs)(`div`,{className:d.reel,style:{height:240},children:[(0,u.jsx)(`div`,{className:d.strip,style:{transform:`translateY(${C[e]}px)`},children:I.map((e,t)=>(0,u.jsx)(`div`,{className:d.item,style:{height:80},children:e},t))}),(0,u.jsx)(`div`,{className:d.mask})]},e))}),(0,u.jsx)(`button`,{type:`button`,className:d.spinBtn,"data-testid":`slot-machine-spin`,onClick:()=>R(),disabled:x,"aria-busy":x,children:x?o:b}),T.length>0&&!x&&(0,u.jsx)(`p`,{className:d.result,children:T[0]===T[1]&&T[1]===T[2]?s:`${v} ${T.join(` `)}`})]})});v.displayName=`SlotMachine`,exports.SlotMachine=v,exports.default=v;
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { forwardRef as e, useCallback as t, useEffect as n, useImperativeHandle as r, useRef as i, useState as a } from "react";
|
|
2
|
-
import {
|
|
3
|
-
import
|
|
2
|
+
import { prefersReducedMotion as o } from "@cos-design/shared";
|
|
3
|
+
import { jsx as s, jsxs as c } from "react/jsx-runtime";
|
|
4
|
+
import './index.css';var l = {
|
|
4
5
|
slotMachine: "cos-slotMachine-P6FyZ",
|
|
5
6
|
machine: "cos-machine-B066r",
|
|
6
7
|
reel: "cos-reel-eEpJ3",
|
|
@@ -9,116 +10,121 @@ import './index.css';var c = {
|
|
|
9
10
|
mask: "cos-mask-jdrj3",
|
|
10
11
|
spinBtn: "cos-spinBtn-DvDE7",
|
|
11
12
|
result: "cos-result-dLN5b"
|
|
12
|
-
},
|
|
13
|
+
}, u = [
|
|
13
14
|
"🍒",
|
|
14
15
|
"🍋",
|
|
15
16
|
"🍊",
|
|
16
17
|
"🍇",
|
|
17
18
|
"⭐",
|
|
18
19
|
"7️⃣"
|
|
19
|
-
],
|
|
20
|
+
], d = 30, f = 8, p = [
|
|
20
21
|
0,
|
|
21
22
|
350,
|
|
22
23
|
700
|
|
23
|
-
],
|
|
24
|
+
], m = (e) => 1 - (1 - e) ** 3, h = (e, t) => {
|
|
24
25
|
if (t === void 0) return Math.floor(Math.random() * e.length);
|
|
25
26
|
let n = e.indexOf(t);
|
|
26
27
|
return n >= 0 ? n : Math.floor(Math.random() * e.length);
|
|
27
|
-
},
|
|
28
|
-
let
|
|
28
|
+
}, g = e(({ symbols: e = u, spinDuration: g = 3e3, targetResults: _, onSpinEnd: v, startText: y, buttonText: b, spinningText: x = "旋转中...", jackpotText: S = "🎰 大奖!", resultPrefix: C = "结果:" }, w) => {
|
|
29
|
+
let T = b ?? y ?? "开始", [E, D] = a(!1), [O, k] = a([
|
|
29
30
|
0,
|
|
30
31
|
0,
|
|
31
32
|
0
|
|
32
|
-
]), [
|
|
33
|
+
]), [A, j] = a([]), M = i(0), N = i(!1), P = i(O), F = i(v), I = i(e), L = i(_), R = i(null);
|
|
33
34
|
n(() => {
|
|
34
|
-
|
|
35
|
-
}, [
|
|
36
|
-
|
|
35
|
+
F.current = v;
|
|
36
|
+
}, [v]), n(() => {
|
|
37
|
+
I.current = e;
|
|
37
38
|
}, [e]), n(() => {
|
|
38
|
-
|
|
39
|
-
}, [
|
|
40
|
-
|
|
41
|
-
}, [
|
|
42
|
-
|
|
43
|
-
}, [
|
|
44
|
-
let
|
|
39
|
+
L.current = _;
|
|
40
|
+
}, [_]), n(() => {
|
|
41
|
+
N.current = E;
|
|
42
|
+
}, [E]), n(() => {
|
|
43
|
+
P.current = O;
|
|
44
|
+
}, [O]);
|
|
45
|
+
let z = e.length * 80, B = Math.floor(d / 2) * z, V = t(() => {
|
|
45
46
|
let t = [];
|
|
46
|
-
for (let n = 0; n <
|
|
47
|
+
for (let n = 0; n < d; n++) t.push(...e);
|
|
47
48
|
return t;
|
|
48
|
-
}, [e])(),
|
|
49
|
-
let t =
|
|
50
|
-
if (
|
|
51
|
-
|
|
52
|
-
let n = e ??
|
|
49
|
+
}, [e])(), H = (e, t) => -(e - 1 + t * I.current.length) * 80, U = t((e) => {
|
|
50
|
+
let t = I.current;
|
|
51
|
+
if (N.current || t.length === 0) return;
|
|
52
|
+
D(!0), j([]);
|
|
53
|
+
let n = e ?? L.current, r = [
|
|
53
54
|
0,
|
|
54
55
|
1,
|
|
55
56
|
2
|
|
56
|
-
].map((e) =>
|
|
57
|
-
let n = i[t], r =
|
|
58
|
-
for (; n - s < r;) o += 1, s =
|
|
57
|
+
].map((e) => h(t, n?.[e])), i = [...P.current], a = t.length * 80, s = r.map((e, t) => {
|
|
58
|
+
let n = i[t], r = f * a, o = Math.ceil((-n + r) / a) + 1, s = H(e, o);
|
|
59
|
+
for (; n - s < r;) o += 1, s = H(e, o);
|
|
59
60
|
return s;
|
|
60
|
-
}),
|
|
61
|
-
|
|
62
|
-
let
|
|
63
|
-
if (
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
61
|
+
}), c = Math.max(g, 16), l = performance.now(), u = { cancelled: !1 };
|
|
62
|
+
R.current = u;
|
|
63
|
+
let d = () => {
|
|
64
|
+
if (u.cancelled) return;
|
|
65
|
+
let e = r.map((e) => t[e]);
|
|
66
|
+
k(r.map((e) => H(e, Math.round(B / a)))), j(e), D(!1), F.current?.(e);
|
|
67
|
+
};
|
|
68
|
+
if (o()) {
|
|
69
|
+
d();
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
let _ = (e) => {
|
|
73
|
+
u.cancelled || (k(i.map((t, n) => {
|
|
74
|
+
let r = e - l - p[n];
|
|
75
|
+
if (r < 0) return t;
|
|
76
|
+
let i = m(Math.min(r / c, 1));
|
|
77
|
+
return t + (s[n] - t) * i;
|
|
78
|
+
})), p.every((t) => e - l - t >= c) ? d() : M.current = requestAnimationFrame(_));
|
|
75
79
|
};
|
|
76
|
-
|
|
77
|
-
}, [
|
|
78
|
-
|
|
80
|
+
M.current = requestAnimationFrame(_);
|
|
81
|
+
}, [B, g]), W = t(() => {
|
|
82
|
+
N.current || j([]);
|
|
79
83
|
}, []);
|
|
80
|
-
return r(
|
|
81
|
-
spin:
|
|
82
|
-
reset:
|
|
83
|
-
}), [
|
|
84
|
-
|
|
85
|
-
}, []), /* @__PURE__ */
|
|
86
|
-
className:
|
|
84
|
+
return r(w, () => ({
|
|
85
|
+
spin: U,
|
|
86
|
+
reset: W
|
|
87
|
+
}), [U, W]), n(() => () => {
|
|
88
|
+
R.current && (R.current.cancelled = !0), cancelAnimationFrame(M.current);
|
|
89
|
+
}, []), /* @__PURE__ */ c("div", {
|
|
90
|
+
className: l.slotMachine,
|
|
87
91
|
children: [
|
|
88
|
-
/* @__PURE__ */
|
|
89
|
-
className:
|
|
92
|
+
/* @__PURE__ */ s("div", {
|
|
93
|
+
className: l.machine,
|
|
90
94
|
children: [
|
|
91
95
|
0,
|
|
92
96
|
1,
|
|
93
97
|
2
|
|
94
|
-
].map((e) => /* @__PURE__ */
|
|
95
|
-
className:
|
|
98
|
+
].map((e) => /* @__PURE__ */ c("div", {
|
|
99
|
+
className: l.reel,
|
|
96
100
|
style: { height: 240 },
|
|
97
|
-
children: [/* @__PURE__ */
|
|
98
|
-
className:
|
|
99
|
-
style: { transform: `translateY(${
|
|
100
|
-
children:
|
|
101
|
-
className:
|
|
101
|
+
children: [/* @__PURE__ */ s("div", {
|
|
102
|
+
className: l.strip,
|
|
103
|
+
style: { transform: `translateY(${O[e]}px)` },
|
|
104
|
+
children: V.map((e, t) => /* @__PURE__ */ s("div", {
|
|
105
|
+
className: l.item,
|
|
102
106
|
style: { height: 80 },
|
|
103
107
|
children: e
|
|
104
108
|
}, t))
|
|
105
|
-
}), /* @__PURE__ */
|
|
109
|
+
}), /* @__PURE__ */ s("div", { className: l.mask })]
|
|
106
110
|
}, e))
|
|
107
111
|
}),
|
|
108
|
-
/* @__PURE__ */
|
|
112
|
+
/* @__PURE__ */ s("button", {
|
|
109
113
|
type: "button",
|
|
110
|
-
className:
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
+
className: l.spinBtn,
|
|
115
|
+
"data-testid": "slot-machine-spin",
|
|
116
|
+
onClick: () => U(),
|
|
117
|
+
disabled: E,
|
|
118
|
+
"aria-busy": E,
|
|
119
|
+
children: E ? x : T
|
|
114
120
|
}),
|
|
115
|
-
|
|
116
|
-
className:
|
|
117
|
-
children:
|
|
121
|
+
A.length > 0 && !E && /* @__PURE__ */ s("p", {
|
|
122
|
+
className: l.result,
|
|
123
|
+
children: A[0] === A[1] && A[1] === A[2] ? S : `${C} ${A.join(" ")}`
|
|
118
124
|
})
|
|
119
125
|
]
|
|
120
126
|
});
|
|
121
127
|
});
|
|
122
|
-
|
|
128
|
+
g.displayName = "SlotMachine";
|
|
123
129
|
//#endregion
|
|
124
|
-
export {
|
|
130
|
+
export { g as SlotMachine, g as default };
|
|
@@ -0,0 +1,10 @@
|
|
|
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 { useElementSize, resolveCanvasBoxSize, useCanvasBox } from './use-element-size';
|
|
10
|
+
export type { UseElementSizeOptions, CanvasBoxSizeInput, CanvasBoxSize, UseCanvasBoxOptions, UseCanvasBoxResult } from './use-element-size';
|
|
@@ -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,45 @@
|
|
|
1
|
+
import { CSSProperties, RefObject } from 'react';
|
|
2
|
+
import { ElementSize } from './size';
|
|
3
|
+
export interface UseElementSizeOptions {
|
|
4
|
+
/** 为 false 时不观察 */
|
|
5
|
+
enabled?: boolean;
|
|
6
|
+
}
|
|
7
|
+
/** 观察元素尺寸(clientWidth / clientHeight) */
|
|
8
|
+
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
|
+
export interface UseCanvasBoxOptions {
|
|
27
|
+
/** 为 true 时铺满父容器(父级需有明确高度) */
|
|
28
|
+
fill?: boolean;
|
|
29
|
+
width?: number;
|
|
30
|
+
height?: number;
|
|
31
|
+
defaultWidth: number;
|
|
32
|
+
defaultHeight: number;
|
|
33
|
+
}
|
|
34
|
+
export interface UseCanvasBoxResult {
|
|
35
|
+
hostRef: RefObject<HTMLDivElement | null>;
|
|
36
|
+
width: number;
|
|
37
|
+
height: number;
|
|
38
|
+
/** 挂到宿主容器的样式(fill 时为 100%×100%) */
|
|
39
|
+
hostStyle: CSSProperties;
|
|
40
|
+
fill: boolean;
|
|
41
|
+
/** 是否处于 fill 模式且尚未量到有效尺寸 */
|
|
42
|
+
pending: boolean;
|
|
43
|
+
}
|
|
44
|
+
/** 画布/效果宿主尺寸:fill 时跟随父容器,否则用显式宽高或默认值 */
|
|
45
|
+
export declare const useCanvasBox: ({ fill, width: widthProp, height: heightProp, defaultWidth, defaultHeight }: UseCanvasBoxOptions) => UseCanvasBoxResult;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cos-design/slot-machine",
|
|
3
|
-
"version": "3.5.
|
|
3
|
+
"version": "3.5.7",
|
|
4
4
|
"description": "SlotMachine component from cos-design",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.cjs",
|
|
@@ -50,5 +50,8 @@
|
|
|
50
50
|
"publishConfig": {
|
|
51
51
|
"access": "public",
|
|
52
52
|
"registry": "https://registry.npmjs.org"
|
|
53
|
+
},
|
|
54
|
+
"dependencies": {
|
|
55
|
+
"@cos-design/shared": "3.5.6"
|
|
53
56
|
}
|
|
54
57
|
}
|