@cos-design/scratch-card 3.5.1

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/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ MIT LICENSE
2
+
3
+ Copyright (c) 2021-present jiaxiantao
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/dist/index.cjs ADDED
@@ -0,0 +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={scratchCard:`cos-scratchCard-McIEK`,prize:`cos-prize-X0z5V`,canvas:`cos-canvas-JqIlr`,hidden:`cos-hidden-Lsfbt`},d=({coverColor:e=`#94a3b8`,prize:t=`🎉 恭喜中奖!`,onReveal:n,width:r=300,height:i=180})=>{let a=(0,c.useRef)(null),o=(0,c.useRef)(!1),s=(0,c.useRef)(!1),d=(0,c.useRef)(n),[f,p]=(0,c.useState)(!1);(0,c.useEffect)(()=>{d.current=n},[n]);let m=(0,c.useCallback)(t=>{t.globalCompositeOperation=`source-over`,t.fillStyle=e,t.fillRect(0,0,r,i),t.fillStyle=`rgb(255 255 255 / 30%)`,t.font=`bold 16px system-ui, sans-serif`,t.textAlign=`center`,t.fillText(`刮开涂层`,r/2,i/2)},[e,i,r]),h=(0,c.useCallback)(e=>{if(s.current)return;let t=e.getContext(`2d`);if(!t)return;let n=t.getImageData(0,0,e.width,e.height),r=0;for(let e=3;e<n.data.length;e+=4)n.data[e]<128&&r++;r/(e.width*e.height)>.45&&(s.current=!0,p(!0),d.current?.())},[]);(0,c.useEffect)(()=>{let e=a.current;if(!e)return;let t=window.devicePixelRatio||1;e.width=r*t,e.height=i*t;let n=e.getContext(`2d`);n&&(n.setTransform(t,0,0,t,0,0),m(n))},[m,i,r]);let g=(e,t)=>{let n=a.current;if(!n||s.current)return;let r=n.getContext(`2d`);r&&(r.globalCompositeOperation=`destination-out`,r.beginPath(),r.arc(e,t,20,0,Math.PI*2),r.fill(),h(n))},_=e=>{let t=a.current;if(!t)return null;let n=t.getBoundingClientRect();if(`touches`in e){let t=e.touches[0];return{x:t.clientX-n.left,y:t.clientY-n.top}}return{x:e.clientX-n.left,y:e.clientY-n.top}},v=e=>{o.current=!0;let t=_(e);t&&g(t.x,t.y)},y=e=>{if(!o.current)return;let t=_(e);t&&g(t.x,t.y)},b=()=>{o.current=!1};return(0,l.jsxs)(`div`,{className:u.scratchCard,style:{width:r,height:i},children:[(0,l.jsx)(`div`,{className:u.prize,children:t}),(0,l.jsx)(`canvas`,{ref:a,className:`${u.canvas} ${f?u.hidden:``}`,style:{width:r,height:i},onMouseDown:v,onMouseMove:y,onMouseUp:b,onMouseLeave:b,onTouchStart:v,onTouchMove:y,onTouchEnd:b})]})};exports.ScratchCard=d,exports.default=d;
package/dist/index.css ADDED
@@ -0,0 +1 @@
1
+ .cos-scratchCard-McIEK{border-radius:12px;position:relative;overflow:hidden;box-shadow:0 8px 24px #0003}.cos-prize-X0z5V{color:#b45309;background:linear-gradient(135deg,#fef3c7,#fde68a);justify-content:center;align-items:center;font-size:20px;font-weight:700;display:flex;position:absolute;inset:0}.cos-canvas-JqIlr{z-index:1;cursor:grab;touch-action:none;position:relative}.cos-canvas-JqIlr:active{cursor:grabbing}.cos-canvas-JqIlr.cos-hidden-Lsfbt{opacity:0;pointer-events:none;transition:opacity .4s}
@@ -0,0 +1,3 @@
1
+ export { default } from './src/components/scratchCard/index';
2
+ export { default as ScratchCard } from './src/components/scratchCard/index';
3
+ export type * from './src/components/scratchCard/index';
package/dist/index.js ADDED
@@ -0,0 +1,97 @@
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 = {
4
+ scratchCard: "cos-scratchCard-McIEK",
5
+ prize: "cos-prize-X0z5V",
6
+ canvas: "cos-canvas-JqIlr",
7
+ hidden: "cos-hidden-Lsfbt"
8
+ }, s = ({ coverColor: s = "#94a3b8", prize: c = "🎉 恭喜中奖!", onReveal: l, width: u = 300, height: d = 180 }) => {
9
+ let f = n(null), p = n(!1), m = n(!1), h = n(l), [g, _] = r(!1);
10
+ t(() => {
11
+ h.current = l;
12
+ }, [l]);
13
+ let v = e((e) => {
14
+ e.globalCompositeOperation = "source-over", e.fillStyle = s, e.fillRect(0, 0, u, d), e.fillStyle = "rgb(255 255 255 / 30%)", e.font = "bold 16px system-ui, sans-serif", e.textAlign = "center", e.fillText("刮开涂层", u / 2, d / 2);
15
+ }, [
16
+ s,
17
+ d,
18
+ u
19
+ ]), y = e((e) => {
20
+ if (m.current) return;
21
+ let t = e.getContext("2d");
22
+ if (!t) return;
23
+ let n = t.getImageData(0, 0, e.width, e.height), r = 0;
24
+ for (let e = 3; e < n.data.length; e += 4) n.data[e] < 128 && r++;
25
+ r / (e.width * e.height) > .45 && (m.current = !0, _(!0), h.current?.());
26
+ }, []);
27
+ t(() => {
28
+ let e = f.current;
29
+ if (!e) return;
30
+ let t = window.devicePixelRatio || 1;
31
+ e.width = u * t, e.height = d * t;
32
+ let n = e.getContext("2d");
33
+ n && (n.setTransform(t, 0, 0, t, 0, 0), v(n));
34
+ }, [
35
+ v,
36
+ d,
37
+ u
38
+ ]);
39
+ let b = (e, t) => {
40
+ let n = f.current;
41
+ if (!n || m.current) return;
42
+ let r = n.getContext("2d");
43
+ r && (r.globalCompositeOperation = "destination-out", r.beginPath(), r.arc(e, t, 20, 0, Math.PI * 2), r.fill(), y(n));
44
+ }, x = (e) => {
45
+ let t = f.current;
46
+ if (!t) return null;
47
+ let n = t.getBoundingClientRect();
48
+ if ("touches" in e) {
49
+ let t = e.touches[0];
50
+ return {
51
+ x: t.clientX - n.left,
52
+ y: t.clientY - n.top
53
+ };
54
+ }
55
+ return {
56
+ x: e.clientX - n.left,
57
+ y: e.clientY - n.top
58
+ };
59
+ }, S = (e) => {
60
+ p.current = !0;
61
+ let t = x(e);
62
+ t && b(t.x, t.y);
63
+ }, C = (e) => {
64
+ if (!p.current) return;
65
+ let t = x(e);
66
+ t && b(t.x, t.y);
67
+ }, w = () => {
68
+ p.current = !1;
69
+ };
70
+ return /* @__PURE__ */ a("div", {
71
+ className: o.scratchCard,
72
+ style: {
73
+ width: u,
74
+ height: d
75
+ },
76
+ children: [/* @__PURE__ */ i("div", {
77
+ className: o.prize,
78
+ children: c
79
+ }), /* @__PURE__ */ i("canvas", {
80
+ ref: f,
81
+ className: `${o.canvas} ${g ? o.hidden : ""}`,
82
+ style: {
83
+ width: u,
84
+ height: d
85
+ },
86
+ onMouseDown: S,
87
+ onMouseMove: C,
88
+ onMouseUp: w,
89
+ onMouseLeave: w,
90
+ onTouchStart: S,
91
+ onTouchMove: C,
92
+ onTouchEnd: w
93
+ })]
94
+ });
95
+ };
96
+ //#endregion
97
+ export { s as ScratchCard, s as default };
@@ -0,0 +1,3 @@
1
+ export { default } from '../../../src/components/scratchCard';
2
+ export { default as ScratchCard } from '../../../src/components/scratchCard';
3
+ export type * from '../../../src/components/scratchCard';
@@ -0,0 +1,13 @@
1
+ import { default as React } from 'react';
2
+ export interface ScratchCardProps {
3
+ /** 涂层颜色 */
4
+ coverColor?: string;
5
+ /** 奖品文字 */
6
+ prize?: string;
7
+ /** 刮开完成回调 */
8
+ onReveal?: () => void;
9
+ width?: number;
10
+ height?: number;
11
+ }
12
+ declare const ScratchCard: React.FC<ScratchCardProps>;
13
+ export default ScratchCard;
@@ -0,0 +1,39 @@
1
+ /// <reference types="vite/client" />
2
+
3
+ declare module '*.module.less' {
4
+ const classes: { readonly [key: string]: string };
5
+ export default classes;
6
+ }
7
+
8
+ declare module '*.less' {
9
+ const classes: { readonly [key: string]: string };
10
+ export default classes;
11
+ }
12
+
13
+ declare module '*.png' {
14
+ const src: string;
15
+ export default src;
16
+ }
17
+
18
+ declare module '*.jpg' {
19
+ const src: string;
20
+ export default src;
21
+ }
22
+
23
+ declare module '*.mp4' {
24
+ const src: string;
25
+ export default src;
26
+ }
27
+
28
+ declare module '*.mp3' {
29
+ const src: string;
30
+ export default src;
31
+ }
32
+
33
+ declare module '*.svg' {
34
+ import type { FC, SVGProps } from 'react';
35
+
36
+ export const ReactComponent: FC<SVGProps<SVGSVGElement>>;
37
+ const src: string;
38
+ export default src;
39
+ }
package/package.json ADDED
@@ -0,0 +1,54 @@
1
+ {
2
+ "name": "@cos-design/scratch-card",
3
+ "version": "3.5.1",
4
+ "description": "ScratchCard component from cos-design",
5
+ "type": "module",
6
+ "main": "./dist/index.cjs",
7
+ "module": "./dist/index.js",
8
+ "types": "./dist/index.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "import": {
12
+ "types": "./dist/index.d.ts",
13
+ "default": "./dist/index.js"
14
+ },
15
+ "require": {
16
+ "types": "./dist/index.d.ts",
17
+ "default": "./dist/index.cjs"
18
+ }
19
+ }
20
+ },
21
+ "files": [
22
+ "dist",
23
+ "LICENSE"
24
+ ],
25
+ "sideEffects": [
26
+ "**/*.css",
27
+ "**/*.less"
28
+ ],
29
+ "keywords": [
30
+ "cos-design",
31
+ "react",
32
+ "scratchCard",
33
+ "ScratchCard"
34
+ ],
35
+ "homepage": "https://jiaxiantao.github.io/cos-design/",
36
+ "bugs": {
37
+ "url": "https://github.com/jiaxiantao/cos-design/issues"
38
+ },
39
+ "repository": {
40
+ "type": "git",
41
+ "url": "git+https://github.com/jiaxiantao/cos-design.git",
42
+ "directory": "packages/scratchCard"
43
+ },
44
+ "author": "jiaxiantao <jiaxiantao@souche.com>",
45
+ "license": "MIT",
46
+ "peerDependencies": {
47
+ "react": ">=18.0.0",
48
+ "react-dom": ">=18.0.0"
49
+ },
50
+ "publishConfig": {
51
+ "access": "public",
52
+ "registry": "https://registry.npmjs.org"
53
+ }
54
+ }