Howto:Extend the Canvas SVG module: Difference between revisions

Jump to navigation Jump to search
m
Line 139: Line 139:
Once we do have a valid filename/URL, we can simply set the filename property of the new Canvas.Image child. Once again, it makes sense to look at other elements, e.g. Canvas.Text, and its way to set the value of a node:
Once we do have a valid filename/URL, we can simply set the filename property of the new Canvas.Image child. Once again, it makes sense to look at other elements, e.g. Canvas.Text, and its way to set the value of a node:


<syntaxhighlight lang="nasal">
  pushElement('text', id);
  pushElement('text', id);
  stack[-1].set("text", tspan.text);
  stack[-1].set("text", tspan.text);
</syntaxhighlight>


So, now we need to look up the corresponding property that sets the filename, we can do that by opening api.nas and looking up the setFile() method in Canvas.Image:
So, now we need to look up the corresponding property that sets the filename, we can do that by opening api.nas and looking up the setFile() method in Canvas.Image:
Line 156: Line 158:


As we can now see easily, the correct property to set is named '''src'''.
As we can now see easily, the correct property to set is named '''src'''.
Thus, we can now come up with a suitable attempt at mapping the SVG image tag to a Canvas.Image:
<syntaxhighlight lang="nasal">
var ref = attr["xlink:href"];
      if( ref == nil or size(ref) < 2 or ref[0] != `#` )
        return printlog("warn", "Invalid or missing href in image tag: '" ~ ref ~ '"');
pushElement('image',  attr['id']);
stack[-1].set("src", ref); # set the filename to the xlink/href value
</syntaxhighlight>

Navigation menu