gpt4 book ai didi

jquery - 拉斐尔纸缩放动画

转载 作者:行者123 更新时间:2023-12-03 22:19:37 28 4
gpt4 key购买 nike

我设法做了一些修改来缩放拉斐尔纸,因为 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);

是否可以对函数进行改造,使其平滑缩放?

最佳答案

对于任何想要平滑缩放和平移动画的人(像我一样),请看这里:

https://groups.google.com/forum/?fromgroups#!topic/raphaeljs/7eA9xq4enDo

这个片段帮助我自动缩放和平移到 Canvas 上的特定点并设置动画。 (支持Will Morgan)

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);
}

}

关于jquery - 拉斐尔纸缩放动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7736690/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com