Skip to content

Instantly share code, notes, and snippets.

@tobiasfabian
Created September 19, 2022 09:59
Show Gist options
  • Save tobiasfabian/cef60d13b3c44976d2c6082404d5a958 to your computer and use it in GitHub Desktop.
Save tobiasfabian/cef60d13b3c44976d2c6082404d5a958 to your computer and use it in GitHub Desktop.
const svgPolygon = '12.034 19.674 10.634 18.28 14.309 14.61 2.125 14.61 2.125 12.572 14.309 12.572 10.634 8.897 12.034 7.508 18.117 13.591';
const viewBoxWidth = 25;
const viewBoxHeight = 25;
const array = [...svgPolygon.matchAll(/[\d.]+/g)];
let output = '';
array.forEach((item, key) => {
if (key % 2 === 0) {
output += `${(Math.round(((item / viewBoxWidth) * 100) * 1000)) / 1000}% `;
} else {
output += `${(Math.round(((item / viewBoxHeight) * 100) * 1000)) / 1000}%`;
if (key !== array.length - 1) {
output += ', ';
}
}
});
console.log(output);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment