|
1
|
-
{"version":3,"file":"index28.cjs","sources":["../src/rough/roughPathExtractor.js"],"sourcesContent":["/**\n * RoughJS Path Extractor - Utility functions for hand-drawn style paths\n *\n * Wraps RoughJS Generator API to extract path data as points arrays,\n * compatible with the existing StrokeRenderer pipeline.\n *\n * @module rough/roughPathExtractor\n */\n\nimport rough from 'roughjs';\n\nconst generator = rough.generator();\n\n/**\n * Cubic bezier interpolation\n *\n * @param {Array} p0 - Start point [x, y]\n * @param {Array} p1 - Control point 1 [x, y]\n * @param {Array} p2 - Control point 2 [x, y]\n * @param {Array} p3 - End point [x, y]\n * @param {number} t - Interpolation factor (0-1)\n * @returns {Array} Interpolated point [x, y]\n */\nfunction bezierPoint(p0, p1, p2, p3, t) {\n const mt = 1 - t;\n return [\n mt ** 3 * p0[0] + 3 * mt ** 2 * t * p1[0] + 3 * mt * t ** 2 * p2[0] + t ** 3 * p3[0],\n mt ** 3 * p0[1] + 3 * mt ** 2 * t * p1[1] + 3 * mt * t ** 2 * p2[1] + t ** 3 * p3[1]\n ];\n}\n\n/**\n * Convert RoughJS ops array to points array\n *\n * Processes move, lineTo, and bcurveTo operations.\n * Bezier curves are linearized with 4 intermediate points.\n *\n * @param {Array} ops - Array of RoughJS op objects\n * @returns {Array} Array of [x, y] points\n */\nfunction opsToPoints(ops) {\n const points = [];\n let current = [0, 0];\n\n for (const { op, data } of ops) {\n switch (op) {\n case 'move':\n current = [data[0], data[1]];\n points.push([...current]);\n break;\n\n case 'lineTo':\n current = [data[0], data[1]];\n points.push([...current]);\n break;\n\n case 'bcurveTo': {\n const [cx1, cy1, cx2, cy2, x, y] = data;\n const p0 = current;\n const p1 = [cx1, cy1];\n const p2 = [cx2, cy2];\n const p3 = [x, y];\n\n for (let t = 0.25; t <= 1; t += 0.25) {\n points.push(bezierPoint(p0, p1, p2, p3, t));\n }\n current = [x, y];\n break;\n }\n }\n }\n\n return points;\n}\n\n/**\n * Extract points from a RoughJS drawable\n *\n * Collects all path operations from drawable.sets and converts to points.\n *\n * @param {Object} drawable - RoughJS drawable object\n * @returns {Array} Array of [x, y] points\n */\nfunction extractPoints(drawable) {\n const allPoints = [];\n\n for (const set of drawable.sets) {\n if (set.type === 'path') {\n allPoints.push(...opsToPoints(set.ops));\n }\n }\n\n return allPoints;\n}\n\n/**\n * Generate a hash seed from a string\n *\n * Produces a deterministic number from string input for reproducible randomness.\n *\n * @param {string} str - Input string (e.g., annotation ID)\n * @returns {number} Hash value for use as seed\n */\nexport function hashSeed(str) {\n let hash = 0;\n for (let i = 0; i < str.length; i++) {\n hash = ((hash << 5) - hash) + str.charCodeAt(i);\n hash |= 0;\n }\n return Math.abs(hash);\n}\n\n/**\n * Convert a line to RoughJS style points\n *\n * @param {number} x1 - Start x coordinate\n * @param {number} y1 - Start y coordinate\n * @param {number} x2 - End x coordinate\n * @param {number} y2 - End y coordinate\n * @param {Object} [options={}] - RoughJS options\n * @param {number} [options.roughness=1.0] - Line irregularity (0=clean, 3+=very rough)\n * @param {number} [options.bowing=1.0] - Line curvature (0=straight, 2+=very curved)\n * @param {number} [options.seed] - Seed for reproducible randomness\n * @param {number} [options.curveFitting=0.95] - Curve fitting (0=loose, 1=tight)\n * @param {number} [options.curveStepCount=9] - Curve resolution (higher=smoother)\n * @param {number} [options.maxRandomnessOffset=2] - Max random offset\n * @param {boolean} [options.disableMultiStroke=false] - Disable double-stroke effect\n * @returns {Array} Array of [x, y] points with hand-drawn style\n */\nexport function roughLine(x1, y1, x2, y2, options = {}) {\n const drawable = generator.line(x1, y1, x2, y2, {\n roughness: options.roughness ?? 1.0,\n bowing: options.bowing ?? 1.0,\n seed: options.seed ?? Math.floor(Math.random() * 2 ** 31),\n curveFitting: options.curveFitting ?? 0.95,\n curveStepCount: options.curveStepCount ?? 9,\n maxRandomnessOffset: options.maxRandomnessOffset ?? 2,\n disableMultiStroke: options.disableMultiStroke ?? false,\n ...options\n });\n\n return extractPoints(drawable);\n}\n\n/**\n * Convert an ellipse to RoughJS style points\n *\n * @param {number} cx - Center x coordinate\n * @param {number} cy - Center y coordinate\n * @param {number} width - Ellipse width\n * @param {number} height - Ellipse height\n * @param {Object} [options={}] - RoughJS options\n * @param {number} [options.roughness=1.0] - Shape irregularity\n * @param {number} [options.bowing=1.0] - Line curvature\n * @param {number} [options.seed] - Seed for reproducible randomness\n * @param {number} [options.curveFitting=0.95] - Curve fitting (0=loose, 1=tight)\n * @param {number} [options.curveStepCount=9] - Curve resolution (higher=smoother)\n * @param {number} [options.maxRandomnessOffset=2] - Max random offset\n * @param {boolean} [options.disableMultiStroke=false] - Disable double-stroke effect\n * @returns {Array} Array of [x, y] points with hand-drawn style\n */\nexport function roughEllipse(cx, cy, width, height, options = {}) {\n const drawable = generator.ellipse(cx, cy, width, height, {\n roughness: options.roughness ?? 1.0,\n bowing: options.bowing ?? 1.0,\n seed: options.seed ?? Math.floor(Math.random() * 2 ** 31),\n curveFitting: options.curveFitting ?? 0.95,\n curveStepCount: options.curveStepCount ?? 9,\n maxRandomnessOffset: options.maxRandomnessOffset ?? 2,\n disableMultiStroke: options.disableMultiStroke ?? false,\n ...options\n });\n\n return extractPoints(drawable);\n}\n\n/**\n * Convert a linear path (multiple connected points) to RoughJS style\n *\n * @param {Array} points - Array of [x, y] points to connect\n * @param {Object} [options={}] - RoughJS options\n * @param {number} [options.roughness=1.0] - Path irregularity\n * @param {number} [options.bowing=1.0] - Line curvature\n * @param {number} [options.seed] - Seed for reproducible randomness\n * @returns {Array} Array of [x, y] points with hand-drawn style\n */\nexport function roughLinearPath(points, options = {}) {\n if (!points || points.length < 2) {\n return points || [];\n }\n\n const drawable = generator.linearPath(points, {\n roughness: options.roughness ?? 1.0,\n bowing: options.bowing ?? 1.0,\n seed: options.seed ?? Math.floor(Math.random() * 2 ** 31),\n ...options\n });\n\n return extractPoints(drawable);\n}\n\nexport default {\n roughLine,\n roughEllipse,\n roughLinearPath,\n hashSeed\n};\n"],"names":["generator","rough","bezierPoint","p0","p1","p2","p3","t","mt","opsToPoints","ops","points","current","op","data","cx1","cy1","cx2","cy2","x","y","extractPoints","drawable","allPoints","set","hashSeed","str","hash","i","roughLine","x1","y1","x2","y2","options","roughEllipse","cx","cy","width","height"],"mappings":"iHAWMA,EAAYC,EAAAA,QAAM,UAAS,EAYjC,SAASC,EAAYC,EAAIC,EAAIC,EAAIC,EAAIC,EAAG,CACtC,MAAMC,EAAK,EAAID,EACf,MAAO,CACLC,GAAM,EAAIL,EAAG,CAAC,EAAI,EAAIK,GAAM,EAAID,EAAIH,EAAG,CAAC,EAAI,EAAII,EAAKD,GAAK,EAAIF,EAAG,CAAC,EAAIE,GAAK,EAAID,EAAG,CAAC,EACnFE,GAAM,EAAIL,EAAG,CAAC,EAAI,EAAIK,GAAM,EAAID,EAAIH,EAAG,CAAC,EAAI,EAAII,EAAKD,GAAK,EAAIF,EAAG,CAAC,EAAIE,GAAK,EAAID,EAAG,CAAC,CACvF,CACA,CAWA,SAASG,EAAYC,EAAK,CACxB,MAAMC,EAAS,CAAA,EACf,IAAIC,EAAU,CAAC,EAAG,CAAC,EAEnB,SAAW,CAAE,GAAAC,EAAI,KAAAC,CAAI,IAAMJ,EACzB,OAAQG,EAAE,CACR,IAAK,OACHD,EAAU,CAACE,EAAK,CAAC,EAAGA,EAAK,CAAC,CAAC,EAC3BH,EAAO,KAAK,CAAC,GAAGC,CAAO,CAAC,EACxB,MAEF,IAAK,SACHA,EAAU,CAACE,EAAK,CAAC,EAAGA,EAAK,CAAC,CAAC,EAC3BH,EAAO,KAAK,CAAC,GAAGC,CAAO,CAAC,EACxB,MAEF,IAAK,WAAY,CACf,KAAM,CAACG,EAAKC,EAAKC,EAAKC,EAAKC,EAAGC,CAAC,EAAIN,EAC7BX,EAAKS,EACLR,EAAK,CAACW,EAAKC,CAAG,EACdX,EAAK,CAACY,EAAKC,CAAG,EACdZ,EAAK,CAACa,EAAGC,CAAC,EAEhB,QAASb,EAAI,IAAMA,GAAK,EAAGA,GAAK,IAC9BI,EAAO,KAAKT,EAAYC,EAAIC,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