{"version":3,"file":"smoothCornersScript.mjs","sources":["../../../../../src/components/FeatureProvider/SmoothCornersFeature/smoothCornersScript.ts"],"sourcesContent":["export const smoothCornersScript = `\nclass SmoothCorners {\n  static get inputProperties() {\n    return [\n      'border-image-source',\n      '--smooth-corners',\n      '--smooth-corners-shadow',\n      '--smooth-corners-bg-color',\n      '--smooth-corners-padding',\n      '--smooth-corners-radius-unit',\n    ]\n  }\n\n  constructor() {\n    this.superellipseCache = new Map()\n  }\n\n  trimPX(pixel) {\n    return parseInt(pixel.replace('px', ''), 10)\n  }\n\n  splitValueAndUnit(value) {\n    const regex = /([\\\\d.\\\\-+]+)\\\\s*(\\\\D*)/\n    const matches = value.match(regex)\n    return matches ? [matches[1], matches[2]] : [value, '']\n  }\n\n  superellipse(...args) {\n    const sanitizedArgs = this.sanitizeSuperellipseArgs(...args)\n\n    const cacheKey = this.getSuperellipseCacheKey(sanitizedArgs)\n\n    if (this.superellipseCache.has(cacheKey)) {\n      return [...this.superellipseCache.get(cacheKey)]\n    }\n\n    const result = this.computeSuperellipse(...sanitizedArgs)\n\n    this.superellipseCache.set(cacheKey, result)\n\n    return [...result]\n  }\n\n  sanitizeSuperellipseArgs(a, b, nX, nY) {\n    if (nX > 100) { nX = 100 }\n    if (nY > 100) { nY = 100 }\n    if (nX < 0.00000000001) { nX = 0.00000000001 }\n    if (nY < 0.00000000001) { nY = 0.00000000001 }\n\n    return [a, b, nX, nY]\n  }\n\n  computeSuperellipse(a, b, nX, nY) {\n    const nX2 = 2 / nX\n    const nY2 = 2 / nY\n    const steps = 360\n    const step = (2 * Math.PI) / steps\n    const points = t => {\n      const cosT = Math.cos(t)\n      const sinT = Math.sin(t)\n      return {\n        x: Math.abs(cosT) ** nX2 * a * Math.sign(cosT),\n        y: Math.abs(sinT) ** nY2 * b * Math.sign(sinT),\n      }\n    }\n\n    return Array.from({ length: steps + 1 }, (_, i) => points(i * step))\n  }\n\n  getSuperellipseCacheKey(args) {\n    return args.join(':')\n  }\n\n  paint(ctx, geom, properties) {\n    const backgroundImage = properties\n      .get('border-image-source')\n\n    const backgroundColor = properties\n      .get('--smooth-corners-bg-color')\n      .toString()\n\n    const padding = properties\n      .get('--smooth-corners-padding')\n      .toString()\n\n    const [offsetX, offsetY, blur, spread, color] = properties\n      .get('--smooth-corners-shadow')\n      .toString()\n      .split(/,s*/)\n\n    const halfWidth = geom.width / 2\n    const halfHeight = geom.height / 2\n\n    const baseN = properties\n      .get('--smooth-corners')\n      .toString()\n\n    const targetWidth = geom.width - (2 * this.trimPX(padding))\n    const targetHeight = geom.height - (2 * this.trimPX(padding))\n\n    const [rValue, rUnit] = this.splitValueAndUnit(baseN)\n    const targetR = rUnit === '%'\n      ? (Number(rValue) / 100) * Math.min(targetWidth, targetHeight)\n      : Number(rValue)\n\n    const targetNX = targetWidth / targetR\n    const targetNY = targetHeight / targetR\n\n    const smooth = this.superellipse(\n      halfWidth - this.trimPX(padding),\n      halfHeight - this.trimPX(padding),\n      parseFloat(targetNX, 10),\n      parseFloat(targetNY, 10),\n    )\n\n    ctx.setTransform(1, 0, 0, 1, halfWidth, halfHeight)\n    ctx.beginPath()\n\n    ctx.shadowColor = null\n    ctx.shadowOffsetX = 0\n    ctx.shadowOffsetY = 0\n    ctx.shadowBlur = 0\n\n    const trimedX = this.trimPX(offsetX) * 2\n    const trimedY = this.trimPX(offsetY) * 2\n    const trimedBlur = this.trimPX(blur)\n    const trimedSpread = this.trimPX(spread)\n\n    if (trimedBlur === 0) {\n      ctx.strokeStyle = color\n      ctx.lineWidth = trimedSpread * 2\n    } else {\n      ctx.shadowColor = color\n      ctx.shadowOffsetX = trimedX\n      ctx.shadowOffsetY = trimedY\n      ctx.shadowBlur = trimedBlur * 2\n    }\n\n    smooth.forEach(({ x, y }, index) => {\n      if (index === 0) {\n        ctx.moveTo(x, y)\n      } else {\n        ctx.lineTo(x, y)\n      }\n    })\n\n    if (trimedBlur === 0) {\n      ctx.stroke()\n    }\n\n    if (backgroundColor) {\n      ctx.fillStyle = backgroundColor\n      ctx.fill()\n    }\n\n    if (backgroundImage instanceof CSSImageValue) {\n      smooth.forEach(({ x, y }, index) => {\n        if (index === 0) {\n          ctx.moveTo(x, y)\n        } else {\n          ctx.lineTo(x, y)\n        }\n      })\n\n      ctx.closePath()\n      ctx.clip()\n\n      ctx.drawImage(backgroundImage, -(targetWidth / 2), -(targetHeight / 2), targetWidth, targetHeight)\n    }\n\n    ctx.closePath()\n  }\n}\n\ntry {\n  registerPaint('smooth-corners', SmoothCorners)\n} catch (e) {\n  // If the paint already exists, don't make it error.\n}\n`\n"],"names":["smoothCornersScript"],"mappings":"AAAO,MAAMA,mBAAmB,GAAG;AACnC;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;;AAEA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;;AAEA;AACA;;AAEA;;AAEA;AACA;AACA;;AAEA;;AAEA;;AAEA;AACA;;AAEA;AACA;AACA;AACA;AACA;;AAEA;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAEA;AACA;;AAEA;AACA;AACA;;AAEA;AACA;AACA;;AAEA;AACA;AACA;;AAEA;AACA;AACA;;AAEA;AACA;AACA;AACA;;AAEA;AACA;;AAEA;AACA;AACA;;AAEA;AACA;;AAEA;AACA;AACA;AACA;;AAEA;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA;;AAEA;AACA;;AAEA;AACA;AACA;AACA;;AAEA;AACA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;;AAEA;AACA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAEA;AACA;;AAEA;AACA;;AAEA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA;;;;"}