So I have an SVG file created in Inkscape embedded in a webpage, and I'd like it to rotate slowly. I've tried using Javascript and inserting animation commands directly into the SVG, but nothing works. I don't want to load in an entire JS library for this one task. This is what I have so far:
<html>
<body bgcolor="#333333">
<embed src="gear.svg" id="gear" width="1000" height="1000" style="position: absolute; top: -500px; left: -500px;" />
<script type="text/javascript">
var gear = document.getElementById("gear");
window.setInterval(function() {
// Somehow animate the gear.
}, 10);
</script>
</body>
</html>
<g>
element inside your<svg>
element that wraps everything inside the<svg>
, and add<animateTransform type="rotate" attributeName="transform" values="0 cx cy;360 cx cy" dur="30s"/>
as a child of that<g>
element, and replace "cx" and "cy" with whatever actual absolute point you want to use as the center of rotation, e.g "100 300". Should work in the newest generation of web browsers, apart from IE9.<g>
element inside your<svg>
element that wraps everything inside the<svg>
and then add a<script>
inside it that doesyourGelement.setAttribute("transform", "rotate(" + (newRotation++) + " cx cy)")
from awindow.setInterval
timer, as before replace the cx and cy with your center of rotation. This solution should be less than 10 lines of code, and should work fine even in older implementations that don't support declarative (SMIL) animations (e.g IE9, Firefox2, Safari3).Interesting topic because AFAIK currently Firefox doesn't support animation in SVG.
So I made a little investigation and found a working solution. Tested in Firefox 3.6, IE7 with Adobe plug-in, Opera 10.51, Safari 4.0.5, Chrome 5.0.
The background of the SVG area has no transparency in IE7, Safari and Chrome... I might try with the object tag (not supported by IE, probably need some conditional HTML...).
[EDIT] OK, I changed to use the more standard object (embed have never been defined in HTML...) except for IE where it isn't well supported by Adobe SVG plugin. The latter allows to add an attribute to have transparency of the embed object. For Webkit-based browsers, no transparency: see object embedded in HTML: default background should be transparent bug.
The HTML code:
The SVG code I used (only the ID is important, the SVG is from Mozilla SVG Project):