Reputation: 147
I am using Raphael.js to create a "Play" button icon. However, the triangle is positioned too close to the edge of the circle. On mouseover, I am increasing the stroke width - the problem being that the triangle actually comes outside the circle, which is not what I want at all. The HTML is just a div with an id:
<html>
<head>
</head>
<body>
<div id="playB"></div>
</body>
</html>
The javascript is rather long so here is the jsfiddle
What is the easiest way to "move" a path of an SVG in this case? i.e. - starting further from the right and further from the top. I have tried various things, but apart from changing the size of an icon - multiplying each number of the path by x, I cannot manipulate icons at all. In this case move a specific element of the icon.
Upvotes: 2
Views: 1697
Reputation: 1514
I took the easy way out and altered your path within inkscape. It's pretty easy to do. If you're interested, here's what I did.
Then I just selected the triangle nodes and moved them over. Go back to the xml editor, select the "d" value, and put that into your fiddle.
var playBP = playB.path("M 64,5.864 C 31.892,5.864 5.864,31.892 5.864,
64 5.864,96.108 31.892,122.136 64,122.136 96.108,122.136 122.136,96.108 122.136,
64 122.136,31.892 96.108,5.864 64,5.864 z M 38.640816,102.728 109.16882,
62 38.640816,21.272 z");
Upvotes: 0
Reputation: 6719
You need to split up the path in order to adjust the position of the triangle. I was able to do this by eye by picking out where the second "M" was in the path:
var playB = Raphael('playB', 200, 200);
var circle = playB.path("M64,5.864C31.892,5.864,5.864,31.892,5.864,64c0,32.108,26.028,58.136,58.136,58.136c32.108,0,58.136-26.028,58.136-58.136C122.136,31.892,96.108,5.864,64,5.864z");
var triangle = playB.path("M26.736,102.728L97.264,62L26.736,21.272V102.728z");
Then you can move the triangle over:
triangle.attr("transform", "T15,0");
To keep the properties and animation intact, add both to a Raphael set:
var playBP = playB.set();
playBP.push(circle, triangle);
playBP.attr({
'stroke-width': 5,
'stroke': "#000",
'fill': "#fff",
'opacity': 1
});
Then you only need one small change: Change "this" in the mouseover commands to "playBP". This may seem redundant, but "this" refers to the single element that was moused over, not the set.
var speed = 5;
playBP.mouseover(function(){
playBP.animate({
'stroke-width': 10,
'stroke': "#fff",
'fill': "#000",
'opacity': .9
}, 1000, 'elastic');
});
playBP.mouseout(function(){
playBP.stop().animate({
'stroke-width': 5,
'stroke': "#000",
'fill': "#fff",
'opacity': 1
}, speed*4, 'elastic');
});
If you'd rather just fix the path, you can use the Raphael.mapPath() function to get the adjusted x/y for the triangle, like so:
console.log(Raphael.mapPath(triangle.attr("path"), triangle.matrix));
Upvotes: 2
Reputation: 11461
Use el.transform('t{x},{y}')
, for example el.transform('t1,1')
to translate any element right and down by 1 pixel each. You can substitute different values for x and y as needed, including negative values.
Upvotes: -1