@cs-open/react-fabric
Version:
1 lines • 14.5 kB
Source Map (JSON)
{"version":3,"file":"business.mjs","sources":["../../../src/utils/business.ts"],"sourcesContent":["import type { Canvas, FabricObject } from 'fabric'\nimport { Path, util } from 'fabric'\n\n/**\n * 将points转换为rect\n * @param points 支持字符串(以逗号分隔的8个数字)或数字数组\n * @returns 返回矩形属性或null\n */\nexport const getRectProps = (points: string | number[] | undefined, strokeWidth = 1) => {\n if (!points) {\n console.warn('getRectProps: points is required')\n return null\n }\n\n const coordinates = Array.isArray(points) ? points : points.split(',').map(Number)\n\n if (coordinates.length === 0) {\n console.warn('getRectProps: points array is empty')\n return null\n }\n\n if (coordinates.length !== 8) {\n console.warn(`getRectProps: points must contain exactly 8 numbers, got ${coordinates.length}`)\n return null\n }\n\n if (coordinates.some(n => Number.isNaN(n))) {\n console.warn('getRectProps: all points must be valid numbers')\n return null\n }\n\n // 按顺时针顺序:左上(x1,y1)、右上(x2,y2)、右下(x3,y3)、左下(x4,y4)\n\n const [x1, y1, x2, _y2, _x3, y3, _x4, _y4] = coordinates\n const result = {\n left: Number(x1),\n top: Number(y1),\n width: Math.abs(Number(x2) - Number(x1)) - strokeWidth,\n height: Math.abs(Number(y3) - Number(y1)) - strokeWidth,\n strokeWidth,\n }\n\n return result\n}\n// 将object转换成points\nexport const getObjectPoints = (object: FabricObject) => {\n if (!object) {\n console.warn('getObjectPoints: object is required')\n return null\n }\n const { left, top, width, height } = object\n return [left, top, left + width, top, left + width, top + height, left, top + height]\n}\n\n/**\n * 获取路径的宽高\n * @param pathString\n * @returns\n */\nexport const getPathBounds = (pathString: string) => {\n const path = new Path(util.parsePath(pathString))\n return {\n width: path.width!,\n height: path.height!,\n }\n}\n\nconst 错误Path =\n 'M0.0654297 47.7528C1.00227 47.8765 1.87859 47.6528 2.54701 47.4107C3.28908 47.1264 4.00202 46.7713 4.67596 46.3502C6.08911 45.4897 7.7049 44.2581 9.43385 42.7949C12.9023 39.8581 17.0917 35.7633 21.4391 31.2476C22.5023 30.1423 23.576 29.0133 24.6549 27.866C25.6549 28.8054 26.6365 29.7423 27.597 30.666C32.0707 34.9765 36.0154 39.0186 38.7812 42.2844C40.1654 43.9239 41.2023 45.3002 41.8523 46.3739C42.1786 46.9107 42.3681 47.3081 42.4681 47.5765C42.5207 47.7212 42.5312 47.787 42.5312 47.787C42.5312 47.787 42.5181 47.7081 42.5365 47.5686L47.7549 48.2502C47.876 47.316 47.6549 46.437 47.4128 45.7712C47.1279 45.0281 46.7719 44.3143 46.3496 43.6397C45.4917 42.2265 44.2575 40.6107 42.797 38.8818C39.8602 35.4133 35.7654 31.2265 31.247 26.8765C30.2628 25.9291 29.2575 24.9712 28.2338 24.0081C34.2525 17.4362 40.131 10.7373 45.8654 3.91597L41.8338 0.534393C36.1573 7.28575 30.3384 13.9162 24.3812 20.4212C17.6908 14.2829 10.8681 8.29029 3.91806 2.44755L0.533851 6.48176C6.03385 11.0949 13.5838 17.6397 20.7996 24.2818C19.7417 25.4081 18.6891 26.516 17.6496 27.5976C13.3391 32.0712 9.29701 36.0133 6.03385 38.7765C4.39438 40.166 3.01543 41.1976 1.94438 41.8502C1.56348 42.0923 1.16087 42.2985 0.741745 42.466C0.594377 42.5186 0.531219 42.5291 0.531219 42.5291C0.531219 42.5291 0.610166 42.516 0.747009 42.5344L0.0654297 47.7528V47.7528Z'\n\nconst 正确Path =\n 'M46.5079 2.50162L49.9999 5.55541L47.3484 10.151L45.4673 13.2534L44.0748 15.5229L42.2457 18.4638L40.4512 21.3007L38.6971 24.0242L36.9853 26.6284L35.7332 28.496L34.508 30.2886L33.3155 32.0004L32.1557 33.6256L31.3998 34.6623L30.2977 36.1414L29.5822 37.0762L28.8841 37.9667L27.8724 39.2226L27.2184 40.0016L26.5837 40.7344L25.9702 41.421L25.3758 42.0577L24.8027 42.6462L24.5238 42.9213L23.9814 43.431L23.4621 43.8907C22.1888 44.9677 21.1271 45.5467 20.3116 45.5467C18.6498 45.5467 16.3899 44.3196 13.7953 42.25L12.9644 41.5691L12.1142 40.8363C11.8257 40.5825 11.5372 40.3209 11.2429 40.0516L10.3582 39.2188L9.45422 38.3417L8.54062 37.4224L7.6174 36.4626L6.68072 35.4663L5.73827 34.4354L4.79005 33.3737L3.83799 32.2831L2.88207 31.1676L1.92808 30.027L0.974095 28.8653L0.499023 28.2787L4.30734 24.7839L5.12477 25.7899L5.95759 26.79L6.7981 27.7805L8.06944 29.2423L8.92149 30.1963L9.77162 31.1291L10.6179 32.0369L11.4546 32.9198L12.2816 33.7699L13.0952 34.5873L13.8934 35.3682L14.6704 36.1087L15.0513 36.4626L15.7937 37.1358L16.5072 37.7609L17.192 38.3321L17.5228 38.5995L18.1556 39.088L18.7499 39.5169C19.8039 40.2478 19.7596 40.0515 20.3116 40.0515C20.4212 40.0515 20.6001 39.9495 20.8444 39.7534L21.1098 39.5264L21.4156 39.2379L21.7618 38.8917L22.3465 38.2589L22.7831 37.7685L23.2524 37.2222L24.016 36.3067L24.8469 35.2758L25.4374 34.5276L26.6895 32.8927L27.6896 31.5521L28.736 30.1173L29.8207 28.5978L31.321 26.4398C34.5388 21.7545 38.1547 16.1537 41.5821 10.6221L43.6113 7.31965C44.2768 6.22718 44.9307 5.14625 45.5673 4.08262L46.5079 2.50162Z'\n\nconst 问号Path =\n 'M27.862 45.101L27.962 45.412L28.2036 46.3368C28.378 47.0217 28.2849 47.7472 27.943 48.3657C27.6012 48.9842 27.0364 49.4492 26.3638 49.6658C25.6911 49.8825 24.9612 49.8346 24.3226 49.5319C23.6841 49.2291 23.1849 48.6944 22.9269 48.0365L22.8241 47.731L22.5853 46.8034C22.4136 46.1438 22.4905 45.4441 22.8013 44.8375C23.1121 44.2309 23.6351 43.7598 24.2708 43.5138C24.9064 43.2679 25.6103 43.2643 26.2485 43.5037C26.8866 43.7431 27.4144 44.2088 27.7315 44.8121L27.862 45.101V45.101ZM28.2342 0.320863C35.9965 1.78169 40.7539 7.85272 38.3433 15.5123C37.2435 19.0116 35.7827 20.9557 32.9888 23.283L30.8225 25.0382L30.4448 25.3715C29.034 26.6296 28.0009 27.9266 27.0705 29.7873C25.7346 32.4534 25.5541 33.656 25.9041 35.0668L26.1762 36.0277C26.2595 36.3416 26.3262 36.6221 26.3706 36.9165C26.4151 37.2025 26.4373 37.4858 26.4373 37.7746C26.4365 38.4825 26.1655 39.1633 25.6795 39.6781C25.1936 40.1928 24.5295 40.5025 23.8228 40.544C23.1162 40.5855 22.4204 40.3556 21.8775 39.9013C21.3347 39.4469 20.9859 38.8025 20.9023 38.0996L20.8689 37.6913L20.6523 36.922C19.8108 34.0281 20.0413 31.4148 22.102 27.3017C23.1077 25.2436 24.4712 23.3807 26.129 21.8L27.2621 20.7724L28.9396 19.4199C31.3308 17.5036 32.2584 16.3538 33.0443 13.8488C34.3496 9.70791 31.9723 6.67517 27.2066 5.7809C22.152 4.82831 17.0558 6.93346 15.0701 11.2243C14.9275 11.5707 14.7163 11.8847 14.4492 12.1474C14.1822 12.41 13.8647 12.616 13.516 12.7527C13.1672 12.8895 12.7944 12.9544 12.42 12.9433C12.0455 12.9323 11.6772 12.8456 11.3371 12.6885C10.9971 12.5314 10.6923 12.3071 10.4412 12.0292C10.19 11.7512 9.99771 11.4253 9.8758 11.0711C9.75389 10.7169 9.70489 10.3417 9.73177 9.96806C9.75864 9.59442 9.86082 9.23008 10.0322 8.89696C13.1732 2.09829 20.8467 -1.06775 28.2342 0.320863Z'\n\nconst 半对Path =\n 'M47.1047 5.83231L43.7817 2.77777L42.8867 4.35916C42.2809 5.42304 41.6586 6.50424 41.0253 7.59698L39.0944 10.9002C38.1277 12.5401 37.1453 14.1861 36.1616 15.8125L34.792 14.4103L32.9661 12.5637L30.4656 10.0614L26.7341 6.48651L23.1841 9.24437L24.462 10.5019C25.321 11.3517 26.1925 12.2222 27.0724 13.1063L29.7286 15.7962C31.0497 17.1441 32.3732 18.5096 33.678 19.872C32.1854 22.2824 30.7187 24.5963 29.3299 26.7218L27.9023 28.8804L26.8701 30.4002L25.8744 31.8354L24.9227 33.1763L23.7312 34.8116L23.1693 35.5599L22.3786 36.5911L21.652 37.5068L21.2054 38.0532L20.7899 38.5438L20.2335 39.1767L19.9041 39.523L19.6131 39.8116L19.3605 40.0386C19.1281 40.2348 18.9578 40.3368 18.8535 40.3368C18.7508 40.3368 18.6698 40.3443 18.5984 40.3509C18.3048 40.3781 18.1743 40.3902 17.3674 39.8021L16.8018 39.3731L16.1997 38.8844L15.8849 38.617L15.2333 38.0456L14.5543 37.4204L13.8478 36.7471L13.4854 36.3931L12.746 35.6524L11.9864 34.8713L11.2122 34.0537L10.4252 33.2033L9.62905 32.3203L8.82374 31.4123L8.01476 30.4792L7.20396 29.525L5.99415 28.0629L5.19433 27.0721L4.40183 26.0717L3.62397 25.0655L0 28.5611L0.452074 29.1479L1.35988 30.3099L2.26769 31.4507L3.17733 32.5666L4.08331 33.6574L4.98563 34.7193L5.88246 35.7505L6.7738 36.7471L7.65232 37.707L8.5217 38.6266L9.38192 39.5039L10.2238 40.3369C10.5039 40.6063 10.7784 40.8679 11.0529 41.1219L11.8619 41.8548L12.6526 42.5359C15.1216 44.6059 17.2722 45.8333 18.8535 45.8333C19.6295 45.8333 20.6399 45.2543 21.8515 44.1769L22.3457 43.7171L22.8618 43.2073L23.1272 42.9322L23.6726 42.3435L24.2381 41.7067L24.822 41.0199L25.426 40.2869L26.0483 39.5078L27.011 38.2515L27.6754 37.3608L28.3562 36.4258L29.405 34.9463L30.1243 33.9094L31.2279 32.2838L32.3627 30.5715L33.5286 28.7785L34.7201 26.9105L36.349 24.3056L36.9499 23.3249C38.8545 25.3573 40.6757 27.3426 42.3409 29.2094L44.0412 31.1374L45.2325 32.5227L46.353 33.8523L50 30.8934L49.1904 29.9328L47.916 28.4516L46.5678 26.9199L45.1512 25.3389L43.6703 23.7155L41.5984 21.4853L39.4451 19.2102L39.7258 18.7438L41.4664 15.8023L42.7915 13.5321L44.5815 10.429L47.1047 5.83231Z'\n\n// 1正确;2错误;3问好存疑;4半对;9半对(不知道谁写的,跟4有什么区别);5 无痕迹,占位使用\nexport const PATH_LIST = [\n {\n name: '正确',\n scale: 0.02,\n path: 正确Path,\n ...getPathBounds(正确Path),\n\n midScale: 0.03,\n result: 1,\n },\n {\n name: '错误',\n scale: 0.02,\n path: 错误Path,\n ...getPathBounds(错误Path),\n midScale: 0.03,\n result: 2,\n },\n {\n name: '问号存疑',\n scale: 0.02,\n path: 问号Path,\n ...getPathBounds(问号Path),\n midScale: 0.03,\n result: 3,\n },\n {\n name: '半对',\n scale: 1,\n path: 半对Path,\n ...getPathBounds(半对Path),\n midScale: 0.3,\n result: 4,\n },\n {\n name: '占位', // 无痕迹,占位使用\n scale: 1,\n path: 'M 0 0',\n ...getPathBounds('M 0 0'),\n midScale: 0.3,\n result: 5,\n },\n] as const\n\n// 禁用所有对象并获取还原函数\nexport const disableCanvasObjects = (canvas: Canvas) => {\n // 保存所有对象的原始状态\n const originalStates = canvas.getObjects().map((obj: any) => ({\n object: obj,\n selectable: obj.selectable,\n evented: obj.evented,\n }))\n\n // 禁用所有对象的事件和选择\n canvas.getObjects().forEach((obj: any) => {\n obj.selectable = false\n obj.evented = false\n })\n\n // 返回还原函数\n return () => {\n originalStates.forEach((state: any) => {\n state.object.selectable = state.selectable\n state.object.evented = state.evented\n })\n }\n}\n\n/**\n * 计算相对 Text 字体大小\n */\nexport const getRelativeTextFontSize = (naturalWidth: number, width: number, defaultFontSize?: number) => {\n return Math.round((naturalWidth / width) * (defaultFontSize || 14))\n}\n\n/**\n * 还原痕迹至原图点位\n * @param brush 痕迹对象,canvas toJSON 后的对象\n * @param scale 当前缩放, imgTop,imgLeft 图片基于居中偏移\n * @param expanded_points 基于原图的点位,字符串\n */\nexport const restore2Origin = ({\n brush,\n scale = 1,\n expanded_points,\n imgTop = 0,\n imgLeft = 0,\n}: {\n brush: any\n scale?: number\n expanded_points?: string\n imgTop?: number\n imgLeft?: number\n}) => {\n const obj = JSON.parse(JSON.stringify(brush))\n const [left, top] = expanded_points ? expanded_points.split(',') : [0, 0]\n const item_scale = brush.scaleX || 1\n obj.top = (brush.top - imgTop) / scale + +top\n obj.left = (brush.left - imgLeft) / scale + +left\n obj.scaleX = item_scale / scale\n obj.scaleY = item_scale / scale\n return obj\n}\n"],"names":["getRectProps","points","strokeWidth","coordinates","n","x1","y1","x2","_y2","_x3","y3","_x4","_y4","getObjectPoints","object","left","top","width","height","getPathBounds","pathString","path","Path","util","错误Path","正确Path","问号Path","半对Path","PATH_LIST","disableCanvasObjects","canvas","originalStates","obj","state","getRelativeTextFontSize","naturalWidth","defaultFontSize","restore2Origin","brush","scale","expanded_points","imgTop","imgLeft","item_scale"],"mappings":"qDAQa,MAAAA,EAAe,CAACC,EAAuCC,EAAc,IAAM,CACpF,GAAI,CAACD,EACD,OAAA,QAAQ,KAAK,kCAAkC,EACxC,KAGX,MAAME,EAAc,MAAM,QAAQF,CAAM,EAAIA,EAASA,EAAO,MAAM,GAAG,EAAE,IAAI,MAAM,EAEjF,GAAIE,EAAY,SAAW,EACvB,OAAA,QAAQ,KAAK,qCAAqC,EAC3C,KAGX,GAAIA,EAAY,SAAW,EACvB,OAAQ,QAAA,KAAK,4DAA4DA,EAAY,MAAM,EAAE,EACtF,KAGX,GAAIA,EAAY,KAAKC,GAAK,OAAO,MAAMA,CAAC,CAAC,EACrC,OAAA,QAAQ,KAAK,gDAAgD,EACtD,KAKX,KAAM,CAACC,EAAIC,EAAIC,EAAIC,EAAKC,EAAKC,EAAIC,EAAKC,CAAG,EAAIT,EAS7C,MARe,CACX,KAAM,OAAOE,CAAE,EACf,IAAK,OAAOC,CAAE,EACd,MAAO,KAAK,IAAI,OAAOC,CAAE,EAAI,OAAOF,CAAE,CAAC,EAAIH,EAC3C,OAAQ,KAAK,IAAI,OAAOQ,CAAE,EAAI,OAAOJ,CAAE,CAAC,EAAIJ,EAC5C,YAAAA,CACJ,CAGJ,EAEaW,EAAmBC,GAAyB,CACrD,GAAI,CAACA,EACD,OAAA,QAAQ,KAAK,qCAAqC,EA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