拉斐尔纸缩放动画
我设法做了一些 hack 来缩放 raphael paper,因为 setviewbox 不适合我,这是我编写的函数:
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));
}
其中 viewport1 元素定义为:
var gelem = document.createElementNS('http://www.w3.org/2000/svg', 'g');
gelem.id = 'viewport1';
paper.canvas.appendChild(gelem);
paper.canvas = gelem;
然后我可以调用: paper.zoomAndMove(minx ,miny,zoomRatio);
是否可以对函数进行改造,使其平滑缩放?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
对于任何想要缩放和缩放的人(像我一样)平滑平移动画看看这里:
https://groups.google.com/forum/?fromgroups #!topic/raphaeljs/7eA9xq4enDo
这个片段帮助我自动缩放和平移到画布上的特定点并设置动画。 (支持威尔·摩根)
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)
请参阅这个 JS Fiddle 示例,了解如何利用 jQuery 和 Raphael 的 setViewBox,它可以平滑地进行缩放和平移。我们在更复杂和更大的环境中使用完全相同的功能,即使在屏幕上绘制大量项目,它也能完美运行并保持流畅。
基本上,不要尝试重新发明轮子。我知道这可能不是您正在寻找的答案,但它几乎肯定是您的最佳选择。
编辑:
我已经修复了附加的小提琴(由于 JSFiddle 对网站所做的更改而损坏),但显然不允许我在不包含一些代码的情况下保存 JSFiddle 链接,所以...
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...
对 patrics 的回答进行了小修改,以摆脱“currentViewBox”...这适用于 Raphael 2.1.0:
如果发布 mod 是不礼貌的行为,请原谅。请随意将其合并到 patrics 的版本中,但这样注释就没有意义了。
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.
与上面类似,但有区别:
线性
、easeIn
、easeOut
、easeInOut
、backIn
、backOut
、elastic
、bounce
)由于这是完全重写,因此我没有修改上面的内容。
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.