Scaling SVG (Raphael.js) like an SWF

12,985

Solution 1

It took me awhile but I finally came up with a solution to this problem. I've wrapped the solution in a small js file that can be used with Raphael. You can get the js file along with some simple documentation here. See it in action.

How it works:

  1. use viewBox for svg
  2. wrap all vml nodes in a group node
  3. wrap the Raphael constructor up so that the vml group node is passed to the Raphael constructor
  4. alter a few css properties when the paper is resized to deal with centering, clipping and maintaining the correct aspect ratio.

Any feedback would be greatly appreciated.

Solution 2

You could loop over all paths and scale() them acording to the new scale of the paper after resizing it.

Solution 3

You could add a 'viewBox' attribute on the svg root element to define the coordinate system, the shapes will then scale to whatever the size of the container is. Not sure how to deal with the VML side of things though. I'd suggest reporting an issue for it, https://github.com/DmitryBaranovskiy/raphael/issues.

Solution 4

This might just be too old of a thread, but Raphael 2.0 at least has a setViewBox method -- http://raphaeljs.com/reference.html#Paper.setViewBox

You'll still need to set the actual canvas size to 100%, so the container scales to the window, but calling setViewBox with appropriate w/h in your window.resize callback should do the trick.

Share:
12,985
Zevan
Author by

Zevan

Programmer / Artist / Consultant

Updated on June 06, 2022

Comments

  • Zevan
    Zevan almost 2 years

    I started using Raphael.js a few days ago and I'm really enjoying it. The only thing I haven't been able to figure out is how to get the "paper" or svg/vml tag to fill the browser window like an swf. See this example.

    Note the way the above example resizes with the browser window

    I was able to get the "paper" to resize with the browser window, but no luck getting all the vector graphics to adjust their size. Any feedback would be greatly appreciated.

    EDIT

    I tried a bunch of different routes with this. viewBox worked great but its SVG only. I just figured out how to do it using Raphael sets and a little code on the window.onresize event. I'll post my findings later tonight or tomorrow. I'd still really like to see other solutions to the question if there are any.

  • Zevan
    Zevan over 13 years
    Cool I'll give this a try - and I'll look into the VML side of things.
  • Zevan
    Zevan over 13 years
    turns out that there is no need to loop if you use a set and do myset.scale(2,2,0,0)
  • Gipsy King
    Gipsy King over 13 years
    @Zeven Interesting! I didn't look into sets when I used raphaël.
  • Zevan
    Zevan over 13 years
    I spoke to soon though. Unfortunately if you scale a set of objects the coordinate system isn't preserved which can really mess up any animation that might be going on. Meaning the cx and cy change depending on scale. I'm pretty sure this could be avoided if Raphael were using the transform() SVG stuff. Not sure why scale is done manually rather than with built in SVG functionality. Maybe it has something to do with having the code be easily compatable with VML. I'm going to keep looking into it. Worst case I'll write some kind of plug-in.
  • Zevan
    Zevan over 13 years
    This really works great. The equivalent in VML seems to be to wrap all the objects in a group node. I did this and it actually worked nicely. Only problem is that Raphael wasn't able to fill or style the objects anymore. I guess because of the change to the VML structure.
  • Spadar Shut
    Spadar Shut almost 13 years
    I guess in IE the width and height of the paper could be set to an expression which calculates the dimensions of the parent element.
  • drzaus
    drzaus over 11 years
    here's an equally old random example of using setViewBox to "zoom" on mousewheel - jsfiddle.net/9zu4U/2
  • dsdsdsdsd
    dsdsdsdsd over 10 years
    can you only have one instance of ScaleRaphael?? ... I am putting .svg files into thumbnails ... the original .svg's are about 400x400, but the thumbs are 75x75 ... I have been using a unique instance of Raphael( container_id ) for each thumb. ... BUT it seems that when I switch to new ScaleRaphael( container_id , 75, 75), all the thumbs are getting lumped into one visual element.