@cos-design/fireworks 3.5.5 → 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 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("@cos-design/shared"),u=require("react/jsx-runtime");var d={fireworks:`cos-fireworks-fsSqL`,canvas:`cos-canvas-spL8G`,hint:`cos-hint-5XDRH`},f=[`#ff6b6b`,`#ffd93d`,`#6bcb77`,`#4d96ff`,`#a66cff`,`#ff85c0`],p=.12,m=240,h=()=>f[Math.floor(Math.random()*f.length)],g=(e,t,n)=>{let r=60+Math.floor(Math.random()*30);return Array.from({length:r},()=>{let r=Math.random()*Math.PI*2,i=Math.random()*4+2;return{x:e,y:t,vx:Math.cos(r)*i,vy:Math.sin(r)*i,alpha:1,color:Math.random()>.3?n:h(),decay:Math.random()*.015+.01}})},_=(0,c.forwardRef)((e,t)=>{let{width:n,height:r,fill:i=!1,auto:a=!0,hint:o=`点击画布燃放烟花`,onComplete:s}=e,f=(0,c.useRef)(null),{width:_,height:v}=(0,l.resolveCanvasBoxSize)({fill:i,width:n,height:r,defaultWidth:800,defaultHeight:500,measured:(0,l.useElementSize)(f,{enabled:i})}),y=(0,c.useRef)(null),b=(0,c.useRef)([]),x=(0,c.useRef)(0),S=(0,c.useRef)(!1),C=(0,c.useRef)(s);(0,c.useEffect)(()=>{C.current=s},[s]);let w=(0,c.useCallback)(e=>{let t=e??Math.random()*_*.6+_*.2;b.current.push({x:t,y:v,vy:-(Math.random()*4+6),targetY:Math.random()*v*.35+v*.15,color:h(),exploded:!1,particles:[],age:0}),S.current=!0},[v,_]);return(0,c.useImperativeHandle)(t,()=>({launch:w}),[w]),(0,c.useEffect)(()=>{let e=y.current;if(!e)return;let t=window.devicePixelRatio||1;e.width=_*t,e.height=v*t;let n=e.getContext(`2d`);if(!n)return;n.setTransform(t,0,0,t,0,0);let r=0,i=document.hidden,o=(0,l.prefersReducedMotion)(),s=(0,l.bindVisibilityPause)(e=>{i=e}),c=(0,l.bindPrefersReducedMotion)(e=>{o=e}),u=()=>{if(x.current=requestAnimationFrame(u),!i){if(n.fillStyle=`rgb(15 23 42 / 25%)`,n.fillRect(0,0,_,v),o){S.current&&b.current.length===0&&(S.current=!1,C.current?.());return}b.current=b.current.filter(e=>{if(!e.exploded){e.age+=1,e.y+=e.vy,e.vy+=p,n.beginPath(),n.arc(e.x,e.y,2.5,0,Math.PI*2),n.fillStyle=e.color,n.fill();let t=e.vy>=0,r=e.y<=e.targetY,i=e.y<0,a=e.age>=m;return(t||r||i||a)&&(e.exploded=!0,e.particles=g(e.x,e.y,e.color)),!0}return e.particles=e.particles.filter(e=>(e.x+=e.vx,e.y+=e.vy,e.vy+=.06,e.alpha-=e.decay,e.alpha<=0?!1:(n.beginPath(),n.arc(e.x,e.y,2,0,Math.PI*2),n.fillStyle=e.color,n.globalAlpha=e.alpha,n.fill(),n.globalAlpha=1,!0))),e.particles.length>0}),S.current&&b.current.length===0&&(S.current=!1,C.current?.())}};return u(),a&&!o?r=window.setInterval(()=>w(),1200):a&&o&&w(),()=>{cancelAnimationFrame(x.current),clearInterval(r),s(),c()}},[a,v,w,_]),(0,u.jsxs)(`div`,{ref:f,className:d.fireworks,style:i?{width:`100%`,height:`100%`}:{width:_,height:v},children:[(0,u.jsx)(`canvas`,{ref:y,className:d.canvas,style:{width:_,height:v,touchAction:`none`},onPointerDown:e=>{let t=(0,l.getRelativePointerPosition)(e.currentTarget,e.nativeEvent);t&&w(t.x)}}),(0,u.jsx)(`p`,{className:d.hint,children:o})]})});_.displayName=`Fireworks`,exports.Fireworks=_,exports.default=_;
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={fireworks:`cos-fireworks-fsSqL`,canvas:`cos-canvas-spL8G`,canvasPassthrough:`cos-canvasPassthrough-jCNke`,hint:`cos-hint-5XDRH`},f=[`#ff6b6b`,`#ffd93d`,`#6bcb77`,`#4d96ff`,`#a66cff`,`#ff85c0`],p=.12,m=240,h=()=>f[Math.floor(Math.random()*f.length)],g=(e,t,n)=>{let r=60+Math.floor(Math.random()*30);return Array.from({length:r},()=>{let r=Math.random()*Math.PI*2,i=Math.random()*4+2;return{x:e,y:t,vx:Math.cos(r)*i,vy:Math.sin(r)*i,alpha:1,color:Math.random()>.3?n:h(),decay:Math.random()*.015+.01}})},_=(0,c.forwardRef)((e,t)=>{let{width:n,height:r,fill:i=!1,auto:a=!0,interactive:o,hint:s=`点击画布燃放烟花`,onComplete:f}=e,_=o??a,v=(0,c.useRef)(null),{width:y,height:b}=(0,l.resolveCanvasBoxSize)({fill:i,width:n,height:r,defaultWidth:800,defaultHeight:500,measured:(0,l.useElementSize)(v,{enabled:i})}),x=(0,c.useRef)(null),S=(0,c.useRef)([]),C=(0,c.useRef)(0),w=(0,c.useRef)(!1),T=(0,c.useRef)(f);(0,c.useEffect)(()=>{T.current=f},[f]);let E=(0,c.useCallback)(e=>{let t=e??Math.random()*y*.6+y*.2;S.current.push({x:t,y:b,vy:-(Math.random()*4+6),targetY:Math.random()*b*.35+b*.15,color:h(),exploded:!1,particles:[],age:0}),w.current=!0},[b,y]);return(0,c.useImperativeHandle)(t,()=>({launch:E}),[E]),(0,c.useEffect)(()=>{let e=x.current;if(!e)return;let t=window.devicePixelRatio||1;e.width=y*t,e.height=b*t;let n=e.getContext(`2d`);if(!n)return;n.setTransform(t,0,0,t,0,0);let r=0,i=document.hidden,o=(0,l.prefersReducedMotion)(),s=(0,l.bindVisibilityPause)(e=>{i=e}),c=(0,l.bindPrefersReducedMotion)(e=>{o=e}),u=()=>{if(C.current=requestAnimationFrame(u),!i){if(n.fillStyle=`rgb(15 23 42 / 25%)`,n.fillRect(0,0,y,b),o){w.current&&S.current.length===0&&(w.current=!1,T.current?.());return}S.current=S.current.filter(e=>{if(!e.exploded){e.age+=1,e.y+=e.vy,e.vy+=p,n.beginPath(),n.arc(e.x,e.y,2.5,0,Math.PI*2),n.fillStyle=e.color,n.fill();let t=e.vy>=0,r=e.y<=e.targetY,i=e.y<0,a=e.age>=m;return(t||r||i||a)&&(e.exploded=!0,e.particles=g(e.x,e.y,e.color)),!0}return e.particles=e.particles.filter(e=>(e.x+=e.vx,e.y+=e.vy,e.vy+=.06,e.alpha-=e.decay,e.alpha<=0?!1:(n.beginPath(),n.arc(e.x,e.y,2,0,Math.PI*2),n.fillStyle=e.color,n.globalAlpha=e.alpha,n.fill(),n.globalAlpha=1,!0))),e.particles.length>0}),w.current&&S.current.length===0&&(w.current=!1,T.current?.())}};return u(),a&&!o?r=window.setInterval(()=>E(),1200):a&&o&&E(),()=>{cancelAnimationFrame(C.current),clearInterval(r),s(),c()}},[a,b,E,y]),(0,u.jsxs)(`div`,{ref:v,className:d.fireworks,style:i?{width:`100%`,height:`100%`}:{width:y,height:b},children:[(0,u.jsx)(`canvas`,{ref:x,className:`${d.canvas} ${_?``:d.canvasPassthrough}`,style:{width:y,height:b,touchAction:_?`none`:`auto`},onPointerDown:_?e=>{let t=(0,l.getRelativePointerPosition)(e.currentTarget,e.nativeEvent);t&&E(t.x)}:void 0}),_&&s?(0,u.jsx)(`p`,{className:d.hint,children:s}):null]})});_.displayName=`Fireworks`,exports.Fireworks=_,exports.default=_;
package/dist/index.css CHANGED
@@ -1 +1 @@
1
- .cos-fireworks-fsSqL{background:#0f172a;border-radius:12px;position:relative;overflow:hidden}.cos-canvas-spL8G{cursor:crosshair;display:block}.cos-hint-5XDRH{color:#94a3b8cc;pointer-events:none;margin:0;font-size:13px;position:absolute;bottom:12px;left:50%;transform:translate(-50%)}
1
+ .cos-fireworks-fsSqL{background:#0f172a;border-radius:12px;position:relative;overflow:hidden}.cos-canvas-spL8G{cursor:crosshair;display:block}.cos-canvasPassthrough-jCNke{cursor:default;pointer-events:none}.cos-hint-5XDRH{color:#94a3b8cc;pointer-events:none;margin:0;font-size:13px;position:absolute;bottom:12px;left:50%;transform:translate(-50%)}
package/dist/index.js CHANGED
@@ -4,6 +4,7 @@ import { jsx as d, jsxs as f } from "react/jsx-runtime";
4
4
  import './index.css';var p = {
5
5
  fireworks: "cos-fireworks-fsSqL",
6
6
  canvas: "cos-canvas-spL8G",
7
+ canvasPassthrough: "cos-canvasPassthrough-jCNke",
7
8
  hint: "cos-hint-5XDRH"
8
9
  }, m = [
9
10
  "#ff6b6b",
@@ -27,35 +28,35 @@ import './index.css';var p = {
27
28
  };
28
29
  });
29
30
  }, y = e((e, m) => {
30
- let { width: y, height: b, fill: x = !1, auto: S = !0, hint: C = "点击画布燃放烟花", onComplete: w } = e, T = i(null), { width: E, height: D } = l({
31
+ let { width: y, height: b, fill: x = !1, auto: S = !0, interactive: C, hint: w = "点击画布燃放烟花", onComplete: T } = e, E = C ?? S, D = i(null), { width: O, height: k } = l({
31
32
  fill: x,
32
33
  width: y,
33
34
  height: b,
34
35
  defaultWidth: 800,
35
36
  defaultHeight: 500,
36
- measured: u(T, { enabled: x })
37
- }), O = i(null), k = i([]), A = i(0), j = i(!1), M = i(w);
37
+ measured: u(D, { enabled: x })
38
+ }), A = i(null), j = i([]), M = i(0), N = i(!1), P = i(T);
38
39
  n(() => {
39
- M.current = w;
40
- }, [w]);
41
- let N = t((e) => {
42
- let t = e ?? Math.random() * E * .6 + E * .2;
43
- k.current.push({
40
+ P.current = T;
41
+ }, [T]);
42
+ let F = t((e) => {
43
+ let t = e ?? Math.random() * O * .6 + O * .2;
44
+ j.current.push({
44
45
  x: t,
45
- y: D,
46
+ y: k,
46
47
  vy: -(Math.random() * 4 + 6),
47
- targetY: Math.random() * D * .35 + D * .15,
48
+ targetY: Math.random() * k * .35 + k * .15,
48
49
  color: _(),
49
50
  exploded: !1,
50
51
  particles: [],
51
52
  age: 0
52
- }), j.current = !0;
53
- }, [D, E]);
54
- return r(m, () => ({ launch: N }), [N]), n(() => {
55
- let e = O.current;
53
+ }), N.current = !0;
54
+ }, [k, O]);
55
+ return r(m, () => ({ launch: F }), [F]), n(() => {
56
+ let e = A.current;
56
57
  if (!e) return;
57
58
  let t = window.devicePixelRatio || 1;
58
- e.width = E * t, e.height = D * t;
59
+ e.width = O * t, e.height = k * t;
59
60
  let n = e.getContext("2d");
60
61
  if (!n) return;
61
62
  n.setTransform(t, 0, 0, t, 0, 0);
@@ -64,55 +65,55 @@ import './index.css';var p = {
64
65
  }), u = a((e) => {
65
66
  s = e;
66
67
  }), d = () => {
67
- if (A.current = requestAnimationFrame(d), !i) {
68
- if (n.fillStyle = "rgb(15 23 42 / 25%)", n.fillRect(0, 0, E, D), s) {
69
- j.current && k.current.length === 0 && (j.current = !1, M.current?.());
68
+ if (M.current = requestAnimationFrame(d), !i) {
69
+ if (n.fillStyle = "rgb(15 23 42 / 25%)", n.fillRect(0, 0, O, k), s) {
70
+ N.current && j.current.length === 0 && (N.current = !1, P.current?.());
70
71
  return;
71
72
  }
72
- k.current = k.current.filter((e) => {
73
+ j.current = j.current.filter((e) => {
73
74
  if (!e.exploded) {
74
75
  e.age += 1, e.y += e.vy, e.vy += h, n.beginPath(), n.arc(e.x, e.y, 2.5, 0, Math.PI * 2), n.fillStyle = e.color, n.fill();
75
76
  let t = e.vy >= 0, r = e.y <= e.targetY, i = e.y < 0, a = e.age >= g;
76
77
  return (t || r || i || a) && (e.exploded = !0, e.particles = v(e.x, e.y, e.color)), !0;
77
78
  }
78
79
  return e.particles = e.particles.filter((e) => (e.x += e.vx, e.y += e.vy, e.vy += .06, e.alpha -= e.decay, e.alpha <= 0 ? !1 : (n.beginPath(), n.arc(e.x, e.y, 2, 0, Math.PI * 2), n.fillStyle = e.color, n.globalAlpha = e.alpha, n.fill(), n.globalAlpha = 1, !0))), e.particles.length > 0;
79
- }), j.current && k.current.length === 0 && (j.current = !1, M.current?.());
80
+ }), N.current && j.current.length === 0 && (N.current = !1, P.current?.());
80
81
  }
81
82
  };
82
- return d(), S && !s ? r = window.setInterval(() => N(), 1200) : S && s && N(), () => {
83
- cancelAnimationFrame(A.current), clearInterval(r), l(), u();
83
+ return d(), S && !s ? r = window.setInterval(() => F(), 1200) : S && s && F(), () => {
84
+ cancelAnimationFrame(M.current), clearInterval(r), l(), u();
84
85
  };
85
86
  }, [
86
87
  S,
87
- D,
88
- N,
89
- E
88
+ k,
89
+ F,
90
+ O
90
91
  ]), /* @__PURE__ */ f("div", {
91
- ref: T,
92
+ ref: D,
92
93
  className: p.fireworks,
93
94
  style: x ? {
94
95
  width: "100%",
95
96
  height: "100%"
96
97
  } : {
97
- width: E,
98
- height: D
98
+ width: O,
99
+ height: k
99
100
  },
100
101
  children: [/* @__PURE__ */ d("canvas", {
101
- ref: O,
102
- className: p.canvas,
102
+ ref: A,
103
+ className: `${p.canvas} ${E ? "" : p.canvasPassthrough}`,
103
104
  style: {
104
- width: E,
105
- height: D,
106
- touchAction: "none"
105
+ width: O,
106
+ height: k,
107
+ touchAction: E ? "none" : "auto"
107
108
  },
108
- onPointerDown: (e) => {
109
+ onPointerDown: E ? (e) => {
109
110
  let t = s(e.currentTarget, e.nativeEvent);
110
- t && N(t.x);
111
- }
112
- }), /* @__PURE__ */ d("p", {
111
+ t && F(t.x);
112
+ } : void 0
113
+ }), E && w ? /* @__PURE__ */ d("p", {
113
114
  className: p.hint,
114
- children: C
115
- })]
115
+ children: w
116
+ }) : null]
116
117
  });
117
118
  });
