fabric
Version:
Object model for HTML5 canvas, and SVG-to-canvas parser. Backed by jsdom and node-canvas.
1 lines • 1.9 kB
Source Map (JSON)
{"version":3,"file":"isTransparent.mjs","sources":["../../../../src/util/misc/isTransparent.ts"],"sourcesContent":["/**\n * Returns true if context has transparent pixel\n * at specified location (taking tolerance into account)\n * @param {CanvasRenderingContext2D} ctx context\n * @param {Number} x x coordinate in canvasElementCoordinate, not fabric space. integer\n * @param {Number} y y coordinate in canvasElementCoordinate, not fabric space. integer\n * @param {Number} tolerance Tolerance pixels around the point, not alpha tolerance, integer\n * @return {boolean} true if transparent\n */\nexport const isTransparent = (\n ctx: CanvasRenderingContext2D,\n x: number,\n y: number,\n tolerance: number,\n): boolean => {\n tolerance = Math.round(tolerance);\n const size = tolerance * 2 + 1;\n const { data } = ctx.getImageData(x - tolerance, y - tolerance, size, size);\n\n // Split image data - for tolerance > 1, pixelDataSize = 4;\n for (let i = 3; i < data.length; i += 4) {\n const alphaChannel = data[i];\n if (alphaChannel > 0) {\n return false;\n }\n }\n return true;\n};\n"],"names":["isTransparent","ctx","x","y","tolerance","Math","round","size","data","getImageData","i","length","alphaChannel"],"mappings":"AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,MAAMA,aAAa,GAAGA,CAC3BC,GAA6B,EAC7BC,CAAS,EACTC,CAAS,EACTC,SAAiB,KACL;AACZA,EAAAA,SAAS,GAAGC,IAAI,CAACC,KAAK,CAACF,SAAS,CAAC,CAAA;AACjC,EAAA,MAAMG,IAAI,GAAGH,SAAS,GAAG,CAAC,GAAG,CAAC,CAAA;EAC9B,MAAM;AAAEI,IAAAA,IAAAA;AAAK,GAAC,GAAGP,GAAG,CAACQ,YAAY,CAACP,CAAC,GAAGE,SAAS,EAAED,CAAC,GAAGC,SAAS,EAAEG,IAAI,EAAEA,IAAI,CAAC,CAAA;;AAE3E;AACA,EAAA,KAAK,IAAIG,CAAC,GAAG,CAAC,EAAEA,CAAC,GAAGF,IAAI,CAACG,MAAM,EAAED,CAAC,IAAI,CAAC,EAAE;AACvC,IAAA,MAAME,YAAY,GAAGJ,IAAI,CAACE,CAAC,CAAC,CAAA;IAC5B,IAAIE,YAAY,GAAG,CAAC,EAAE;AACpB,MAAA,OAAO,KAAK,CAAA;AACd,KAAA;AACF,GAAA;AACA,EAAA,OAAO,IAAI,CAAA;AACb;;;;"}