@cos-design/slot-machine 3.5.3 → 3.5.4

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=({symbols:e=d,spinDuration:t=3e3,onSpinEnd:n,startText:r=`开始`,spinningText:i=`旋转中...`,jackpotText:a=`🎰 大奖!`,resultPrefix:o=`结果:`})=>{let[s,g]=(0,c.useState)(!1),[_,v]=(0,c.useState)([0,0,0]),[y,b]=(0,c.useState)([]),x=(0,c.useRef)(0),S=(0,c.useRef)(n),C=(0,c.useRef)(null);(0,c.useEffect)(()=>{S.current=n},[n]);let w=e.length*80,T=Math.floor(f/2)*w,E=(0,c.useCallback)(()=>{let t=[];for(let n=0;n<f;n++)t.push(...e);return t},[e])(),D=(t,n)=>-(t-1+n*e.length)*80;return(0,c.useEffect)(()=>()=>{C.current&&(C.current.cancelled=!0),cancelAnimationFrame(x.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(${_[e]}px)`},children:E.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(s||e.length===0)return;g(!0),b([]);let n=[0,1,2].map(()=>Math.floor(Math.random()*e.length)),r=[..._],i=n.map((e,t)=>{let n=r[t],i=p*w,a=Math.ceil((-n+i)/w)+1,o=D(e,a);for(;n-o<i;)a+=1,o=D(e,a);return o}),a=Math.max(t,16),o=performance.now(),c={cancelled:!1};C.current=c;let l=t=>{if(!c.cancelled){if(v(r.map((e,n)=>{let r=t-o-m[n];if(r<0)return e;let s=h(Math.min(r/a,1));return e+(i[n]-e)*s})),!m.every(e=>t-o-e>=a))x.current=requestAnimationFrame(l);else if(!c.cancelled){let t=n.map(t=>e[t]);v(n.map(e=>D(e,Math.round(T/w)))),b(t),g(!1),S.current?.(t)}}};x.current=requestAnimationFrame(l)},disabled:s,children:s?i:r}),y.length>0&&!s&&(0,l.jsx)(`p`,{className:u.result,children:y[0]===y[1]&&y[1]===y[2]?a:`${o} ${y.join(` `)}`})]})};exports.SlotMachine=g,exports.default=g;
package/dist/index.js CHANGED
@@ -16,22 +16,26 @@ import './index.css';var o = {
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
+ ], c = 30, l = 8, u = [
20
+ 0,
21
+ 350,
22
+ 700
23
+ ], d = (e) => 1 - (1 - e) ** 3, f = ({ symbols: f = s, spinDuration: p = 3e3, onSpinEnd: m, startText: h = "开始", spinningText: g = "旋转中...", jackpotText: _ = "🎰 大奖!", resultPrefix: v = "结果:" }) => {
24
+ let [y, b] = r(!1), [x, S] = r([
21
25
  0,
22
26
  0,
23
27
  0
24
- ]), [h, g] = r([]), _ = n(0), v = n(u), y = n(null);
28
+ ]), [C, w] = r([]), T = n(0), E = n(m), D = n(null);
25
29
  t(() => {
26
- v.current = u;
27
- }, [u]);
28
- let b = e(() => {
30
+ E.current = m;
31
+ }, [m]);
32
+ let O = f.length * 80, k = Math.floor(c / 2) * O, A = e(() => {
29
33
  let e = [];
30
- for (let t = 0; t < 20; t++) e.push(...l);
34
+ for (let t = 0; t < c; t++) e.push(...f);
31
35
  return e;
32
- }, [l])();
36
+ }, [f])(), j = (e, t) => -(e - 1 + t * f.length) * 80;
33
37
  return t(() => () => {
34
- y.current && (y.current.cancelled = !0), cancelAnimationFrame(_.current);
38
+ D.current && (D.current.cancelled = !0), cancelAnimationFrame(T.current);
35
39
  }, []), /* @__PURE__ */ a("div", {
36
40
  className: o.slotMachine,
37
41
  children: [
@@ -46,8 +50,8 @@ import './index.css';var o = {
46
50
  style: { height: 240 },
47
51
  children: [/* @__PURE__ */ i("div", {
48
52
  className: o.strip,
49
- style: { transform: `translateY(${p[e]}px)` },
50
- children: b.map((e, t) => /* @__PURE__ */ i("div", {
53
+ style: { transform: `translateY(${x[e]}px)` },
54
+ children: A.map((e, t) => /* @__PURE__ */ i("div", {
51
55
  className: o.item,
52
56
  style: { height: 80 },
53
57
  children: e
@@ -59,43 +63,43 @@ import './index.css';var o = {
59
63
  type: "button",
60
64
  className: o.spinBtn,
61
65
  onClick: () => {
62
- if (d) return;
63
- f(!0), g([]);
66
+ if (y || f.length === 0) return;
67
+ b(!0), w([]);
64
68
  let e = [
65
69
  0,
66
70
  1,
67
71
  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);
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);
85
89
  }
86
90
  }
87
91
  };
88
- _.current = requestAnimationFrame(s);
92
+ T.current = requestAnimationFrame(o);
89
93
  },
90
- disabled: d,
91
- children: d ? "旋转中..." : "开始"
94
+ disabled: y,
95
+ children: y ? g : h
92
96
  }),
93
- h.length > 0 && !d && /* @__PURE__ */ i("p", {
97
+ C.length > 0 && !y && /* @__PURE__ */ i("p", {
94
98
  className: o.result,
95
- children: h[0] === h[1] && h[1] === h[2] ? "🎰 大奖!" : `结果: ${h.join(" ")}`
99
+ children: C[0] === C[1] && C[1] === C[2] ? _ : `${v} ${C.join(" ")}`
96
100
  })
97
101
  ]
98
102
  });
99
103
  };
100
104
  //#endregion
101
- export { l as SlotMachine, l as default };
105
+ export { f as SlotMachine, f as default };
@@ -2,8 +2,18 @@ import { default as React } from 'react';
2
2
  export interface SlotMachineProps {
3
3
  /** 符号列表 */
4
4
  symbols?: string[];
5
+ /** 单列旋转时长(毫秒),默认 3000;列之间仍有错峰停轮 */
6
+ spinDuration?: number;
5
7
  /** 旋转结束回调 */
6
8
  onSpinEnd?: (results: string[]) => void;
9
+ /** 开始按钮文案 */
10
+ startText?: string;
11
+ /** 旋转中的按钮文案 */
12
+ spinningText?: string;
13
+ /** 中奖提示 */
14
+ jackpotText?: string;
15
+ /** 普通结果前缀 */
16
+ resultPrefix?: string;
7
17
  }
8
18
  declare const SlotMachine: React.FC<SlotMachineProps>;
9
19
  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.4",
4
4
  "description": "SlotMachine component from cos-design",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",