@cos-design/rope-chain 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("@cos-design/shared"),u=require("react/jsx-runtime");var d={ropeChain:`cos-ropeChain-gYaSK`,canvas:`cos-canvas-5i95N`},f=({width:e=400,height:t=400,segments:n=16,color:r=`#38bdf8`,gravity:i=.4})=>{let a=(0,c.useRef)(null),o=(0,c.useRef)([]),s=(0,c.useRef)({active:!1,index:-1});return(0,c.useEffect)(()=>{let r=Math.max(4,Math.min(n,30)),i=t*.7/r,a=e/2,s=t*.08;o.current=Array.from({length:r+1},(e,t)=>({x:a,y:s+t*i,prevX:a,prevY:s+t*i,pinned:t===0}))},[t,n,e]),(0,c.useEffect)(()=>{let c=a.current;if(!c)return;let u=window.devicePixelRatio||1;c.width=e*u,c.height=t*u;let d=c.getContext(`2d`);if(!d)return;d.setTransform(u,0,0,u,0,0);let f=Math.max(4,Math.min(n,30)),p=t*.7/f,m=(e,t)=>{let n=o.current,r=-1,i=30;return n.forEach((n,a)=>{let o=Math.hypot(n.x-e,n.y-t);o<i&&(i=o,r=a)}),r},h=(e,t)=>{let n=c.getBoundingClientRect(),r=e-n.left,i=t-n.top,a=m(r,i);if(a>=0){s.current={active:!0,index:a};let e=o.current[a];e.x=r,e.y=i,e.prevX=r,e.prevY=i}},g=(e,t)=>{if(!s.current.active)return;let n=c.getBoundingClientRect(),r=o.current[s.current.index];r.x=e-n.left,r.y=t-n.top},_=()=>{s.current={active:!1,index:-1}},v=e=>h(e.clientX,e.clientY),y=e=>g(e.clientX,e.clientY),b=e=>{e.touches[0]&&h(e.touches[0].clientX,e.touches[0].clientY)},x=e=>{e.touches[0]&&g(e.touches[0].clientX,e.touches[0].clientY)};c.addEventListener(`mousedown`,v),c.addEventListener(`mousemove`,y),window.addEventListener(`mouseup`,_),c.addEventListener(`touchstart`,b,{passive:!0}),c.addEventListener(`touchmove`,x,{passive:!0}),window.addEventListener(`touchend`,_);let S=0,C=document.hidden,w=(0,l.bindVisibilityPause)(e=>{C=e}),T=()=>{if(S=requestAnimationFrame(T),C)return;let n=o.current,a=s.current;for(let e of n){if(e.pinned||a.active&&n.indexOf(e)===a.index)continue;let t=(e.x-e.prevX)*.99,r=(e.y-e.prevY)*.99;e.prevX=e.x,e.prevY=e.y,e.x+=t,e.y+=r,e.y+=i}for(let e=0;e<6;e++)for(let e=0;e<n.length-1;e++){let t=n[e],r=n[e+1],i=r.x-t.x,o=r.y-t.y,s=Math.hypot(i,o)||.001,c=(s-p)/s,l=i*c*.5,u=o*c*.5;!t.pinned&&!(a.active&&e===a.index)&&(t.x+=l,t.y+=u),!r.pinned&&!(a.active&&e+1===a.index)&&(r.x-=l,r.y-=u)}d.fillStyle=`#0f172a`,d.fillRect(0,0,e,t),d.beginPath(),n.forEach((e,t)=>t===0?d.moveTo(e.x,e.y):d.lineTo(e.x,e.y)),d.strokeStyle=r,d.lineWidth=3,d.lineCap=`round`,d.lineJoin=`round`,d.shadowColor=r,d.shadowBlur=8,d.stroke(),d.shadowBlur=0,n.forEach((e,t)=>{(t===0||t===n.length-1||t%4==0)&&(d.beginPath(),d.arc(e.x,e.y,t===0?6:4,0,Math.PI*2),d.fillStyle=t===0?`#94a3b8`:r,d.fill())})};return T(),()=>{cancelAnimationFrame(S),w(),c.removeEventListener(`mousedown`,v),c.removeEventListener(`mousemove`,y),window.removeEventListener(`mouseup`,_),c.removeEventListener(`touchstart`,b),c.removeEventListener(`touchmove`,x),window.removeEventListener(`touchend`,_)}},[r,i,t,n,e]),(0,u.jsx)(`div`,{className:d.ropeChain,style:{width:e,height:t},children:(0,u.jsx)(`canvas`,{ref:a,className:d.canvas,style:{width:e,height:t}})})};exports.RopeChain=f,exports.default=f;
package/dist/index.css ADDED
@@ -0,0 +1 @@
1
+ .cos-ropeChain-gYaSK{cursor:grab;background:#0f172a;border-radius:12px;overflow:hidden}.cos-ropeChain-gYaSK:active{cursor:grabbing}.cos-canvas-5i95N{display:block}
@@ -0,0 +1,3 @@
1
+ export { default } from './src/components/ropeChain/index';
2
+ export { default as RopeChain } from './src/components/ropeChain/index';
3
+ export type * from './src/components/ropeChain/index';
package/dist/index.js ADDED
@@ -0,0 +1,108 @@
1
+ import { useEffect as e, useRef as t } from "react";
2
+ import { bindVisibilityPause as n } from "@cos-design/shared";
3
+ import { jsx as r } from "react/jsx-runtime";
4
+ import './index.css';var i = {
5
+ ropeChain: "cos-ropeChain-gYaSK",
6
+ canvas: "cos-canvas-5i95N"
7
+ }, a = ({ width: a = 400, height: o = 400, segments: s = 16, color: c = "#38bdf8", gravity: l = .4 }) => {
8
+ let u = t(null), d = t([]), f = t({
9
+ active: !1,
10
+ index: -1
11
+ });
12
+ return e(() => {
13
+ let e = Math.max(4, Math.min(s, 30)), t = o * .7 / e, n = a / 2, r = o * .08;
14
+ d.current = Array.from({ length: e + 1 }, (e, i) => ({
15
+ x: n,
16
+ y: r + i * t,
17
+ prevX: n,
18
+ prevY: r + i * t,
19
+ pinned: i === 0
20
+ }));
21
+ }, [
22
+ o,
23
+ s,
24
+ a
25
+ ]), e(() => {
26
+ let e = u.current;
27
+ if (!e) return;
28
+ let t = window.devicePixelRatio || 1;
29
+ e.width = a * t, e.height = o * t;
30
+ let r = e.getContext("2d");
31
+ if (!r) return;
32
+ r.setTransform(t, 0, 0, t, 0, 0);
33
+ let i = Math.max(4, Math.min(s, 30)), p = o * .7 / i, m = (e, t) => {
34
+ let n = d.current, r = -1, i = 30;
35
+ return n.forEach((n, a) => {
36
+ let o = Math.hypot(n.x - e, n.y - t);
37
+ o < i && (i = o, r = a);
38
+ }), r;
39
+ }, h = (t, n) => {
40
+ let r = e.getBoundingClientRect(), i = t - r.left, a = n - r.top, o = m(i, a);
41
+ if (o >= 0) {
42
+ f.current = {
43
+ active: !0,
44
+ index: o
45
+ };
46
+ let e = d.current[o];
47
+ e.x = i, e.y = a, e.prevX = i, e.prevY = a;
48
+ }
49
+ }, g = (t, n) => {
50
+ if (!f.current.active) return;
51
+ let r = e.getBoundingClientRect(), i = d.current[f.current.index];
52
+ i.x = t - r.left, i.y = n - r.top;
53
+ }, _ = () => {
54
+ f.current = {
55
+ active: !1,
56
+ index: -1
57
+ };
58
+ }, v = (e) => h(e.clientX, e.clientY), y = (e) => g(e.clientX, e.clientY), b = (e) => {
59
+ e.touches[0] && h(e.touches[0].clientX, e.touches[0].clientY);
60
+ }, x = (e) => {
61
+ e.touches[0] && g(e.touches[0].clientX, e.touches[0].clientY);
62
+ };
63
+ e.addEventListener("mousedown", v), e.addEventListener("mousemove", y), window.addEventListener("mouseup", _), e.addEventListener("touchstart", b, { passive: !0 }), e.addEventListener("touchmove", x, { passive: !0 }), window.addEventListener("touchend", _);
64
+ let S = 0, C = document.hidden, w = n((e) => {
65
+ C = e;
66
+ }), T = () => {
67
+ if (S = requestAnimationFrame(T), C) return;
68
+ let e = d.current, t = f.current;
69
+ for (let n of e) {
70
+ if (n.pinned || t.active && e.indexOf(n) === t.index) continue;
71
+ let r = (n.x - n.prevX) * .99, i = (n.y - n.prevY) * .99;
72
+ n.prevX = n.x, n.prevY = n.y, n.x += r, n.y += i, n.y += l;
73
+ }
74
+ for (let n = 0; n < 6; n++) for (let n = 0; n < e.length - 1; n++) {
75
+ let r = e[n], i = e[n + 1], a = i.x - r.x, o = i.y - r.y, s = Math.hypot(a, o) || .001, c = (s - p) / s, l = a * c * .5, u = o * c * .5;
76
+ !r.pinned && !(t.active && n === t.index) && (r.x += l, r.y += u), !i.pinned && !(t.active && n + 1 === t.index) && (i.x -= l, i.y -= u);
77
+ }
78
+ r.fillStyle = "#0f172a", r.fillRect(0, 0, a, o), r.beginPath(), e.forEach((e, t) => t === 0 ? r.moveTo(e.x, e.y) : r.lineTo(e.x, e.y)), r.strokeStyle = c, r.lineWidth = 3, r.lineCap = "round", r.lineJoin = "round", r.shadowColor = c, r.shadowBlur = 8, r.stroke(), r.shadowBlur = 0, e.forEach((t, n) => {
79
+ (n === 0 || n === e.length - 1 || n % 4 == 0) && (r.beginPath(), r.arc(t.x, t.y, n === 0 ? 6 : 4, 0, Math.PI * 2), r.fillStyle = n === 0 ? "#94a3b8" : c, r.fill());
80
+ });
81
+ };
82
+ return T(), () => {
83
+ cancelAnimationFrame(S), w(), e.removeEventListener("mousedown", v), e.removeEventListener("mousemove", y), window.removeEventListener("mouseup", _), e.removeEventListener("touchstart", b), e.removeEventListener("touchmove", x), window.removeEventListener("touchend", _);
84
+ };
85
+ }, [
86
+ c,
87
+ l,
88
+ o,
89
+ s,
90
+ a
91
+ ]), /* @__PURE__ */ r("div", {
92
+ className: i.ropeChain,
93
+ style: {
94
+ width: a,
95
+ height: o
96
+ },
97
+ children: /* @__PURE__ */ r("canvas", {
98
+ ref: u,
99
+ className: i.canvas,
100
+ style: {
101
+ width: a,
102
+ height: o
103
+ }
104
+ })
105
+ });
106
+ };
107
+ //#endregion
108
+ export { a as RopeChain, a as default };
@@ -0,0 +1,3 @@
1
+ export { default } from '../../../src/components/ropeChain';
2
+ export { default as RopeChain } from '../../../src/components/ropeChain';
3
+ export type * from '../../../src/components/ropeChain';
@@ -0,0 +1,3 @@
1
+ /** 注册 visibilitychange,页面隐藏时暂停动画循环 */
2
+ export declare const bindVisibilityPause: (onChange: (paused: boolean) => void) => () => void;
3
+ export declare const clamp: (value: number, min: number, max: number) => number;
@@ -0,0 +1,10 @@
1
+ import { default as React } from 'react';
2
+ export interface RopeChainProps {
3
+ width?: number;
4
+ height?: number;
5
+ segments?: number;
6
+ color?: string;
7
+ gravity?: number;
8
+ }
9
+ declare const RopeChain: React.FC<RopeChainProps>;
10
+ export default RopeChain;
@@ -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,57 @@
1
+ {
2
+ "name": "@cos-design/rope-chain",
3
+ "version": "3.5.1",
4
+ "description": "RopeChain 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
+ "ropeChain",
33
+ "RopeChain"
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/ropeChain"
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
+ "dependencies": {
55
+ "@cos-design/shared": "3.5.1"
56
+ }
57
+ }