I've never done JS before and I was just learning CSS now, but stumbled upon this animation tutorial and wanted to try it.
http://goinkscape.com/how-to-animate-icons-with-inkscape-and-snap-svg/
It didn't work tho :< (blank page output) ... I think it's because I have to extract the JS var so I can output it. I looked for solutions here on SO and tried document.getElementById('iconDiv').innerHTML = s;
but that didn't work.
Any help is appreciated.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Inkscape Animated Icon Snap</title>
<!--We need to add the Snap.svg script to our document-->
<script src="snap.svg-min.js"></script>
<script>
//Run script right away
window.onload = function () {
//We'll be appending the icon to this DIV later
var s = Snap("#iconDiv");
//Have Snap load the SVG file
Snap.load("icon.svg", function(f) {
//Assign the white rectangle
whiteRect = f.select("#whiteRect");
//Assign the whole icon group
icon = f.select("#icon");
//When the icon is hovered over, have the white rectangle move up slightly with elastic properties
icon.hover(function() {
whiteRect.animate({y:960}, 500, mina.elastic);
},
//And return to original position when not hovered over
function() {
whiteRect.animate({y:977.36218}, 500, mina.elastic);
}
);
//Finally append the icon to iconDiv in the body
s.append(f);
});
};
</script>
</head>
<body>
<!--Here's the DIV that will hold the animated SVG icon-->
<div id="iconDiv"></div>
</body>
</html>
I went back and did it again as follows:
- Made a 100x100 pixel blue square. Set ID:blueRect, Label: #blueRect.
- Made a 50x50 pixel white square. Set ID:whiteRect, Label: #whiteRect.
- Grouped them and set ID:icon, Label: #icon.
- Saved icon.svg as Plain SVG in the same folder where my index.html file is.
- Downloaded Snap.svg and put snap.svg-min.js in the same folder where my index.html file is.
Opened icon.svg in notepad:
"id="whiteRect" width="13.229167" height="13.229167" x="6.6145835" y="277.15625"
I don't know why those are the numbers (same as from my first trial) since I specifically put my squares at 0,0 this time ... I even saved a copy with a different name. Still getting those numbers, but whatever...
Copy pasted the html code directly from the website and changed the y first to 277.15625, then to 0.
Set the other y to 270, then -15.
icon.hover(function() { whiteRect.animate({y:-15}, 500, mina.elastic);
Both times, got a blank page.