@cos-design/fireworks 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 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=800,height:r=500,auto:i=!0,hint:a=`点击画布燃放烟花`,onComplete:o}=e,s=(0,c.useRef)(null),f=(0,c.useRef)([]),_=(0,c.useRef)(0),v=(0,c.useRef)(!1),y=(0,c.useRef)(o);(0,c.useEffect)(()=>{y.current=o},[o]);let b=(0,c.useCallback)(e=>{let t=e??Math.random()*n*.6+n*.2;f.current.push({x:t,y:r,vy:-(Math.random()*4+6),targetY:Math.random()*r*.35+r*.15,color:h(),exploded:!1,particles:[],age:0}),v.current=!0},[r,n]);return(0,c.useImperativeHandle)(t,()=>({launch:b}),[b]),(0,c.useEffect)(()=>{let e=s.current;if(!e)return;let t=window.devicePixelRatio||1;e.width=n*t,e.height=r*t;let a=e.getContext(`2d`);if(!a)return;a.setTransform(t,0,0,t,0,0);let o=0,c=document.hidden,u=(0,l.prefersReducedMotion)(),d=(0,l.bindVisibilityPause)(e=>{c=e}),h=(0,l.bindPrefersReducedMotion)(e=>{u=e}),x=()=>{if(_.current=requestAnimationFrame(x),!c){if(a.fillStyle=`rgb(15 23 42 / 25%)`,a.fillRect(0,0,n,r),u){v.current&&f.current.length===0&&(v.current=!1,y.current?.());return}f.current=f.current.filter(e=>{if(!e.exploded){e.age+=1,e.y+=e.vy,e.vy+=p,a.beginPath(),a.arc(e.x,e.y,2.5,0,Math.PI*2),a.fillStyle=e.color,a.fill();let t=e.vy>=0,n=e.y<=e.targetY,r=e.y<0,i=e.age>=m;return(t||n||r||i)&&(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:(a.beginPath(),a.arc(e.x,e.y,2,0,Math.PI*2),a.fillStyle=e.color,a.globalAlpha=e.alpha,a.fill(),a.globalAlpha=1,!0))),e.particles.length>0}),v.current&&f.current.length===0&&(v.current=!1,y.current?.())}};return x(),i&&!u?o=window.setInterval(()=>b(),1200):i&&u&&b(),()=>{cancelAnimationFrame(_.current),clearInterval(o),d(),h()}},[i,r,b,n]),(0,u.jsxs)(`div`,{className:d.fireworks,style:{width:n,height:r},children:[(0,u.jsx)(`canvas`,{ref:s,className:d.canvas,style:{width:n,height:r,touchAction:`none`},onPointerDown:e=>{let t=(0,l.getRelativePointerPosition)(e.currentTarget,e.nativeEvent);t&&b(t.x)}}),(0,u.jsx)(`p`,{className:d.hint,children:a})]})});_.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
@@ -1,18 +1,19 @@
1
1
  import { forwardRef as e, useCallback as t, useEffect as n, useImperativeHandle as r, useRef as i } from "react";
