Skip to content

Commit 4d4812e

Browse files
committed
fix: Fix mouseDown position
1 parent 150c1d0 commit 4d4812e

File tree

2 files changed

+10
-9
lines changed

2 files changed

+10
-9
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-thumbnail-generator",
3-
"version": "3.0.2",
3+
"version": "3.0.3",
44
"description": "react-thumbnail-generator",
55
"main": "dist/index.js",
66
"module": "dist/index.js",

src/components/Canvas/index.tsx

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ const Canvas = React.forwardRef(({ canvasState }: CanvasProps, ref: any) => {
2525
fontColor,
2626
strokeColor,
2727
} = canvasState;
28+
2829
const {
2930
dragAndDropTextData,
3031
handleCanvasMouseDown,
@@ -45,15 +46,15 @@ const Canvas = React.forwardRef(({ canvasState }: CanvasProps, ref: any) => {
4546
return strokeObj[fontStrokeType];
4647
}, [fontStrokeType]);
4748

48-
const getCenterX = useCallback(
49-
(lineMaxWidth: number) => {
49+
const getCalculatedX = useCallback(
50+
(originWidth: number, lineMaxWidth: number) => {
5051
switch (textAlign) {
5152
case 'center':
52-
return +canvasWidth / 2;
53+
return originWidth;
5354
case 'end':
54-
return +canvasWidth / 2 + lineMaxWidth / 2;
55+
return originWidth + lineMaxWidth / 2;
5556
default:
56-
return +canvasWidth / 2 + (lineMaxWidth / 2) * -1;
57+
return originWidth + (lineMaxWidth / 2) * -1;
5758
}
5859
},
5960
[textAlign, canvasWidth]
@@ -68,16 +69,16 @@ const Canvas = React.forwardRef(({ canvasState }: CanvasProps, ref: any) => {
6869
) => {
6970
const { offsetX, offsetY } = dragAndDropTextData;
7071
const centerY = +canvasHeight / 2;
71-
const centerX = getCenterX(lineMaxWidth);
72+
const centerX = getCalculatedX(+canvasWidth / 2, lineMaxWidth);
7273

73-
const x = offsetX ? offsetX : centerX;
74+
const x = offsetX ? getCalculatedX(offsetX, lineMaxWidth) : centerX;
7475
const y = offsetY
7576
? offsetY - ((linesLength - 1) * lineHeight) / 2 + idx * lineHeight
7677
: centerY - ((linesLength - 1) * lineHeight) / 2 + idx * lineHeight;
7778

7879
return { x, y };
7980
},
80-
[dragAndDropTextData, canvasHeight, getCenterX]
81+
[dragAndDropTextData, canvasHeight, getCalculatedX]
8182
);
8283

8384
const setFontStroke = useCallback(

0 commit comments

Comments
 (0)