Last active
April 29, 2026 16:56
-
-
Save simonwep/e1220c027443786949d990ec9c1c6c83 to your computer and use it in GitHub Desktop.
Transform a svg path's coordinates
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const scalePath = (path: string, oldSize: number, newSize: number): string => { | |
| const tokens = path.match(/[a-zA-Z]|-?\d*\.?\d+/g); | |
| if (!tokens) return path; | |
| const normalize = (v: number) => (v / oldSize) * (newSize - 1); | |
| let i = 0; | |
| const result: string[] = []; | |
| while (i < tokens.length) { | |
| const token = tokens[i++]; | |
| if (isNaN(Number(token))) { | |
| result.push(token); | |
| continue; | |
| } | |
| const x = normalize(parseFloat(token)); | |
| const yToken = tokens[i++]; | |
| if (!yToken) break; | |
| const y = normalize(parseFloat(yToken)); | |
| result.push(`${x.toFixed(2)} ${y.toFixed(2)}`); | |
| } | |
| return result.join(' '); | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment