How to get the width and height of a BBox group of Raphael objects?

In Raphel.js, how to get the width and height of a BBox group of Raphael objects?

For example, I drew some elements on my Rapheal paper, as shown below:

var paper = Raphael(10, 50, 320, 200);
var st = paper.set();

var c = paper.rect(40, 40, 50, 50, 10);
var e = paper.ellipse(50, 50, 40, 20);
var i = paper.image("apple.png", 10, 10, 80, 80);
var t = paper.text(50, 50, "Raphaël\nkicks\nbutt!");
...

st.push(c, e, i, t ...);

I tried using the following method to get the width and height of a BBox group of elements:

var myBBox = st.getBBox();
var width = myBBox.width;
var height = myBBox.height;

console.log(width+','+height);

Sometimes it works, but sometimes I got the Infinity value for height. I think this is Raphael's mistake. So, if I want to get the current BBox size of my canvas (group of elements), what is the right way to do this?

+3
source share
1 answer

You can do it manually. For each element of the group:

  • left x. . var MinLeft

  • right. left + width x2. . var MaxRight

  • top y. . var MinTop

  • bottom. top + height y2. . var MaxBottom

  • : left= MinLeft; top= MinTop; height= MaxBottom-MinTop; width= MaxRight-MinLeft

+1

All Articles