118
119
  y.displayName = "Fireworks";
@@ -6,5 +6,5 @@ export type { ElementSize } from './size';
6
6
  export { getRelativePointerPosition } from './pointer';
7
7
  export type { PointerPosition } from './pointer';
8
8
  export type { PhotoItem, PhotoIndexChangeHandler, PhotoFaceChangeHandler } from './photo';
9
- export { useElementSize, resolveCanvasBoxSize } from './use-element-size';
10
- export type { UseElementSizeOptions, CanvasBoxSizeInput, CanvasBoxSize } from './use-element-size';
9
+ export { useElementSize, resolveCanvasBoxSize, useCanvasBox } from './use-element-size';
10
+ export type { UseElementSizeOptions, CanvasBoxSizeInput, CanvasBoxSize, UseCanvasBoxOptions, UseCanvasBoxResult } from './use-element-size';
@@ -1,4 +1,4 @@
1
- import { RefObject } from 'react';
1
+ import { CSSProperties, RefObject } from 'react';
2
2
  import { ElementSize } from './size';
3
3
  export interface UseElementSizeOptions {
4
4
  /** 为 false 时不观察 */
@@ -23,3 +23,23 @@ export interface CanvasBoxSize {
23
23
  }
24
24
  /** 根据 fill / 显式宽高 / 测量结果解析画布逻辑尺寸 */
25
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;
@@ -6,7 +6,11 @@ export interface FireworksProps {
6
6
  fill?: boolean;
7
7
  /** 是否自动燃放,默认 true */
8
8
  auto?: boolean;
9
- /** 画布操作提示 */
9
+ /**
10
+ * 是否响应点击燃放;默认跟随 auto(auto=false 时不拦截指针)
11
+ */
12
+ interactive?: boolean;
13
+ /** 画布操作提示;非 interactive 时不展示 */
10
14
  hint?: string;
11
15
  /** 画面空闲(无火箭/粒子)时回调 */
12
16
  onComplete?: () => void;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cos-design/fireworks",
3
- "version": "3.5.5",
3
+ "version": "3.5.6",
4
4
  "description": "Fireworks component from cos-design",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",
@@ -52,6 +52,6 @@
52
52
  "registry": "https://registry.npmjs.org"
53
53
  },
54
54
  "dependencies": {
55
- "@cos-design/shared": "3.5.5"
55
+ "@cos-design/shared": "3.5.6"
56
56
  }
57
57
  }