Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 9 additions & 10 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,8 @@ function ParseFillStringToPathColor(fillString) {
// is valid and usable, will return the corresponding path color command. If
// the fill is unusable, will return empty string.
function GetPathColorCommandFromFill(element) {
const supportedSVGElements = ['path', 'circle', 'rect'];
const isElementSupported = supportedSVGElements.includes(element.tagName);
const fill = element.getAttribute('fill');
if (isElementSupported && fill && fill !== 'none') {
if (fill && fill !== 'none') {
// Colors in form #FFF or #FFFFFF.
const hexColorRegExp = /^#([0-9a-f]{3})$|^#([0-9a-f]{6})$/gi;
const fillMatch = fill.match(hexColorRegExp);
Expand All @@ -112,14 +110,7 @@ function GetPathColorCommandFromFill(element) {
function HandleNode(svgNode, scaleX, scaleY, translateX, translateY, preserveFill) {
var output = '';
for (var idx = 0; idx < svgNode.children.length; ++idx) {
if (idx !== 0)
output += "NEW_PATH,\n";

var svgElement = svgNode.children[idx];

if (preserveFill)
output += GetPathColorCommandFromFill(svgElement);

switch (svgElement.tagName) {
// g ---------------------------------------------------------------------
case 'g':
Expand All @@ -138,6 +129,8 @@ function HandleNode(svgNode, scaleX, scaleY, translateX, translateY, preserveFil
break;

output += "NEW_PATH,\n";
if (preserveFill)
output += GetPathColorCommandFromFill(svgElement);

var commands = [];
var path = svgElement.getAttribute('d').replace(/,/g, ' ').trim();
Expand Down Expand Up @@ -263,6 +256,8 @@ function HandleNode(svgNode, scaleX, scaleY, translateX, translateY, preserveFil
// CIRCLE ----------------------------------------------------------------
case 'circle':
output += "NEW_PATH,\n";
if (preserveFill)
output += GetPathColorCommandFromFill(svgElement);

var cx = parseFloat(svgElement.getAttribute('cx'));
cx *= scaleX;
Expand All @@ -277,6 +272,8 @@ function HandleNode(svgNode, scaleX, scaleY, translateX, translateY, preserveFil
// RECT ------------------------------------------------------------------
case 'rect':
output += "NEW_PATH,\n";
if (preserveFill)
output += GetPathColorCommandFromFill(svgElement);

var x = parseFloat(svgElement.getAttribute('x')) || 0;
x *= scaleX;
Expand All @@ -299,6 +296,8 @@ function HandleNode(svgNode, scaleX, scaleY, translateX, translateY, preserveFil
// OVAL ----------------------------------------------------------------
case 'ellipse':
output += "NEW_PATH,\n";
if (preserveFill)
output += GetPathColorCommandFromFill(svgElement);

var cx = parseFloat(svgElement.getAttribute('cx')) || 0;
cx *= scaleX;
Expand Down