My favorites | Sign in
Project Home Downloads Wiki Issues Source
Search
for
SampleCode  
Usage and Sample Code
Featured
Updated Nov 2, 2010 by n...@sgtwilko.f9.co.uk

Using svg2vml

New Users

Using svg2vml is very simple and requires very little change to your existing pages.

Simply place the svg2vml Javascript file on your website add the following to the top of any HTML file in which you wish to use the SVG DOM.

<script src="svg2vml.js"></script>
<script>var vectorModel = new VectorModel();</script>

You will have to modify the path to the svg2vml Javascript file if you place it in a different folder to the HTML file. You will need to place the call to svg2vml above all Javascript code that uses the SVG DOM, preferable place it above all other Javascript.

There should be no modification required to standards compliant code as the normal SVG calls are translated to VML

You may need to verify if your code is using the correct methods to create and use objects. Some browsers allow the use of "document.createElement" to create SVG objects, when instead you should be using "document.createElementNS" and pass in the correct "http://www.w3.org/2000/svg" Namespace.

The following are a few simple examples of the SVG DOM, with svg2vml translating for VML only Browsers.

  • A HTML DIV element with rounded corners by using a SVG - Example.
  • A selection of SVG shapes with gradients - Example.
  • SVG line elements - Example.
  • A version of the original SVG logo created by Harvey Rayner - Example.

Existing users

If you have been using the version 1.0 "vectorModel" code, there are changes that will be made that will make svg2vml incompatible with your existing code.

Since the current released version (1.1.1) is a maintenance release you can carry on using your existing code. However, in future releases this object will not exist and you should start making changes now to be able to take advantage of the newer code when version 1.2 is released.

Simply replace any reference of the "vectorModel.createElement(..." variable with "document.createElementNS("http://www.w3.org/2000/svg",...".

eg;

	var container = document.getElementById("content");
				
	var mySvg = vectorModel.createElement("svg");
	container.appendChild(mySvg);
	mySvg.setAttribute("version", "1.1");
	var myG = vectorModel.createElement("g");
	mySvg.appendChild(myG);

would become

	var container = document.getElementById("content");
				
	var mySvg = document.createElementNS("http://www.w3.org/2000/svg","svg");
	container.appendChild(mySvg);
	mySvg.setAttribute("version", "1.1");
	var myG = document.createElementNS("http://www.w3.org/2000/svg","g");
	mySvg.appendChild(myG);
Comment by josep....@gmail.com, Jan 8, 2008

var myPoly = vectorModel.createElement("polyline"); myPoly.setAttribute("points","100,120,50,90,60,10,100,100"); myPoly.setAttribute("stroke", "#ffff00"); myPoly.setAttribute("stroke-width", "4px"); myPoly.setAttribute("fill","none"); myG.appendChild(myPoly);

var createVMLPolyline = function() {

var domElement = document.createElement("v:polyline");
domElement.style.width = '21600'; domElement.style.height = '21600'; domElement.coordsize = '21600, 21600'; domElement.style.position = "absolute";
;
domElement.setAttribute = function( key, value ) {
if ( key == "points" ) {
this.points = value; return;
} else if ( key == "stroke" ) {
this.strokecolor = value; return;
} else if ( key == "stroke-width" ) {
this.strokeweight = parseFloat(value)/1.2 + "pt"; return;
} else if ( key == "fill" ) {
return this.fill;
}
}
domElement.getAttribute = function( key ) {
if ( key == "points" ) {
if ( !this.points ) { return 0; } return points;
} else if ( key == "stroke" ) {
return this.strokecolor;
} else if ( key == "stroke-width" ) {
return parseFloat( this.strokecolor ) 1.2 + 'px';
} else if ( key == "fill" ) {
return this.fill;
} else if ( key == "width" ) {
return parseInt( this.style.width );
} else if ( key == "height" ) {
return parseInt( this.style.height );
}
}

return domElement;
};

Comment by buf...@gmail.com, Apr 6, 2008

the polyline extension seems to work in ff and opera, thx. there is maybe a "/" missing before the 1.2 + 'px'.

the polyline extension does not work in IE so. you have to add `

} else if ( element == "polyline") {
return createVMLPolyline();
` in the else if ( this.vml_capable ) branch.


Sign in to add a comment
Powered by Google Project Hosting