UNPKG

fabric

Version:

Object model for HTML5 canvas, and SVG-to-canvas parser. Backed by jsdom and node-canvas.

1 lines 2.31 kB
{"version":3,"file":"mergeClipPaths.mjs","names":[],"sources":["../../../../src/util/misc/mergeClipPaths.ts"],"sourcesContent":["import type { FabricObject } from '../../shapes/Object/FabricObject';\nimport { sendObjectToPlane } from './planeChange';\nimport { Group } from '../../shapes/Group';\n/**\n * Merges 2 clip paths into one visually equal clip path\n *\n * **IMPORTANT**:\\\n * Does **NOT** clone the arguments, clone them proir if necessary.\n *\n * Creates a wrapper (group) that contains one clip path and is clipped by the other so content is kept where both overlap.\n * Use this method if both the clip paths may have nested clip paths of their own, so assigning one to the other's clip path property is not possible.\n *\n * In order to handle the `inverted` property we follow logic described in the following cases:\\\n * **(1)** both clip paths are inverted - the clip paths pass the inverted prop to the wrapper and loose it themselves.\\\n * **(2)** one is inverted and the other isn't - the wrapper shouldn't become inverted and the inverted clip path must clip the non inverted one to produce an identical visual effect.\\\n * **(3)** both clip paths are not inverted - wrapper and clip paths remain unchanged.\n *\n * @param {fabric.Object} c1\n * @param {fabric.Object} c2\n * @returns {fabric.Object} merged clip path\n */\nexport const mergeClipPaths = (c1: FabricObject, c2: FabricObject) => {\n let a = c1,\n b = c2;\n if (a.inverted && !b.inverted) {\n // case (2)\n a = c2;\n b = c1;\n }\n // `b` becomes `a`'s clip path so we transform `b` to `a` coordinate plane\n sendObjectToPlane(b, b.group?.calcTransformMatrix(), a.calcTransformMatrix());\n // assign the `inverted` prop to the wrapping group\n const inverted = a.inverted && b.inverted;\n if (inverted) {\n // case (1)\n a.inverted = b.inverted = false;\n }\n return new Group([a], { clipPath: b, inverted });\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAqBA,MAAa,kBAAkB,IAAkB,OAAqB;;CACpE,IAAI,IAAI,IACN,IAAI;AACN,KAAI,EAAE,YAAY,CAAC,EAAE,UAAU;AAE7B,MAAI;AACJ,MAAI;;AAGN,mBAAkB,IAAA,WAAG,EAAE,WAAA,QAAA,aAAA,KAAA,IAAA,KAAA,IAAA,SAAO,qBAAqB,EAAE,EAAE,qBAAqB,CAAC;CAE7E,MAAM,WAAW,EAAE,YAAY,EAAE;AACjC,KAAI,SAEF,GAAE,WAAW,EAAE,WAAW;AAE5B,QAAO,IAAI,MAAM,CAAC,EAAE,EAAE;EAAE,UAAU;EAAG;EAAU,CAAC"}