Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Applying a transformation to a set in Raphael.js

Tags:

raphael

Using Raphael 2.0, I am trying to apply a transform to a set of objects in a way that is relative to all of the objects in the set. However, the effect I am getting is that the transform is applied to each item individually, irrespective of the other objects in the set.

For example: http://jsfiddle.net/tim_iles/VCca9/8/ - if you now uncomment the last line and run the code, each circle is scaled to 0.5x. The actual effect I am trying to achieve would be to scale the whole set of circles, so their relative distances are also scaled, which should put all four of them inside the bounding box of the white square.

Is there a way to achieve this using Raphael's built in tools?

like image 730
Tim Iles Avatar asked Apr 10 '12 11:04

Tim Iles


People also ask

How to change the attributes of an SVG shape in Raphaël?

Every SVG shape object in Raphaël contains the attr method that can be used to change various attributes of the shape. The argument that you give to attr is an object with the properties you want to shape to have.

What is Raphael JS?

In contrast, Raphael.js uses SVG vector images and is designed to seamlessly integrate with on page events such as clicking, hovering, and dragging. Moreover, Raphael.js provides great legacy browser support (including IE7 and IE8).

How to change the shape of a circle in Raphaël?

Every SVG shape object in Raphaël contains the attr method that can be used to change various attributes of the shape. The argument that you give to attr is an object with the properties you want to shape to have. In this case, we want to manipulate the circle to make it look more like our example.

How do I animate an element in Raphael?

To animate an element we simply need to provide Raphael with a new set of attr properties that describe the final state of the element. For example, we could specify that an element take on a new color or move to a new location.


1 Answers

When you scale, the first parameter is the X-scale. If you provide no other parameters, it will use that as the Y-scale, and scale around the center of the object.

When you scaled the rectangle, it scaled around the center of the rectangle. If you want the circles to scale around that point as well, rather than their centers, you should provide that point.

So the last line could be set.transform("s0.5,0.5,100,100"); (100,100 being the center of the rectangle you scaled)

At least, I think this is what you're asking for.

like image 131
user1322892 Avatar answered Sep 17 '22 18:09

user1322892