svgdotjs / svg.import.js

A plugin for importing raw SVG into the svg.js library

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Style attribute of imported elements

marthijn opened this issue · comments

In the SVG file the style of the element (fill, stroke, etc.) is stored in the style attribute:

<path id="path4261" d="m 529.889 ... 2.125,-8.125 z" style="fill:#ffffff;"></path>

However, when I do:

store.path4261.fill('#000000');

the element gets the fill attribute and nothing happens:

<path id="path4261" d="m 529.889 ... 2.125,-8.125 z" style="fill:#ffffff;" fill="#000000"></path>

It is possible to change the fill using this code:

attr({ style: 'fill:#000000' })

but then animation() will not work.

commented

That's right indeed, styles overrule attributes. But you can fix this easily on import:

draw.svg(rawSvg, function() {
  var fill

  if (fill = this.style('fill'))
    this.fill(fill).style('fill', null)
})

It works, thanks!

commented

Great!