I managed to do some hack in order to zoom raphael paper, as setviewbox wasn't working for me, here is the function I wrote:
function setCTM(element, matrix) {
var s = "matrix(" + matrix.a + "," + matrix.b + "," + matrix.c + "," + matrix.d + "," + matrix.e + "," + matrix.f + ")";
element.setAttribute("transform", s);
}
Raphael.fn.zoomAndMove = function(coordx,coordy,zoom) {
var svg = document.getElementsByTagName("svg")[0];
var z = zoom;
var g = document.getElementById("viewport1");
var p = svg.createSVGPoint();
p.x = coordx;
p.y = coordy;
p = p.matrixTransform(g.getCTM().inverse());
var k = svg.createSVGMatrix().scale(z).translate(-p.x, -p.y);
setCTM(g, g.getCTM().multiply(k));
}
where the viewport1 element was defined as :
var gelem = document.createElementNS('http://www.w3.org/2000/svg', 'g');
gelem.id = 'viewport1';
paper.canvas.appendChild(gelem);
paper.canvas = gelem;
Then I can call: paper.zoomAndMove(minx,miny,zoomRatio);
Is it possible to transform the function to make it zoom smoothly?
For anybody (like me) who wanted to have the zooming & panning smoothly animated have a look here:
https://groups.google.com/forum/?fromgroups#!topic/raphaeljs/7eA9xq4enDo
this snipped helped me to automate and animate zooming and panning to a specific point on the canvas. (Props to Will Morgan)
Similar to above, but with differences:
linear
,easeIn
,easeOut
,easeInOut
,backIn
,backOut
,elastic
,bounce
)Since this is a complete rewrite I didn't modify the above.
See this JS Fiddle example for utilizing jQuery and Raphael's setViewBox which does both zooming and panning smoothly. We use the exact same functionality in a much more complicated and larger context and it works perfect and remains smooth even with a large number of items drawn on screen.
Basically, don't try and re-invent the wheel. I know this may not be the answer you are looking for, but it's almost certainly your best option.
EDIT: I've fixed the attached fiddle (it was broken due to changes JSFiddle made to the site) but apparently SO won't let me save JSFiddle links without including some code, so...
Minor mod to patrics' answer to get rid of "currentViewBox"... this works with Raphael 2.1.0:
Sry if it's bad etiquette to post a mod. Feel free to merge it in patrics' version, but then the comments don't make sense.