@cos-design/slot-machine 3.5.3 → 3.5.5

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