11import useDragAndDropText from '../../hooks/useDragAndDropText' ;
2- import React , { useEffect } from 'react' ;
2+ import React , { useCallback , useEffect } from 'react' ;
33import { CanvasStateWithColors } from '../../types/canvas' ;
44import * as S from './styled' ;
55
@@ -19,6 +19,7 @@ const Canvas = React.forwardRef(({ canvasState }: CanvasProps, ref: any) => {
1919 fontFamily,
2020 angle,
2121 isBlur,
22+ textAlign,
2223 isBlockEvent,
2324 bgColor,
2425 fontColor,
@@ -44,30 +45,51 @@ const Canvas = React.forwardRef(({ canvasState }: CanvasProps, ref: any) => {
4445 return strokeObj [ fontStrokeType ] ;
4546 } ;
4647
47- const getMultiLinePosition = (
48- linesLength : number ,
49- lineHeight : number ,
50- idx : number
51- ) => {
52- const { offsetX , offsetY } = dragAndDropTextData ;
53- const centerX = + canvasWidth / 2 ;
54- const centerY = + canvasHeight / 2 ;
55-
56- const x = offsetX ? offsetX : centerX ;
57- const y = offsetY
58- ? offsetY - ( ( linesLength - 1 ) * lineHeight ) / 2 + idx * lineHeight
59- : centerY - ( ( linesLength - 1 ) * lineHeight ) / 2 + idx * lineHeight ;
48+ const getCenterX = useCallback (
49+ ( lineMaxWidth : number ) => {
50+ switch ( textAlign ) {
51+ case 'center' :
52+ return + canvasWidth / 2 ;
53+ case 'end' :
54+ return + canvasWidth / 2 + lineMaxWidth / 2 ;
55+ default :
56+ return + canvasWidth / 2 + ( lineMaxWidth / 2 ) * - 1 ;
57+ }
58+ } ,
59+ [ textAlign , canvasWidth ]
60+ ) ;
6061
61- return { x, y } ;
62- } ;
62+ const getMultiLinePosition = useCallback (
63+ (
64+ linesLength : number ,
65+ lineHeight : number ,
66+ lineMaxWidth : number ,
67+ idx : number
68+ ) => {
69+ const { offsetX, offsetY } = dragAndDropTextData ;
70+ const centerY = + canvasHeight / 2 ;
71+ const centerX = getCenterX ( lineMaxWidth ) ;
72+
73+ const x = offsetX ? offsetX : centerX ;
74+ const y = offsetY
75+ ? offsetY - ( ( linesLength - 1 ) * lineHeight ) / 2 + idx * lineHeight
76+ : centerY - ( ( linesLength - 1 ) * lineHeight ) / 2 + idx * lineHeight ;
77+
78+ return { x, y } ;
79+ } ,
80+ [ dragAndDropTextData , canvasHeight ]
81+ ) ;
6382
64- const setFontStroke = ( ctx : CanvasRenderingContext2D , line : string ) => {
65- if ( fontStrokeType === 'None' ) return ;
83+ const setFontStroke = useCallback (
84+ ( ctx : CanvasRenderingContext2D , line : string ) => {
85+ if ( fontStrokeType === 'None' ) return ;
6686
67- ctx . lineWidth = getLineWidthByStrokeType ( ) ;
68- ctx . strokeStyle = `${ strokeColor . hex } ` ;
69- ctx . strokeText ( line , 0 , 0 ) ;
70- } ;
87+ ctx . lineWidth = getLineWidthByStrokeType ( ) ;
88+ ctx . strokeStyle = `${ strokeColor . hex } ` ;
89+ ctx . strokeText ( line , 0 , 0 ) ;
90+ } ,
91+ [ fontStrokeType , strokeColor ]
92+ ) ;
7193
7294 const rotateCanvas = ( ctx : CanvasRenderingContext2D ) => {
7395 const { offsetX, offsetY } = dragAndDropTextData ;
@@ -87,9 +109,19 @@ const Canvas = React.forwardRef(({ canvasState }: CanvasProps, ref: any) => {
87109 ) => {
88110 const size = + fontSize . replace ( 'px' , '' ) ;
89111 const fontLineHeight = size + + lineHeight ;
112+ let lineMaxWidth = 0 ;
113+
114+ lines . forEach ( ( line ) => {
115+ lineMaxWidth = Math . max ( lineMaxWidth , ctx . measureText ( line ) . width ) ;
116+ } ) ;
90117
91118 lines . forEach ( ( line , idx ) => {
92- const { x, y } = getMultiLinePosition ( lines . length , fontLineHeight , idx ) ;
119+ const { x, y } = getMultiLinePosition (
120+ lines . length ,
121+ fontLineHeight ,
122+ lineMaxWidth ,
123+ idx
124+ ) ;
93125
94126 ctx . save ( ) ;
95127 ctx . translate ( x , y ) ;
@@ -107,7 +139,7 @@ const Canvas = React.forwardRef(({ canvasState }: CanvasProps, ref: any) => {
107139 const size = + fontSize . replace ( 'px' , '' ) ;
108140
109141 ctx . font = `${ size } px ${ fontFamily } ` ;
110- ctx . textAlign = 'center' ;
142+ ctx . textAlign = textAlign ;
111143 ctx . textBaseline = 'middle' ;
112144
113145 ctx . save ( ) ;
0 commit comments