Yuk*_*lix 10 jquery animation svg zoom raphael
我设法做了一些破解以缩放raphael纸,因为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));
}
Run Code Online (Sandbox Code Playgroud)
其中viewport1元素定义为:
var gelem = document.createElementNS('http://www.w3.org/2000/svg', 'g');
gelem.id = 'viewport1';
paper.canvas.appendChild(gelem);
paper.canvas = gelem;
Run Code Online (Sandbox Code Playgroud)
然后我可以打电话: paper.zoomAndMove(minx,miny,zoomRatio);
是否可以转换功能以使其平滑变焦?
pat*_*ics 14
对于想要平滑缩放和平移动画的任何人(像我一样)看看这里:
https://groups.google.com/forum/?fromgroups#!topic/raphaeljs/7eA9xq4enDo
这个剪辑帮助我自动化和动画缩放和平移到画布上的特定点.(威尔摩根的道具)
Raphael.fn.animateViewBox = function(currentViewBox, viewX, viewY, width, height, duration, callback) {
duration = duration || 250;
var originals = currentViewBox, //current viewBox Data from where the animation should start
differences = {
x: viewX - originals.x,
y: viewY - originals.y,
width: width - originals.width,
height: height - originals.height
},
delay = 13,
stepsNum = Math.ceil(duration / delay),
stepped = {
x: differences.x / stepsNum,
y: differences.y / stepsNum,
width: differences.width / stepsNum,
height: differences.height / stepsNum
}, i,
canvas = this;
/**
* Using a lambda to protect a variable with its own scope.
* Otherwise, the variable would be incremented inside the loop, but its
* final value would be read at run time in the future.
*/
function timerFn(iterator) {
return function() {
canvas.setViewBox(
originals.x + (stepped.x * iterator),
originals.y + (stepped.y * iterator),
originals.width + (stepped.width * iterator),
originals.height + (stepped.height * iterator)
);
// Run the callback as soon as possible, in sync with the last step
if(iterator == stepsNum && callback) {
callback(viewX, viewY, width, height);
}
}
}
// Schedule each animation step in to the future
// Todo: use some nice easing
for(i = 1; i <= stepsNum; ++i) {
setTimeout(timerFn(i), i * delay);
}
}
Run Code Online (Sandbox Code Playgroud)
Jam*_*mes 11
请参阅此JS Fiddle示例,了解如何使用jQuery和Raphael的setViewBox,它可以平滑地进行缩放和平移.我们在更复杂和更大的环境中使用完全相同的功能,即使屏幕上绘制了大量项目,它也能完美呈现并保持平滑.
基本上,不要试图重新发明轮子.我知道这可能不是你想要的答案,但它几乎肯定是你最好的选择.
编辑:我已修复附加小提琴(由于JSFiddle对网站所做的更改而被破坏)但显然SO不会让我保存JSFiddle链接而不包含一些代码,所以...
console.log("hello world!");
Run Code Online (Sandbox Code Playgroud)