Svg pass parameter

Viewed 3496

I'm trying to pass parameters to the svg, but I'm not doing something wrong can you give me a hand?

Link: codesandbox

Html:

<object type="image/svg+xml" data="button.svg?color=yellow">
      <param name="color" value="red" />
      <param name="label" value="stop" />
</object>

Svg:

<svg
    xmlns="http://www.w3.org/2000/svg"
    xmlns:xlink="http://www.w3.org/1999/xlink"
viewBox="0 0 110 40"
width="100%"
height="100%">
    <g>
        <rect 
id="button_rect" 
x="5" 
y="5" 
width="100" 
height="30" 
rx="15" 
ry="15" 
fill="param(color) blue" 
stroke="navy"
/>
<text id="button_label" x="55" y="30" text-anchor="middle" 
            font-size="25" fill="white" font-family="Verdana"
            content-value="param(label)">
            Ok
          </text>
    </g>
</svg>
1 Answers

The folks who have written the specs have noted that while this isn't implemented in the browser itself, you need to include a Javascript library. What they left out is where you should include it. Obviously your first thought might be to include it inside the html document but that's not the case - it needs to be included in the .svg file as:

<script type="text/ecmascript" xlink:href="https://dev.w3.org/SVG/modules/ref/master/ref2.js"></script>

Furthermore I'd recommend referencing the parameters using the url(#parameterName) syntax.

For example:

<html>
  <head>
    <title></title>
    <meta charset="UTF-8" />
    <link rel="stylesheet" href="./style.css" />
  </head>
  <body>
    <object type="image/svg+xml" data="button.svg?fill=red">
      <param name="fill" value="blue" />
      <param name="stroke" value="red" />
    </object>
  </body>
</html>

and button.svg:

<svg
    xmlns="http://www.w3.org/2000/svg"
    xmlns:xlink="http://www.w3.org/1999/xlink"
viewBox="0 0 110 40"
width="100%"
height="100%">
<defs>
    <ref id="paramX" param="fill" default="red"/>
    <ref id="paramY" param="stroke" default="blue"/>

</defs>
    <g>
        <rect 
id="button_rect" 
x="5" 
y="5" 
width="100" 
height="30" 
rx="15" 
ry="15" 
fill="url(#paramX)" 
stroke="url(#paramY)"
/>
<text 
id="button_label" 
x="55"
y="30"
text-anchor="middle" 
font-size="25"
fill="white"
font-family="Verdana" >
Go
</text>
    </g>
<script type="text/ecmascript" xlink:href="https://dev.w3.org/SVG/modules/ref/master/ref2.js"></script>
</svg>
Related