Hi, I am attaching some code to animate a small group which consists of a object (rect / circle etc) with some attached text.
In the snap.animate function/method, the FROM/ TO accepts a value of 0, xxx, where the change xxx in x,y coordinates is applied equally to both x an y. That will result in a motion at approximately 45 degrees. I need to specify the x, y values separately so that the motion can be from fully from a horizontal motion to a vertical motion.
I tried three options as can be seen from the code below, only the first animate function/method works.
Whats wrong, please help, I am confounded and searching on the internet for over a week has not helped.
here is the code:
<!DOCTYPE html>
<html>
<head>
<title>SVG Example</title>
<script src="js/snap.svg-min.js"></script>
<style>
#svg {
background-color: #000;
}
</style>
</head>
<body>
<input id="button" type="button" class="zhxx_api_button" value="click Me">
<svg id="svg" width="494" height="406"></svg>
<script>
var svg = Snap(494,406);
var ta = Snap("#svg").paper.circle(100, 200, 8).attr({
"stroke": "#FF0",
"stroke-width": 2,
"fill": "#FF0",
"transform": "translate(200, 150)"
});
var ta_txt = Snap("#svg").paper.text(ta.getBBox().cx + 10, ta.getBBox().cy, "-01").attr({
"stroke": "#FF0",
"stroke-width": 1
});
var ta_group = Snap("#svg").group(ta, ta_txt);
//***********************************************
var ra = Snap("#svg").paper.rect(250, 200, 14,14).attr({
"stroke": "#F00",
"stroke-width": 2,
"fill": "#F00",
"transform": "translate(50, 200)"
});
var ra_txt = Snap("#svg").paper.text(ra.getBBox().cx + 10, ra.getBBox().cy, "00").attr({
"stroke": "#F00",
"stroke-width": 1
});
var ra_group = Snap("#svg").group(ra, ra_txt);
//***********************************************
document.getElementById("button").onclick = function() {
var x_move = 100;
var y_move = -100;
var path = "m 17.764486,109.36261 170.428034,-1.11027 -33.86355,47.18691";
var from = 0;
var to = path.getTotalLength();
var duration = 5000;
//METHOD 1 WORKS
Snap.animate(0,-200, function(val) {
ta_group.transform('translate(' + val + ',' + val + ')');
}, 2000);
// METHOD 2 FAILS
Snap.animate(0, {x_move, y_move}, function(val) {
ra_group.transform('translate(' + val + ',' + val + ')');
}, 7000);
// METHOD 3 FAILS
Snap.animate(from, to , function( val ) {
var point = path.getPointAtLength( val );
var movePoint_x = point.x - ra.getBBox().cx;
var movePoint_y = point.y - ra.getBBox().cy;
ra_group.transform( 't' + movePoint_x + ',' + movePoint_y);
}, duration);
}; //button press
</script>
</body>
</html>