f3d0cfb3edbeca864ea81505ed726458.json
20.7 KB
{"ast":null,"code":"/**\r\n * @author NHN Ent. FE Development Team <dl_javascript@nhn.com>\r\n * @fileoverview Shape resize helper\r\n */\nconst DIVISOR = {\n rect: 1,\n circle: 2,\n triangle: 1\n};\nconst DIMENSION_KEYS = {\n rect: {\n w: 'width',\n h: 'height'\n },\n circle: {\n w: 'rx',\n h: 'ry'\n },\n triangle: {\n w: 'width',\n h: 'height'\n }\n};\n/**\r\n * Set the start point value to the shape object\r\n * @param {fabric.Object} shape - Shape object\r\n * @ignore\r\n */\n\nfunction setStartPoint(shape) {\n const {\n originX,\n originY\n } = shape;\n const originKey = originX.substring(0, 1) + originY.substring(0, 1);\n shape.startPoint = shape.origins[originKey];\n}\n/**\r\n * Get the positions of ratated origin by the pointer value\r\n * @param {{x: number, y: number}} origin - Origin value\r\n * @param {{x: number, y: number}} pointer - Pointer value\r\n * @param {number} angle - Rotating angle\r\n * @returns {Object} Postions of origin\r\n * @ignore\r\n */\n\n\nfunction getPositionsOfRotatedOrigin(origin, pointer, angle) {\n const sx = origin.x;\n const sy = origin.y;\n const px = pointer.x;\n const py = pointer.y;\n const r = angle * Math.PI / 180;\n const rx = (px - sx) * Math.cos(r) - (py - sy) * Math.sin(r) + sx;\n const ry = (px - sx) * Math.sin(r) + (py - sy) * Math.cos(r) + sy;\n return {\n originX: sx > rx ? 'right' : 'left',\n originY: sy > ry ? 'bottom' : 'top'\n };\n}\n/**\r\n * Whether the shape has the center origin or not\r\n * @param {fabric.Object} shape - Shape object\r\n * @returns {boolean} State\r\n * @ignore\r\n */\n\n\nfunction hasCenterOrigin(shape) {\n return shape.originX === 'center' && shape.originY === 'center';\n}\n/**\r\n * Adjust the origin of shape by the start point\r\n * @param {{x: number, y: number}} pointer - Pointer value\r\n * @param {fabric.Object} shape - Shape object\r\n * @ignore\r\n */\n\n\nfunction adjustOriginByStartPoint(pointer, shape) {\n const centerPoint = shape.getPointByOrigin('center', 'center');\n const angle = -shape.angle;\n const originPositions = getPositionsOfRotatedOrigin(centerPoint, pointer, angle);\n const {\n originX,\n originY\n } = originPositions;\n const origin = shape.getPointByOrigin(originX, originY);\n const left = shape.left - (centerPoint.x - origin.x);\n const top = shape.top - (centerPoint.y - origin.y);\n shape.set({\n originX,\n originY,\n left,\n top\n });\n shape.setCoords();\n}\n/**\r\n * Adjust the origin of shape by the moving pointer value\r\n * @param {{x: number, y: number}} pointer - Pointer value\r\n * @param {fabric.Object} shape - Shape object\r\n * @ignore\r\n */\n\n\nfunction adjustOriginByMovingPointer(pointer, shape) {\n const origin = shape.startPoint;\n const angle = -shape.angle;\n const originPositions = getPositionsOfRotatedOrigin(origin, pointer, angle);\n const {\n originX,\n originY\n } = originPositions;\n shape.setPositionByOrigin(origin, originX, originY);\n shape.setCoords();\n}\n/**\r\n * Adjust the dimension of shape on firing scaling event\r\n * @param {fabric.Object} shape - Shape object\r\n * @ignore\r\n */\n\n\nfunction adjustDimensionOnScaling(shape) {\n const {\n type,\n scaleX,\n scaleY\n } = shape;\n const dimensionKeys = DIMENSION_KEYS[type];\n let width = shape[dimensionKeys.w] * scaleX;\n let height = shape[dimensionKeys.h] * scaleY;\n\n if (shape.isRegular) {\n const maxScale = Math.max(scaleX, scaleY);\n width = shape[dimensionKeys.w] * maxScale;\n height = shape[dimensionKeys.h] * maxScale;\n }\n\n const options = {\n hasControls: false,\n hasBorders: false,\n scaleX: 1,\n scaleY: 1\n };\n options[dimensionKeys.w] = width;\n options[dimensionKeys.h] = height;\n shape.set(options);\n}\n/**\r\n * Adjust the dimension of shape on firing mouse move event\r\n * @param {{x: number, y: number}} pointer - Pointer value\r\n * @param {fabric.Object} shape - Shape object\r\n * @ignore\r\n */\n\n\nfunction adjustDimensionOnMouseMove(pointer, shape) {\n const {\n type,\n strokeWidth,\n startPoint: origin\n } = shape;\n const divisor = DIVISOR[type];\n const dimensionKeys = DIMENSION_KEYS[type];\n const isTriangle = !!(shape.type === 'triangle');\n const options = {};\n let width = Math.abs(origin.x - pointer.x) / divisor;\n let height = Math.abs(origin.y - pointer.y) / divisor;\n\n if (width > strokeWidth) {\n width -= strokeWidth / divisor;\n }\n\n if (height > strokeWidth) {\n height -= strokeWidth / divisor;\n }\n\n if (shape.isRegular) {\n width = height = Math.max(width, height);\n\n if (isTriangle) {\n height = Math.sqrt(3) / 2 * width;\n }\n }\n\n options[dimensionKeys.w] = width;\n options[dimensionKeys.h] = height;\n shape.set(options);\n}\n\nmodule.exports = {\n /**\r\n * Set each origin value to shape\r\n * @param {fabric.Object} shape - Shape object\r\n */\n setOrigins(shape) {\n const leftTopPoint = shape.getPointByOrigin('left', 'top');\n const rightTopPoint = shape.getPointByOrigin('right', 'top');\n const rightBottomPoint = shape.getPointByOrigin('right', 'bottom');\n const leftBottomPoint = shape.getPointByOrigin('left', 'bottom');\n shape.origins = {\n lt: leftTopPoint,\n rt: rightTopPoint,\n rb: rightBottomPoint,\n lb: leftBottomPoint\n };\n },\n\n /**\r\n * Resize the shape\r\n * @param {fabric.Object} shape - Shape object\r\n * @param {{x: number, y: number}} pointer - Mouse pointer values on canvas\r\n * @param {boolean} isScaling - Whether the resizing action is scaling or not\r\n */\n resize(shape, pointer, isScaling) {\n if (hasCenterOrigin(shape)) {\n adjustOriginByStartPoint(pointer, shape);\n setStartPoint(shape);\n }\n\n if (isScaling) {\n adjustDimensionOnScaling(shape, pointer);\n } else {\n adjustDimensionOnMouseMove(pointer, shape);\n }\n\n adjustOriginByMovingPointer(pointer, shape);\n },\n\n /**\r\n * Adjust the origin position of shape to center\r\n * @param {fabric.Object} shape - Shape object\r\n */\n adjustOriginToCenter(shape) {\n const centerPoint = shape.getPointByOrigin('center', 'center');\n const {\n originX,\n originY\n } = shape;\n const origin = shape.getPointByOrigin(originX, originY);\n const left = shape.left + (centerPoint.x - origin.x);\n const top = shape.top + (centerPoint.y - origin.y);\n shape.set({\n hasControls: true,\n hasBorders: true,\n originX: 'center',\n originY: 'center',\n left,\n top\n });\n shape.setCoords(); // For left, top properties\n }\n\n};","map":{"version":3,"sources":["C:/Users/kkwan_000/Desktop/git/2017110269/minsung/src/js/helper/shapeResizeHelper.js"],"names":["DIVISOR","rect","circle","triangle","DIMENSION_KEYS","w","h","setStartPoint","shape","originX","originY","originKey","substring","startPoint","origins","getPositionsOfRotatedOrigin","origin","pointer","angle","sx","x","sy","y","px","py","r","Math","PI","rx","cos","sin","ry","hasCenterOrigin","adjustOriginByStartPoint","centerPoint","getPointByOrigin","originPositions","left","top","set","setCoords","adjustOriginByMovingPointer","setPositionByOrigin","adjustDimensionOnScaling","type","scaleX","scaleY","dimensionKeys","width","height","isRegular","maxScale","max","options","hasControls","hasBorders","adjustDimensionOnMouseMove","strokeWidth","divisor","isTriangle","abs","sqrt","module","exports","setOrigins","leftTopPoint","rightTopPoint","rightBottomPoint","leftBottomPoint","lt","rt","rb","lb","resize","isScaling","adjustOriginToCenter"],"mappings":"AAAA;AACA;AACA;AACA;AACA,MAAMA,OAAO,GAAG;AACdC,EAAAA,IAAI,EAAE,CADQ;AAEdC,EAAAA,MAAM,EAAE,CAFM;AAGdC,EAAAA,QAAQ,EAAE;AAHI,CAAhB;AAKA,MAAMC,cAAc,GAAG;AACrBH,EAAAA,IAAI,EAAE;AACJI,IAAAA,CAAC,EAAE,OADC;AAEJC,IAAAA,CAAC,EAAE;AAFC,GADe;AAKrBJ,EAAAA,MAAM,EAAE;AACNG,IAAAA,CAAC,EAAE,IADG;AAENC,IAAAA,CAAC,EAAE;AAFG,GALa;AASrBH,EAAAA,QAAQ,EAAE;AACRE,IAAAA,CAAC,EAAE,OADK;AAERC,IAAAA,CAAC,EAAE;AAFK;AATW,CAAvB;AAeA;AACA;AACA;AACA;AACA;;AACA,SAASC,aAAT,CAAuBC,KAAvB,EAA8B;AAC5B,QAAM;AAAEC,IAAAA,OAAF;AAAWC,IAAAA;AAAX,MAAuBF,KAA7B;AACA,QAAMG,SAAS,GAAGF,OAAO,CAACG,SAAR,CAAkB,CAAlB,EAAqB,CAArB,IAA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AAAA;AAAX,QAAuBF,KAA7B;AACA,UAAMQ,MAAM,GAAGR,KAAK,CAAC2B,gBAAN,CAAuB1B,OAAvB,EAAgCC,OAAhC,CAAf;AACA,UAAM2B,IAAI,GAAG7B,KAAK,CAAC6B,IAAN,IAAcH,WAAW,CAACd,CAAZ,GAAgBJ,MAAM,CAACI,CAArC,CAAb;AACA,UAAMkB,GAAG,GAAG9B,KAAK,CAAC8B,GAAN,IAAaJ,WAAW,CAACZ,CAAZ,GAAgBN,MAAM,CAACM,CAApC,CAAZ;AAEAd,IAAAA,KAAK,CAAC+B,GAAN,CAAU;AACRe,MAAAA,WAAW,EAAE,IADL;AAERC,MAAAA,UAAU,EAAE,IAFJ;AAGR9C,MAAAA,OAAO,EAAE,QAHD;AAIRC,MAAAA,OAAO,EAAE,QAJD;AAKR2B,MAAAA,IALQ;AAMRC,MAAAA;AANQ,KAAV;AASA9B,IAAAA,KAAK,CAACgC,SAAN,GAhB0B,CAgBP;AACpB;;AA7Dc,CAAjB","sourcesContent":["/**\r\n * @author NHN Ent. FE Development Team <dl_javascript@nhn.com>\r\n * @fileoverview Shape resize helper\r\n */\r\nconst DIVISOR = {\r\n rect: 1,\r\n circle: 2,\r\n triangle: 1,\r\n};\r\nconst DIMENSION_KEYS = {\r\n rect: {\r\n w: 'width',\r\n h: 'height',\r\n },\r\n circle: {\r\n w: 'rx',\r\n h: 'ry',\r\n },\r\n triangle: {\r\n w: 'width',\r\n h: 'height',\r\n },\r\n};\r\n\r\n/**\r\n * Set the start point value to the shape object\r\n * @param {fabric.Object} shape - Shape object\r\n * @ignore\r\n */\r\nfunction setStartPoint(shape) {\r\n const { originX, originY } = shape;\r\n const originKey = originX.substring(0, 1) + originY.substring(0, 1);\r\n\r\n shape.startPoint = shape.origins[originKey];\r\n}\r\n\r\n/**\r\n * Get the positions of ratated origin by the pointer value\r\n * @param {{x: number, y: number}} origin - Origin value\r\n * @param {{x: number, y: number}} pointer - Pointer value\r\n * @param {number} angle - Rotating angle\r\n * @returns {Object} Postions of origin\r\n * @ignore\r\n */\r\nfunction getPositionsOfRotatedOrigin(origin, pointer, angle) {\r\n const sx = origin.x;\r\n const sy = origin.y;\r\n const px = pointer.x;\r\n const py = pointer.y;\r\n const r = (angle * Math.PI) / 180;\r\n const rx = (px - sx) * Math.cos(r) - (py - sy) * Math.sin(r) + sx;\r\n const ry = (px - sx) * Math.sin(r) + (py - sy) * Math.cos(r) + sy;\r\n\r\n return {\r\n originX: sx > rx ? 'right' : 'left',\r\n originY: sy > ry ? 'bottom' : 'top',\r\n };\r\n}\r\n\r\n/**\r\n * Whether the shape has the center origin or not\r\n * @param {fabric.Object} shape - Shape object\r\n * @returns {boolean} State\r\n * @ignore\r\n */\r\nfunction hasCenterOrigin(shape) {\r\n return shape.originX === 'center' && shape.originY === 'center';\r\n}\r\n\r\n/**\r\n * Adjust the origin of shape by the start point\r\n * @param {{x: number, y: number}} pointer - Pointer value\r\n * @param {fabric.Object} shape - Shape object\r\n * @ignore\r\n */\r\nfunction adjustOriginByStartPoint(pointer, shape) {\r\n const centerPoint = shape.getPointByOrigin('center', 'center');\r\n const angle = -shape.angle;\r\n const originPositions = getPositionsOfRotatedOrigin(centerPoint, pointer, angle);\r\n const { originX, originY } = originPositions;\r\n const origin = shape.getPointByOrigin(originX, originY);\r\n const left = shape.left - (centerPoint.x - origin.x);\r\n const top = shape.top - (centerPoint.y - origin.y);\r\n\r\n shape.set({\r\n originX,\r\n originY,\r\n left,\r\n top,\r\n });\r\n\r\n shape.setCoords();\r\n}\r\n\r\n/**\r\n * Adjust the origin of shape by the moving pointer value\r\n * @param {{x: number, y: number}} pointer - Pointer value\r\n * @param {fabric.Object} shape - Shape object\r\n * @ignore\r\n */\r\nfunction adjustOriginByMovingPointer(pointer, shape) {\r\n const origin = shape.startPoint;\r\n const angle = -shape.angle;\r\n const originPositions = getPositionsOfRotatedOrigin(origin, pointer, angle);\r\n const { originX, originY } = originPositions;\r\n\r\n shape.setPositionByOrigin(origin, originX, originY);\r\n shape.setCoords();\r\n}\r\n\r\n/**\r\n * Adjust the dimension of shape on firing scaling event\r\n * @param {fabric.Object} shape - Shape object\r\n * @ignore\r\n */\r\nfunction adjustDimensionOnScaling(shape) {\r\n const { type, scaleX, scaleY } = shape;\r\n const dimensionKeys = DIMENSION_KEYS[type];\r\n let width = shape[dimensionKeys.w] * scaleX;\r\n let height = shape[dimensionKeys.h] * scaleY;\r\n\r\n if (shape.isRegular) {\r\n const maxScale = Math.max(scaleX, scaleY);\r\n\r\n width = shape[dimensionKeys.w] * maxScale;\r\n height = shape[dimensionKeys.h] * maxScale;\r\n }\r\n\r\n const options = {\r\n hasControls: false,\r\n hasBorders: false,\r\n scaleX: 1,\r\n scaleY: 1,\r\n };\r\n\r\n options[dimensionKeys.w] = width;\r\n options[dimensionKeys.h] = height;\r\n\r\n shape.set(options);\r\n}\r\n\r\n/**\r\n * Adjust the dimension of shape on firing mouse move event\r\n * @param {{x: number, y: number}} pointer - Pointer value\r\n * @param {fabric.Object} shape - Shape object\r\n * @ignore\r\n */\r\nfunction adjustDimensionOnMouseMove(pointer, shape) {\r\n const { type, strokeWidth, startPoint: origin } = shape;\r\n const divisor = DIVISOR[type];\r\n const dimensionKeys = DIMENSION_KEYS[type];\r\n const isTriangle = !!(shape.type === 'triangle');\r\n const options = {};\r\n let width = Math.abs(origin.x - pointer.x) / divisor;\r\n let height = Math.abs(origin.y - pointer.y) / divisor;\r\n\r\n if (width > strokeWidth) {\r\n width -= strokeWidth / divisor;\r\n }\r\n\r\n if (height > strokeWidth) {\r\n height -= strokeWidth / divisor;\r\n }\r\n\r\n if (shape.isRegular) {\r\n width = height = Math.max(width, height);\r\n\r\n if (isTriangle) {\r\n height = (Math.sqrt(3) / 2) * width;\r\n }\r\n }\r\n\r\n options[dimensionKeys.w] = width;\r\n options[dimensionKeys.h] = height;\r\n\r\n shape.set(options);\r\n}\r\n\r\nmodule.exports = {\r\n /**\r\n * Set each origin value to shape\r\n * @param {fabric.Object} shape - Shape object\r\n */\r\n setOrigins(shape) {\r\n const leftTopPoint = shape.getPointByOrigin('left', 'top');\r\n const rightTopPoint = shape.getPointByOrigin('right', 'top');\r\n const rightBottomPoint = shape.getPointByOrigin('right', 'bottom');\r\n const leftBottomPoint = shape.getPointByOrigin('left', 'bottom');\r\n\r\n shape.origins = {\r\n lt: leftTopPoint,\r\n rt: rightTopPoint,\r\n rb: rightBottomPoint,\r\n lb: leftBottomPoint,\r\n };\r\n },\r\n\r\n /**\r\n * Resize the shape\r\n * @param {fabric.Object} shape - Shape object\r\n * @param {{x: number, y: number}} pointer - Mouse pointer values on canvas\r\n * @param {boolean} isScaling - Whether the resizing action is scaling or not\r\n */\r\n resize(shape, pointer, isScaling) {\r\n if (hasCenterOrigin(shape)) {\r\n adjustOriginByStartPoint(pointer, shape);\r\n setStartPoint(shape);\r\n }\r\n\r\n if (isScaling) {\r\n adjustDimensionOnScaling(shape, pointer);\r\n } else {\r\n adjustDimensionOnMouseMove(pointer, shape);\r\n }\r\n\r\n adjustOriginByMovingPointer(pointer, shape);\r\n },\r\n\r\n /**\r\n * Adjust the origin position of shape to center\r\n * @param {fabric.Object} shape - Shape object\r\n */\r\n adjustOriginToCenter(shape) {\r\n const centerPoint = shape.getPointByOrigin('center', 'center');\r\n const { originX, originY } = shape;\r\n const origin = shape.getPointByOrigin(originX, originY);\r\n const left = shape.left + (centerPoint.x - origin.x);\r\n const top = shape.top + (centerPoint.y - origin.y);\r\n\r\n shape.set({\r\n hasControls: true,\r\n hasBorders: true,\r\n originX: 'center',\r\n originY: 'center',\r\n left,\r\n top,\r\n });\r\n\r\n shape.setCoords(); // For left, top properties\r\n },\r\n};\r\n"]},"metadata":{},"sourceType":"module"}