{"version":3,"file":"SprayBrush.min.mjs","sources":["../../../src/brushes/SprayBrush.ts"],"sourcesContent":["import type { Point } from '../Point';\nimport { Group } from '../shapes/Group';\nimport { Shadow } from '../Shadow';\nimport { Rect } from '../shapes/Rect';\nimport { getRandomInt } from '../util/internals/getRandomInt';\nimport type { Canvas } from '../canvas/Canvas';\nimport { BaseBrush } from './BaseBrush';\nimport type { SprayBrushPoint } from './typedefs';\nimport { CENTER } from '../constants';\n\n/**\n *\n * @param rects\n * @returns\n */\nfunction getUniqueRects(rects: Rect[]) {\n  const uniqueRects: Record<string, boolean> = {};\n  const uniqueRectsArray: Rect[] = [];\n\n  for (let i = 0, key: string; i < rects.length; i++) {\n    key = `${rects[i].left}${rects[i].top}`;\n    if (!uniqueRects[key]) {\n      uniqueRects[key] = true;\n      uniqueRectsArray.push(rects[i]);\n    }\n  }\n\n  return uniqueRectsArray;\n}\n\nexport class SprayBrush extends BaseBrush {\n  /**\n   * Width of a spray\n   * @type Number\n   * @default\n   */\n  width = 10;\n\n  /**\n   * Density of a spray (number of dots per chunk)\n   * @type Number\n   * @default\n   */\n  density = 20;\n\n  /**\n   * Width of spray dots\n   * @type Number\n   * @default\n   */\n  dotWidth = 1;\n\n  /**\n   * Width variance of spray dots\n   * @type Number\n   * @default\n   */\n  dotWidthVariance = 1;\n\n  /**\n   * Whether opacity of a dot should be random\n   * @type Boolean\n   * @default\n   */\n  randomOpacity = false;\n\n  /**\n   * Whether overlapping dots (rectangles) should be removed (for performance reasons)\n   * @type Boolean\n   * @default\n   */\n  optimizeOverlapping = true;\n\n  private declare sprayChunks: SprayBrushPoint[][];\n\n  private declare sprayChunk: SprayBrushPoint[];\n\n  /**\n   * Constructor\n   * @param {Canvas} canvas\n   * @return {SprayBrush} Instance of a spray brush\n   */\n  constructor(canvas: Canvas) {\n    super(canvas);\n    this.sprayChunks = [];\n    this.sprayChunk = [];\n  }\n\n  /**\n   * Invoked on mouse down\n   * @param {Point} pointer\n   */\n  onMouseDown(pointer: Point) {\n    this.sprayChunks = [];\n    this.canvas.clearContext(this.canvas.contextTop);\n    this._setShadow();\n\n    this.addSprayChunk(pointer);\n    this.renderChunck(this.sprayChunk);\n  }\n\n  /**\n   * Invoked on mouse move\n   * @param {Point} pointer\n   */\n  onMouseMove(pointer: Point) {\n    if (this.limitedToCanvasSize === true && this._isOutSideCanvas(pointer)) {\n      return;\n    }\n    this.addSprayChunk(pointer);\n    this.renderChunck(this.sprayChunk);\n  }\n\n  /**\n   * Invoked on mouse up\n   */\n  onMouseUp() {\n    const originalRenderOnAddRemove = this.canvas.renderOnAddRemove;\n    this.canvas.renderOnAddRemove = false;\n\n    const rects: Rect[] = [];\n\n    for (let i = 0; i < this.sprayChunks.length; i++) {\n      const sprayChunk = this.sprayChunks[i];\n      for (let j = 0; j < sprayChunk.length; j++) {\n        const chunck = sprayChunk[j];\n        const rect = new Rect({\n          width: chunck.width,\n          height: chunck.width,\n          left: chunck.x + 1,\n          top: chunck.y + 1,\n          originX: CENTER,\n          originY: CENTER,\n          fill: this.color,\n        });\n        rects.push(rect);\n      }\n    }\n\n    const group = new Group(\n      this.optimizeOverlapping ? getUniqueRects(rects) : rects,\n      {\n        objectCaching: true,\n        subTargetCheck: false,\n        interactive: false,\n      },\n    );\n    this.shadow && group.set('shadow', new Shadow(this.shadow));\n    this.canvas.fire('before:path:created', { path: group });\n    this.canvas.add(group);\n    this.canvas.fire('path:created', { path: group });\n\n    this.canvas.clearContext(this.canvas.contextTop);\n    this._resetShadow();\n    this.canvas.renderOnAddRemove = originalRenderOnAddRemove;\n    this.canvas.requestRenderAll();\n  }\n\n  renderChunck(sprayChunck: SprayBrushPoint[]) {\n    const ctx = this.canvas.contextTop;\n    ctx.fillStyle = this.color;\n\n    this._saveAndTransform(ctx);\n\n    for (let i = 0; i < sprayChunck.length; i++) {\n      const point = sprayChunck[i];\n      ctx.globalAlpha = point.opacity;\n      ctx.fillRect(point.x, point.y, point.width, point.width);\n    }\n\n    ctx.restore();\n  }\n\n  /**\n   * Render all spray chunks\n   */\n  _render() {\n    const ctx = this.canvas.contextTop;\n    ctx.fillStyle = this.color;\n\n    this._saveAndTransform(ctx);\n\n    for (let i = 0; i < this.sprayChunks.length; i++) {\n      this.renderChunck(this.sprayChunks[i]);\n    }\n    ctx.restore();\n  }\n\n  /**\n   * @param {Point} pointer\n   */\n  addSprayChunk(pointer: Point) {\n    this.sprayChunk = [];\n    const radius = this.width / 2;\n\n    for (let i = 0; i < this.density; i++) {\n      this.sprayChunk.push({\n        x: getRandomInt(pointer.x - radius, pointer.x + radius),\n        y: getRandomInt(pointer.y - radius, pointer.y + radius),\n        width: this.dotWidthVariance\n          ? getRandomInt(\n              // bottom clamp width to 1\n              Math.max(1, this.dotWidth - this.dotWidthVariance),\n              this.dotWidth + this.dotWidthVariance,\n            )\n          : this.dotWidth,\n        opacity: this.randomOpacity ? getRandomInt(0, 100) / 100 : 1,\n      });\n    }\n\n    this.sprayChunks.push(this.sprayChunk);\n  }\n}\n"],"names":["SprayBrush","BaseBrush","constructor","canvas","super","_defineProperty","this","sprayChunks","sprayChunk","onMouseDown","pointer","clearContext","contextTop","_setShadow","addSprayChunk","renderChunck","onMouseMove","limitedToCanvasSize","_isOutSideCanvas","onMouseUp","originalRenderOnAddRemove","renderOnAddRemove","rects","i","length","j","chunck","rect","Rect","width","height","left","x","top","y","originX","CENTER","originY","fill","color","push","group","Group","optimizeOverlapping","uniqueRects","uniqueRectsArray","key","concat","getUniqueRects","objectCaching","subTargetCheck","interactive","shadow","set","Shadow","fire","path","add","_resetShadow","requestRenderAll","sprayChunck","ctx","fillStyle","_saveAndTransform","point","globalAlpha","opacity","fillRect","restore","_render","radius","density","getRandomInt","dotWidthVariance","Math","max","dotWidth","randomOpacity"],"mappings":"+XA8BO,MAAMA,UAAmBC,EAoD9BC,WAAAA,CAAYC,GACVC,MAAMD,GApDRE,eAKQ,IAERA,iBAKU,IAEVA,kBAKW,GAEXA,0BAKmB,GAEnBA,wBAKgB,GAEhBA,8BA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