|
1 | | -import {Point} from '@flatten-js/core'; |
2 | | -import {DEFAULT_TEXT_OPTIONS, type DrawController} from './DrawController'; |
3 | | -import {SVG_MARGIN} from '../App.consts.ts'; |
| 1 | +import {Point, Vector} from '@flatten-js/core'; |
4 | 2 | import {toast} from 'react-toastify'; |
| 3 | +import {SVG_MARGIN, TO_DEGREES} from '../App.consts.ts'; |
5 | 4 | import type {TextOptions} from '../entities/TextEntity.ts'; |
6 | | -import {triggerReactUpdate} from '../state.ts'; |
7 | | -import {StateVariable} from '../helpers/undo-stack.ts'; |
8 | 5 | import {isLengthEqual} from '../helpers/is-length-equal.ts'; |
| 6 | +import {StateVariable} from '../helpers/undo-stack.ts'; |
| 7 | +import {triggerReactUpdate} from '../state.ts'; |
| 8 | +import {DEFAULT_TEXT_OPTIONS, type DrawController} from './DrawController'; |
9 | 9 |
|
10 | 10 | export class SvgDrawController implements DrawController { |
11 | 11 | private lineColor = '#000'; |
@@ -211,17 +211,32 @@ export class SvgDrawController implements DrawController { |
211 | 211 | ...options, |
212 | 212 | }; |
213 | 213 |
|
214 | | - let finalTextColor = textOptions.textColor; |
215 | | - const lowerCaseTextColor = textOptions.textColor.toLowerCase(); |
216 | | - if (lowerCaseTextColor === '#fff' || lowerCaseTextColor === '#ffffff' || lowerCaseTextColor === 'white') { |
217 | | - finalTextColor = '#000'; // Change to black if current color is white |
218 | | - } |
219 | | - // No need to handle black to white, as SVG background is white. |
220 | | - // Other colors will remain as they are. |
| 214 | + let finalTextColor = textOptions.textColor; |
| 215 | + const lowerCaseTextColor = textOptions.textColor.toLowerCase(); |
| 216 | + if ( |
| 217 | + lowerCaseTextColor === '#fff' || |
| 218 | + lowerCaseTextColor === '#ffffff' || |
| 219 | + lowerCaseTextColor === 'white' |
| 220 | + ) { |
| 221 | + finalTextColor = '#000'; // Change to black if current color is white |
| 222 | + } |
| 223 | + // No need to handle black to white, as SVG background is white. |
| 224 | + // Other colors will remain as they are. |
| 225 | + |
| 226 | + let transformAttribute = ''; |
| 227 | + if (textOptions.textDirection) { |
| 228 | + const angle = textOptions.textDirection.angleTo(new Vector(1, 0)) * TO_DEGREES; |
| 229 | + transformAttribute = `transform="rotate(${angle}, ${canvasBasePoint.x}, ${canvasBasePoint.y})"`; |
| 230 | + } |
| 231 | + |
| 232 | + let textAnchorAttribute = ''; |
| 233 | + if (textOptions.textAlign === 'center') { |
| 234 | + textAnchorAttribute = 'text-anchor="middle"'; |
| 235 | + } |
221 | 236 |
|
222 | 237 | this.svgStrings.push( |
223 | | - // Use finalTextColor here |
224 | | - `<text x="${canvasBasePoint.x}" y="${canvasBasePoint.y}" fill="${finalTextColor}" font-size="${textOptions.fontSize}" font-family="${textOptions.fontFamily}">${label}</text>` |
| 238 | + // Use finalTextColor here |
| 239 | + `<text x="${canvasBasePoint.x}" y="${canvasBasePoint.y}" fill="${finalTextColor}" font-size="${textOptions.fontSize}" font-family="${textOptions.fontFamily}" ${transformAttribute} ${textAnchorAttribute}>${label}</text>` |
225 | 240 | ); |
226 | 241 | } |
227 | 242 |
|
|
0 commit comments