S
SMH
Normally an SVG document is loaded/parsed/interpreted inside an HTML
document using an 'object' (or 'embed') element, although there are
supposedly other ways too. The problem is, the SVG document must be
static this way.
I want to use the DOM interface to build SVG dynamically inside an HTML
document. I am guessing I can build it inside HTML within an 'object' (or
maybe 'iframe'?) element.
My intentions/goals:
In Javascript, I construct an object 'embedSVG' which has properties and
methods for creating valid SVG elements and setting their attributes and
attribute values.
During construction, the SVG document is created with its root element.
During debugging in FF 2.0 (I'll work on an MSIE-compatible format later),
I am using the Mozilla DOM Inspector and comparing nodes when the
'object' element is loading a valid external SVG document, and when I am
appending the child representing the SVG document created by the DOM
functions.
However the child node (#document) does not specify 'svg' as the root
element, but instead 'HTML'. Something is not working.
Here is the relevant code in 'ScriptTest.html' which is the HTML in which
the SVG is supposed to be embedded. Below it is the relevant code for
'svglib.js' which is supposed to contain code for building the SVG
dynamically.
What this code is supposed to do is load the HTML page and execute the
anonymous script, and draw a navy blue-bordered yellow rectangle on a
blank page. This is similar to the example in the SVG 1.1 W3C
Recommendation on page 202 of the 719-page PDF.
I am getting an exception when embedSVG object placeInHTML() method is
called: NS_ERROR_DOM_HIERARCHY_REQUEST_ERR. I find in DOM Inspector in
spite of or after the exception that a document is placed as a child of
the object element, but it is HTML, with a default 'head', 'title',
'body' elements placed.
Where am I blowing it?
===== start ScriptTest.html excerpt =====
..
..
..
<body>
<object id="insertSVG"></object>
<script type="text/javascript">
var svgHTMLnode = document.getElementById("insertSVG");
var svgObj = new embedSVG(400, 200);
svgObj.placeInHTML(svgHTMLnode);
svgObj.setFill("yellow");
svgObj.setStroke("navy");
svgObj.setStrokeWidth("10");
svgObj.drawRect(20, 20, 200, 100);
</script>
</body>
</html>
===== end ScriptTest.html excerpt =====
====== start svglib.js excerpt ======
function embedSVG (docWidth, docHeight )
{
//arg checking
var svgXMLdoc;
var namespaceURI = "http://www.w3.org/2000/svg";
var qualifiedName = "svg";
var doctype = null;
this.svgXMLdoc = svgXMLdoc =
document.implementation.createDocument(namespaceURI,
qualifiedName, doctype);
var rootElem = svgXMLdoc.firstChild;
rootElem.setAttribute("xmlns", "http://www.w3.org/2000/svg");
rootElem.setAttribute("height", String(docHeight));
rootElem.setAttribute("width", String(docWidth));
this.rootElement = rootElem;
this.fill = "";
this.setFill = function (fillString) { this.fill = fillString; };
this.stroke = "";
this.setStroke = function (strokeString) {
this.stroke = strokeString; };
this.strokeWidth = "";
this.setStrokeWidth = function (strokeWidthString) {
this.strokeWidth = strokeWidthString;
};
this.placeInHTML = function (htmlNode) {
return ((htmlNode.appendChild(this.svgXMLdoc));
};
this.drawRect = function (left, top, width, height,
roundedXradius, roundedYradius) {
var docElem = this.svgXMLdoc.createElement("rect");
// arg checking
docElem.setAttribute("x", left);
docElem.setAttribute("y", top);
docElem.setAttribute("width", width);
docElem.setAttribute("height", height);
docElem.setAttribute("rx", roundedXradius);
docElem.setAttribute("ry", roundedYradius);
docElem.setAttribute("fill", this.fill);
docElem.setAttribute("stroke", this.stroke);
docElem.setAttribute("strokeWidth", this.strokeWidth);
this.rootElement.appendChild(docElem);
return (docElem);
};
}
====== end svglib.js excerpt ======
I am being guided by N.C. Zakas' Professional JavaScript for Web
Developers.
document using an 'object' (or 'embed') element, although there are
supposedly other ways too. The problem is, the SVG document must be
static this way.
I want to use the DOM interface to build SVG dynamically inside an HTML
document. I am guessing I can build it inside HTML within an 'object' (or
maybe 'iframe'?) element.
My intentions/goals:
In Javascript, I construct an object 'embedSVG' which has properties and
methods for creating valid SVG elements and setting their attributes and
attribute values.
During construction, the SVG document is created with its root element.
During debugging in FF 2.0 (I'll work on an MSIE-compatible format later),
I am using the Mozilla DOM Inspector and comparing nodes when the
'object' element is loading a valid external SVG document, and when I am
appending the child representing the SVG document created by the DOM
functions.
However the child node (#document) does not specify 'svg' as the root
element, but instead 'HTML'. Something is not working.
Here is the relevant code in 'ScriptTest.html' which is the HTML in which
the SVG is supposed to be embedded. Below it is the relevant code for
'svglib.js' which is supposed to contain code for building the SVG
dynamically.
What this code is supposed to do is load the HTML page and execute the
anonymous script, and draw a navy blue-bordered yellow rectangle on a
blank page. This is similar to the example in the SVG 1.1 W3C
Recommendation on page 202 of the 719-page PDF.
I am getting an exception when embedSVG object placeInHTML() method is
called: NS_ERROR_DOM_HIERARCHY_REQUEST_ERR. I find in DOM Inspector in
spite of or after the exception that a document is placed as a child of
the object element, but it is HTML, with a default 'head', 'title',
'body' elements placed.
Where am I blowing it?
===== start ScriptTest.html excerpt =====
..
..
..
<body>
<object id="insertSVG"></object>
<script type="text/javascript">
var svgHTMLnode = document.getElementById("insertSVG");
var svgObj = new embedSVG(400, 200);
svgObj.placeInHTML(svgHTMLnode);
svgObj.setFill("yellow");
svgObj.setStroke("navy");
svgObj.setStrokeWidth("10");
svgObj.drawRect(20, 20, 200, 100);
</script>
</body>
</html>
===== end ScriptTest.html excerpt =====
====== start svglib.js excerpt ======
function embedSVG (docWidth, docHeight )
{
//arg checking
var svgXMLdoc;
var namespaceURI = "http://www.w3.org/2000/svg";
var qualifiedName = "svg";
var doctype = null;
this.svgXMLdoc = svgXMLdoc =
document.implementation.createDocument(namespaceURI,
qualifiedName, doctype);
var rootElem = svgXMLdoc.firstChild;
rootElem.setAttribute("xmlns", "http://www.w3.org/2000/svg");
rootElem.setAttribute("height", String(docHeight));
rootElem.setAttribute("width", String(docWidth));
this.rootElement = rootElem;
this.fill = "";
this.setFill = function (fillString) { this.fill = fillString; };
this.stroke = "";
this.setStroke = function (strokeString) {
this.stroke = strokeString; };
this.strokeWidth = "";
this.setStrokeWidth = function (strokeWidthString) {
this.strokeWidth = strokeWidthString;
};
this.placeInHTML = function (htmlNode) {
return ((htmlNode.appendChild(this.svgXMLdoc));
};
this.drawRect = function (left, top, width, height,
roundedXradius, roundedYradius) {
var docElem = this.svgXMLdoc.createElement("rect");
// arg checking
docElem.setAttribute("x", left);
docElem.setAttribute("y", top);
docElem.setAttribute("width", width);
docElem.setAttribute("height", height);
docElem.setAttribute("rx", roundedXradius);
docElem.setAttribute("ry", roundedYradius);
docElem.setAttribute("fill", this.fill);
docElem.setAttribute("stroke", this.stroke);
docElem.setAttribute("strokeWidth", this.strokeWidth);
this.rootElement.appendChild(docElem);
return (docElem);
};
}
====== end svglib.js excerpt ======
I am being guided by N.C. Zakas' Professional JavaScript for Web
Developers.