2
- import { bindPrefersReducedMotion as a, bindVisibilityPause as o, getRelativePointerPosition as s, prefersReducedMotion as c } from "@cos-design/shared";
3
- import { jsx as l, jsxs as u } from "react/jsx-runtime";
4
- import './index.css';var d = {
2
+ import { bindPrefersReducedMotion as a, bindVisibilityPause as o, getRelativePointerPosition as s, prefersReducedMotion as c, resolveCanvasBoxSize as l, useElementSize as u } from "@cos-design/shared";
3
+ import { jsx as d, jsxs as f } from "react/jsx-runtime";
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
- }, f = [
9
+ }, m = [
9
10
  "#ff6b6b",
10
11
  "#ffd93d",
11
12
  "#6bcb77",
12
13
  "#4d96ff",
13
14
  "#a66cff",
14
15
  "#ff85c0"
15
- ], p = .12, m = 240, h = () => f[Math.floor(Math.random() * f.length)], g = (e, t, n) => {
16
+ ], h = .12, g = 240, _ = () => m[Math.floor(Math.random() * m.length)], v = (e, t, n) => {
16
17
  let r = 60 + Math.floor(Math.random() * 30);
17
18
  return Array.from({ length: r }, () => {
18
19
  let r = Math.random() * Math.PI * 2, i = Math.random() * 4 + 2;
@@ -22,33 +23,40 @@ import './index.css';var d = {
22
23
  vx: Math.cos(r) * i,
23
24
  vy: Math.sin(r) * i,
24
25
  alpha: 1,
25
- color: Math.random() > .3 ? n : h(),
26
+ color: Math.random() > .3 ? n : _(),
26
27
  decay: Math.random() * .015 + .01
27
28
  };
28
29
  });
29
- }, _ = e((e, f) => {
30
- let { width: _ = 800, height: v = 500, auto: y = !0, hint: b = "点击画布燃放烟花", onComplete: x } = e, S = i(null), C = i([]), w = i(0), T = i(!1), E = i(x);
30
+ }, y = e((e, m) => {
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({
32
+ fill: x,
33
+ width: y,
34
+ height: b,
35
+ defaultWidth: 800,
36
+ defaultHeight: 500,
37
+ measured: u(D, { enabled: x })
38
+ }), A = i(null), j = i([]), M = i(0), N = i(!1), P = i(T);
31
39
  n(() => {
32
- E.current = x;
33
- }, [x]);
34
- let D = t((e) => {
35
- let t = e ?? Math.random() * _ * .6 + _ * .2;
36
- C.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({
37
45
  x: t,
38
- y: v,
46
+ y: k,
39
47
  vy: -(Math.random() * 4 + 6),
40
- targetY: Math.random() * v * .35 + v * .15,
41
- color: h(),
48
+ targetY: Math.random() * k * .35 + k * .15,
49
+ color: _(),
42
50
  exploded: !1,
43
51
  particles: [],
44
52
  age: 0
45
- }), T.current = !0;
46
- }, [v, _]);
47
- return r(f, () => ({ launch: D }), [D]), n(() => {
48
- let e = S.current;
53
+ }), N.current = !0;
54
+ }, [k, O]);
55
+ return r(m, () => ({ launch: F }), [F]), n(() => {
56
+ let e = A.current;
49
57
  if (!e) return;
50
58
  let t = window.devicePixelRatio || 1;
51
- e.width = _ * t, e.height = v * t;
59
+ e.width = O * t, e.height = k * t;
52
60
  let n = e.getContext("2d");
53
61
  if (!n) return;
54
62
  n.setTransform(t, 0, 0, t, 0, 0);
@@ -57,53 +65,57 @@ import './index.css';var d = {
57
65
  }), u = a((e) => {
58
66
  s = e;
59
67
  }), d = () => {
60
- if (w.current = requestAnimationFrame(d), !i) {
61
- if (n.fillStyle = "rgb(15 23 42 / 25%)", n.fillRect(0, 0, _, v), s) {
62
- T.current && C.current.length === 0 && (T.current = !1, E.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?.());
63
71
  return;
64
72
  }
65
- C.current = C.current.filter((e) => {
73
+ j.current = j.current.filter((e) => {
66
74
  if (!e.exploded) {
67
- 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();
68
- let t = e.vy >= 0, r = e.y <= e.targetY, i = e.y < 0, a = e.age >= m;
69
- return (t || r || i || a) && (e.exploded = !0, e.particles = g(e.x, e.y, e.color)), !0;
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();
76
+ let t = e.vy >= 0, r = e.y <= e.targetY, i = e.y < 0, a = e.age >= g;
77
+ return (t || r || i || a) && (e.exploded = !0, e.particles = v(e.x, e.y, e.color)), !0;
70
78
  }
71
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;
72
- }), T.current && C.current.length === 0 && (T.current = !1, E.current?.());
80
+ }), N.current && j.current.length === 0 && (N.current = !1, P.current?.());
73
81
  }
74
82
  };
75
- return d(), y && !s ? r = window.setInterval(() => D(), 1200) : y && s && D(), () => {
76
- cancelAnimationFrame(w.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();
77
85
  };
78
86
  }, [
79
- y,
80
- v,
81
- D,
82
- _
83
- ]), /* @__PURE__ */ u("div", {
84
- className: d.fireworks,
85
- style: {
86
- width: _,
87
- height: v
87
+ S,
88
+ k,
89
+ F,
90
+ O
91
+ ]), /* @__PURE__ */ f("div", {
92
+ ref: D,
93
+ className: p.fireworks,
94
+ style: x ? {
95
+ width: "100%",
96
+ height: "100%"
97
+ } : {
98
+ width: O,
99
+ height: k
88
100
  },
89
- children: [/* @__PURE__ */ l("canvas", {
90
- ref: S,
91
- className: d.canvas,
101
+ children: [/* @__PURE__ */ d("canvas", {
102
+ ref: A,
103
+ className: `${p.canvas} ${E ? "" : p.canvasPassthrough}`,
92
104
  style: {
93
- width: _,
94
- height: v,
95
- touchAction: "none"
105
+ width: O,
106
+ height: k,
107
+ touchAction: E ? "none" : "auto"
96
108
  },
97
- onPointerDown: (e) => {
109
+ onPointerDown: E ? (e) => {
98
110
  let t = s(e.currentTarget, e.nativeEvent);
99
- t && D(t.x);
100
- }
101
- }), /* @__PURE__ */ l("p", {
102
- className: d.hint,
103
- children: b
104
- })]
111
+ t && F(t.x);
112
+ } : void 0
113
+ }), E && w ? /* @__PURE__ */ d("p", {
114
+ className: p.hint,
115
+ children: w
116
+ }) : null]
105
117
  });
106
118
  });
107
- _.displayName = "Fireworks";
119
+ y.displayName = "Fireworks";
108
120
  //#endregion
109
- export { _ as Fireworks, _ as default };
121
+ export { y as Fireworks, y as default };
@@ -6,3 +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, useCanvasBox } from './use-element-size';
10
+ export type { UseElementSizeOptions, CanvasBoxSizeInput, CanvasBoxSize, UseCanvasBoxOptions, UseCanvasBoxResult } from './use-element-size';
@@ -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;
@@ -2,9 +2,15 @@ import { default as React } from 'react';
2
2
  export interface FireworksProps {
3
3
  width?: number;
4
4
  height?: number;
5
+ /** 为 true 时铺满父容器(父级需有明确高度) */
6
+ fill?: boolean;
5
7
  /** 是否自动燃放,默认 true */
6
8
  auto?: boolean;
7
- /** 画布操作提示 */
9
+ /**
10
+ * 是否响应点击燃放;默认跟随 auto(auto=false 时不拦截指针)
11
+ */
12
+ interactive?: boolean;
13
+ /** 画布操作提示;非 interactive 时不展示 */
8
14
  hint?: string;
9
15
  /** 画面空闲(无火箭/粒子)时回调 */
10
16
  onComplete?: () => void;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cos-design/fireworks",
3
- "version": "3.5.4",
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.4"
55
+ "@cos-design/shared": "3.5.6"
56
56
  }
57
57
  }