<community>of XML and SVG Developers</community>
This is a discussion on use "symbol" and "use" in javascrtipt. within the SVG Questions forums, part of the SVG Forums category; I wan to craet "symbol" element and use "use" instation it.But in fact, it is not so simple as that ...
| |||||||
| Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| |||
| Here is my code Code: <?xml version="1.0" encoding="ISO-8859-1" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN"
"http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd" >
<svg id="thesvg" x="0" y="0" width="100%" height="100%"
onload="Init(evt)"
xmlns:a3="http://ns.adobe.com/AdobeSVGViewerExtensions/3.0/"
a3:scriptImplementation="Adobe"
xmlns:xlink="http://www.w3.org/1999/xlink">
<script><![CDATA[
var svgdoc;
var svgnsz="http://www.w3.org/2000/svg";
var xlnsz="http://www.w3.org/1999/xlink";
function Init(evt)
{
var targ = evt.getTarget();
svgdoc = targ.getOwnerDocument();
createSymbol();
useSymbol();
}
function createSymbol()
{
var df = svgdoc.createElementNS(svgnsz,"defs");
var symb = svgdoc.createElementNS(svgnsz,"symbol");
symb.setAttribute("id","einsym");
symb.setAttribute("viewBox","0 0 20 30");
symb.setAttribute("preserveAsperctRation","xMidYmid meet");
var rc = svgdoc.createElement("rect");
rc.setAttribute("x","0");
rc.setAttribute("y","16");
rc.setAttribute("width","10");
rc.setAttribute("height","10");
rc.setAttribute("fill","green");
symb.appendChild(rc);
df.appendChild(symb);
svgdoc.appendChild(df);
}
function useSymbol()
{
var ub = svgdoc.createElement("use");
ub.setAttribute("x","200");
ub.setAttribute("y","80");
ub.setAttribute("xlink:href","#einsym");
ub.setAttribute("width","50");
ub.setAttribute("height","50");
svgdoc.appendChild(ub);
}
]]></script>
</svg>
Wish best.Thank you. |
| |||
|
Two errors : - If you copy your svg after loading, you can see that <defs> element is after <svg> - xlink:href is not in svg namespace and in script you have to add xlink:href attribute with setAttributeNS This code run in ASV3 + IE Code: <?xml version="1.0" encoding="ISO-8859-1" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN" "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd" >
<svg id="thesvg" x="0" y="0" width="100%" height="100%" onload="Init(evt)"
xmlns:a3="http://ns.adobe.com/AdobeSVGViewerExtensions/3.0/"
a3:scriptImplementation="Adobe"
xmlns:xlink="http://www.w3.org/1999/xlink">
<script><![CDATA[
var svgdoc;
var svgnsz="http://www.w3.org/2000/svg";
var xlnsz="http://www.w3.org/1999/xlink";
function Init(evt)
{
var targ = evt.target // don't use get method for this object
svgdoc = targ.ownerDocument // idem
createSymbol();
useSymbol();
}
function createSymbol()
{
var df = svgdoc.createElementNS(svgnsz,"defs");
var symb = svgdoc.createElementNS(svgnsz,"symbol");
symb.setAttribute("id","einsym");
symb.setAttribute("viewBox","0 0 20 30");
symb.setAttribute("preserveAsperctRation","xMidYmid meet");
var rc = svgdoc.createElement("rect");
rc.setAttribute("x","0");
rc.setAttribute("y","16");
rc.setAttribute("width","10");
rc.setAttribute("height","10");
rc.setAttribute("fill","green");
symb.appendChild(rc);
df.appendChild(symb);
svgdoc.firstChild.appendChild(df); // add symbol in main svg
}
function useSymbol()
{
var ub = svgdoc.createElement("use");
ub.setAttribute("x","200");
ub.setAttribute("y","80");
ub.setAttributeNS(
"http://www.w3.org/2000/xlink/namespace/",
"xlink:href",
"#einsym"); // href namespace
ub.setAttribute("width","50");
ub.setAttribute("height","50");
svgdoc.firstChild.appendChild(ub); // add use element in main svg
}
]]></script>
</svg>
|
| |||
|
Thanks Michel for your reply. Though the problem has been solved,I have still sevea questions . 1 Quote:
2.In your code: Quote:
Quote:
3.Is there anything different between the setAttribute and setAttributeNS?In what case to msut use setAttributeNS? By the way,In where can I find the details about the "evt"? Oh, my question seems a lot of. I'm sorry for it .Hope not to need to take much of your time,Michel.thank you . |
| ||||
| Quote:
Quote:
Quote:
setAttributeNS(null,"x",200) for svg namespace attribute setAttributeNS(some namespace,some attribute,some value) for other attribute Quote:
Michel |
| |||
| Quote:
|
![]() |
| Tags |
| javascrtipt, symbol |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
| |
Ink Cartridges For Inkjet Printers
It is much cheaper to buy ink cartridges for inkjet printers online.
hard disk drive data recovery lancashire
Data Clinic's dedicated Customer Service and excellent hard disk drive data recovery extend even to Lancashire and surroundings.