Skip to content

Instantly share code, notes, and snippets.

@simonwep
Last active April 29, 2026 16:56
Show Gist options
  • Select an option

  • Save simonwep/e1220c027443786949d990ec9c1c6c83 to your computer and use it in GitHub Desktop.

Select an option

Save simonwep/e1220c027443786949d990ec9c1c6c83 to your computer and use it in GitHub Desktop.
Transform a svg path's coordinates
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