@cos-design/slot-machine 3.5.4 → 3.5.6
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 +89 -68
- package/dist/src/components/slotMachine/index.d.ts +11 -1
- package/package.json +1 -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 u={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`},d=[`🍒`,`🍋`,`🍊`,`🍇`,`⭐`,`7️⃣`],f=30,p=8,m=[0,350,700],h=e=>1-(1-e)**3,g=({symbols:e=d,spinDuration:t=3e3,
|
|
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 u={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`},d=[`🍒`,`🍋`,`🍊`,`🍇`,`⭐`,`7️⃣`],f=30,p=8,m=[0,350,700],h=e=>1-(1-e)**3,g=(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)},_=(0,c.forwardRef)(({symbols:e=d,spinDuration:t=3e3,targetResults:n,onSpinEnd:r,startText:i,buttonText:a,spinningText:o=`旋转中...`,jackpotText:s=`🎰 大奖!`,resultPrefix:_=`结果:`},v)=>{let y=a??i??`开始`,[b,x]=(0,c.useState)(!1),[S,C]=(0,c.useState)([0,0,0]),[w,T]=(0,c.useState)([]),E=(0,c.useRef)(0),D=(0,c.useRef)(!1),O=(0,c.useRef)(S),k=(0,c.useRef)(r),A=(0,c.useRef)(e),j=(0,c.useRef)(n),M=(0,c.useRef)(null);(0,c.useEffect)(()=>{k.current=r},[r]),(0,c.useEffect)(()=>{A.current=e},[e]),(0,c.useEffect)(()=>{j.current=n},[n]),(0,c.useEffect)(()=>{D.current=b},[b]),(0,c.useEffect)(()=>{O.current=S},[S]);let N=e.length*80,P=Math.floor(f/2)*N,F=(0,c.useCallback)(()=>{let t=[];for(let n=0;n<f;n++)t.push(...e);return t},[e])(),I=(e,t)=>-(e-1+t*A.current.length)*80,L=(0,c.useCallback)(e=>{let n=A.current;if(D.current||n.length===0)return;x(!0),T([]);let r=e??j.current,i=[0,1,2].map(e=>g(n,r?.[e])),a=[...O.current],o=n.length*80,s=i.map((e,t)=>{let n=a[t],r=p*o,i=Math.ceil((-n+r)/o)+1,s=I(e,i);for(;n-s<r;)i+=1,s=I(e,i);return s}),c=Math.max(t,16),l=performance.now(),u={cancelled:!1};M.current=u;let d=e=>{if(!u.cancelled){if(C(a.map((t,n)=>{let r=e-l-m[n];if(r<0)return t;let i=h(Math.min(r/c,1));return t+(s[n]-t)*i})),!m.every(t=>e-l-t>=c))E.current=requestAnimationFrame(d);else if(!u.cancelled){let e=i.map(e=>n[e]);C(i.map(e=>I(e,Math.round(P/o)))),T(e),x(!1),k.current?.(e)}}};E.current=requestAnimationFrame(d)},[P,t]),R=(0,c.useCallback)(()=>{D.current||T([])},[]);return(0,c.useImperativeHandle)(v,()=>({spin:L,reset:R}),[L,R]),(0,c.useEffect)(()=>()=>{M.current&&(M.current.cancelled=!0),cancelAnimationFrame(E.current)},[]),(0,l.jsxs)(`div`,{className:u.slotMachine,children:[(0,l.jsx)(`div`,{className:u.machine,children:[0,1,2].map(e=>(0,l.jsxs)(`div`,{className:u.reel,style:{height:240},children:[(0,l.jsx)(`div`,{className:u.strip,style:{transform:`translateY(${S[e]}px)`},children:F.map((e,t)=>(0,l.jsx)(`div`,{className:u.item,style:{height:80},children:e},t))}),(0,l.jsx)(`div`,{className:u.mask})]},e))}),(0,l.jsx)(`button`,{type:`button`,className:u.spinBtn,"data-testid":`slot-machine-spin`,onClick:()=>L(),disabled:b,"aria-busy":b,children:b?o:y}),w.length>0&&!b&&(0,l.jsx)(`p`,{className:u.result,children:w[0]===w[1]&&w[1]===w[2]?s:`${_} ${w.join(` `)}`})]})});_.displayName=`SlotMachine`,exports.SlotMachine=_,exports.default=_;
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { jsx as
|
|
3
|
-
import './index.css';var
|
|
1
|
+
import { forwardRef as e, useCallback as t, useEffect as n, useImperativeHandle as r, useRef as i, useState as a } from "react";
|
|
2
|
+
import { jsx as o, jsxs as s } from "react/jsx-runtime";
|
|
3
|
+
import './index.css';var c = {
|
|
4
4
|
slotMachine: "cos-slotMachine-P6FyZ",
|
|
5
5
|
machine: "cos-machine-B066r",
|
|
6
6
|
reel: "cos-reel-eEpJ3",
|
|
@@ -9,97 +9,118 @@ import './index.css';var o = {
|
|
|
9
9
|
mask: "cos-mask-jdrj3",
|
|
10
10
|
spinBtn: "cos-spinBtn-DvDE7",
|
|
11
11
|
result: "cos-result-dLN5b"
|
|
12
|
-
},
|
|
12
|
+
}, l = [
|
|
13
13
|
"🍒",
|
|
14
14
|
"🍋",
|
|
15
15
|
"🍊",
|
|
16
16
|
"🍇",
|
|
17
17
|
"⭐",
|
|
18
18
|
"7️⃣"
|
|
19
|
-
],
|
|
19
|
+
], u = 30, d = 8, f = [
|
|
20
20
|
0,
|
|
21
21
|
350,
|
|
22
22
|
700
|
|
23
|
-
],
|
|
24
|
-
|
|
23
|
+
], p = (e) => 1 - (1 - e) ** 3, m = (e, t) => {
|
|
24
|
+
if (t === void 0) return Math.floor(Math.random() * e.length);
|
|
25
|
+
let n = e.indexOf(t);
|
|
26
|
+
return n >= 0 ? n : Math.floor(Math.random() * e.length);
|
|
27
|
+
}, h = e(({ symbols: e = l, spinDuration: h = 3e3, targetResults: g, onSpinEnd: _, startText: v, buttonText: y, spinningText: b = "旋转中...", jackpotText: x = "🎰 大奖!", resultPrefix: S = "结果:" }, C) => {
|
|
28
|
+
let w = y ?? v ?? "开始", [T, E] = a(!1), [D, O] = a([
|
|
25
29
|
0,
|
|
26
30
|
0,
|
|
27
31
|
0
|
|
28
|
-
]), [
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
}, [
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
}, [])
|
|
40
|
-
|
|
32
|
+
]), [k, A] = a([]), j = i(0), M = i(!1), N = i(D), P = i(_), F = i(e), I = i(g), L = i(null);
|
|
33
|
+
n(() => {
|
|
34
|
+
P.current = _;
|
|
35
|
+
}, [_]), n(() => {
|
|
36
|
+
F.current = e;
|
|
37
|
+
}, [e]), n(() => {
|
|
38
|
+
I.current = g;
|
|
39
|
+
}, [g]), n(() => {
|
|
40
|
+
M.current = T;
|
|
41
|
+
}, [T]), n(() => {
|
|
42
|
+
N.current = D;
|
|
43
|
+
}, [D]);
|
|
44
|
+
let R = e.length * 80, z = Math.floor(u / 2) * R, B = t(() => {
|
|
45
|
+
let t = [];
|
|
46
|
+
for (let n = 0; n < u; n++) t.push(...e);
|
|
47
|
+
return t;
|
|
48
|
+
}, [e])(), V = (e, t) => -(e - 1 + t * F.current.length) * 80, H = t((e) => {
|
|
49
|
+
let t = F.current;
|
|
50
|
+
if (M.current || t.length === 0) return;
|
|
51
|
+
E(!0), A([]);
|
|
52
|
+
let n = e ?? I.current, r = [
|
|
53
|
+
0,
|
|
54
|
+
1,
|
|
55
|
+
2
|
|
56
|
+
].map((e) => m(t, n?.[e])), i = [...N.current], a = t.length * 80, o = r.map((e, t) => {
|
|
57
|
+
let n = i[t], r = d * a, o = Math.ceil((-n + r) / a) + 1, s = V(e, o);
|
|
58
|
+
for (; n - s < r;) o += 1, s = V(e, o);
|
|
59
|
+
return s;
|
|
60
|
+
}), s = Math.max(h, 16), c = performance.now(), l = { cancelled: !1 };
|
|
61
|
+
L.current = l;
|
|
62
|
+
let u = (e) => {
|
|
63
|
+
if (!l.cancelled) {
|
|
64
|
+
if (O(i.map((t, n) => {
|
|
65
|
+
let r = e - c - f[n];
|
|
66
|
+
if (r < 0) return t;
|
|
67
|
+
let i = p(Math.min(r / s, 1));
|
|
68
|
+
return t + (o[n] - t) * i;
|
|
69
|
+
})), !f.every((t) => e - c - t >= s)) j.current = requestAnimationFrame(u);
|
|
70
|
+
else if (!l.cancelled) {
|
|
71
|
+
let e = r.map((e) => t[e]);
|
|
72
|
+
O(r.map((e) => V(e, Math.round(z / a)))), A(e), E(!1), P.current?.(e);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
j.current = requestAnimationFrame(u);
|
|
77
|
+
}, [z, h]), U = t(() => {
|
|
78
|
+
M.current || A([]);
|
|
79
|
+
}, []);
|
|
80
|
+
return r(C, () => ({
|
|
81
|
+
spin: H,
|
|
82
|
+
reset: U
|
|
83
|
+
}), [H, U]), n(() => () => {
|
|
84
|
+
L.current && (L.current.cancelled = !0), cancelAnimationFrame(j.current);
|
|
85
|
+
}, []), /* @__PURE__ */ s("div", {
|
|
86
|
+
className: c.slotMachine,
|
|
41
87
|
children: [
|
|
42
|
-
/* @__PURE__ */
|
|
43
|
-
className:
|
|
88
|
+
/* @__PURE__ */ o("div", {
|
|
89
|
+
className: c.machine,
|
|
44
90
|
children: [
|
|
45
91
|
0,
|
|
46
92
|
1,
|
|
47
93
|
2
|
|
48
|
-
].map((e) => /* @__PURE__ */
|
|
49
|
-
className:
|
|
94
|
+
].map((e) => /* @__PURE__ */ s("div", {
|
|
95
|
+
className: c.reel,
|
|
50
96
|
style: { height: 240 },
|
|
51
|
-
children: [/* @__PURE__ */
|
|
52
|
-
className:
|
|
53
|
-
style: { transform: `translateY(${
|
|
54
|
-
children:
|
|
55
|
-
className:
|
|
97
|
+
children: [/* @__PURE__ */ o("div", {
|
|
98
|
+
className: c.strip,
|
|
99
|
+
style: { transform: `translateY(${D[e]}px)` },
|
|
100
|
+
children: B.map((e, t) => /* @__PURE__ */ o("div", {
|
|
101
|
+
className: c.item,
|
|
56
102
|
style: { height: 80 },
|
|
57
103
|
children: e
|
|
58
104
|
}, t))
|
|
59
|
-
}), /* @__PURE__ */
|
|
105
|
+
}), /* @__PURE__ */ o("div", { className: c.mask })]
|
|
60
106
|
}, e))
|
|
61
107
|
}),
|
|
62
|
-
/* @__PURE__ */
|
|
108
|
+
/* @__PURE__ */ o("button", {
|
|
63
109
|
type: "button",
|
|
64
|
-
className:
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
1,
|
|
71
|
-
2
|
|
72
|
-
].map(() => Math.floor(Math.random() * f.length)), t = [...x], n = e.map((e, n) => {
|
|
73
|
-
let r = t[n], i = l * O, a = Math.ceil((-r + i) / O) + 1, o = j(e, a);
|
|
74
|
-
for (; r - o < i;) a += 1, o = j(e, a);
|
|
75
|
-
return o;
|
|
76
|
-
}), r = Math.max(p, 16), i = performance.now(), a = { cancelled: !1 };
|
|
77
|
-
D.current = a;
|
|
78
|
-
let o = (s) => {
|
|
79
|
-
if (!a.cancelled) {
|
|
80
|
-
if (S(t.map((e, t) => {
|
|
81
|
-
let a = s - i - u[t];
|
|
82
|
-
if (a < 0) return e;
|
|
83
|
-
let o = d(Math.min(a / r, 1));
|
|
84
|
-
return e + (n[t] - e) * o;
|
|
85
|
-
})), !u.every((e) => s - i - e >= r)) T.current = requestAnimationFrame(o);
|
|
86
|
-
else if (!a.cancelled) {
|
|
87
|
-
let t = e.map((e) => f[e]);
|
|
88
|
-
S(e.map((e) => j(e, Math.round(k / O)))), w(t), b(!1), E.current?.(t);
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
};
|
|
92
|
-
T.current = requestAnimationFrame(o);
|
|
93
|
-
},
|
|
94
|
-
disabled: y,
|
|
95
|
-
children: y ? g : h
|
|
110
|
+
className: c.spinBtn,
|
|
111
|
+
"data-testid": "slot-machine-spin",
|
|
112
|
+
onClick: () => H(),
|
|
113
|
+
disabled: T,
|
|
114
|
+
"aria-busy": T,
|
|
115
|
+
children: T ? b : w
|
|
96
116
|
}),
|
|
97
|
-
|
|
98
|
-
className:
|
|
99
|
-
children:
|
|
117
|
+
k.length > 0 && !T && /* @__PURE__ */ o("p", {
|
|
118
|
+
className: c.result,
|
|
119
|
+
children: k[0] === k[1] && k[1] === k[2] ? x : `${S} ${k.join(" ")}`
|
|
100
120
|
})
|
|
101
121
|
]
|
|
102
122
|
});
|
|
103
|
-
};
|
|
123
|
+
});
|
|
124
|
+
h.displayName = "SlotMachine";
|
|
104
125
|
//#endregion
|
|
105
|
-
export {
|
|
126
|
+
export { h as SlotMachine, h as default };
|
|
@@ -4,10 +4,14 @@ export interface SlotMachineProps {
|
|
|
4
4
|
symbols?: string[];
|
|
5
5
|
/** 单列旋转时长(毫秒),默认 3000;列之间仍有错峰停轮 */
|
|
6
6
|
spinDuration?: number;
|
|
7
|
+
/** 指定下一次停轮结果(长度 3);不传则随机 */
|
|
8
|
+
targetResults?: string[];
|
|
7
9
|
/** 旋转结束回调 */
|
|
8
10
|
onSpinEnd?: (results: string[]) => void;
|
|
9
11
|
/** 开始按钮文案 */
|
|
10
12
|
startText?: string;
|
|
13
|
+
/** 开始按钮文案别名(与 Turntable.buttonText 对齐) */
|
|
14
|
+
buttonText?: string;
|
|
11
15
|
/** 旋转中的按钮文案 */
|
|
12
16
|
spinningText?: string;
|
|
13
17
|
/** 中奖提示 */
|
|
@@ -15,5 +19,11 @@ export interface SlotMachineProps {
|
|
|
15
19
|
/** 普通结果前缀 */
|
|
16
20
|
resultPrefix?: string;
|
|
17
21
|
}
|
|
18
|
-
|
|
22
|
+
export interface SlotMachineHandle {
|
|
23
|
+
/** 手动旋转;可传入三列结果覆盖 props.targetResults */
|
|
24
|
+
spin: (results?: string[]) => void;
|
|
25
|
+
/** 清除结果文案 */
|
|
26
|
+
reset: () => void;
|
|
27
|
+
}
|
|
28
|
+
declare const SlotMachine: React.ForwardRefExoticComponent<SlotMachineProps & React.RefAttributes<SlotMachineHandle>>;
|
|
19
29
|
export default SlotMachine;
|