我目前正在使用velocity.js来控制我正在创建的SVG绘图上的一些动画。它是与用户交互的,所以当某些部分发生单击时,图片可能会向右或向下生长。到目前为止,一切都很完美,直到图片变得对SVG盒来说太大了。
在这种情况下,我只需调整viewBox的大小来缩放图像。
svgDoc.setAttribute("viewBox", "0 0 " + svgDocWidth + " " + svgDocHeight);这是可行的,但它看起来不太好,因为它不是动画。它只是跳到了新的尺寸。有什么方法可以用velocity.js动画化viewBox更改吗?
我尝试过这种方法:
$viewBox = $(svgDoc.viewBox);
$viewBox.velocity({height: svgDocHeight, width: svgDocWidth});但这没什么用。
这是否超出了velocity.js所能支持的范围?
2015年的解决方案-11-21
@Ian给出了我最终使用的解决方案。结果看起来是这样的:
var origHeight = this.svgDoc.viewBox.animVal.height;
var origWidth = this.svgDoc.viewBox.animVal.width;
var docHeight = this.SvgHeight();
var docWidth = this.SvgWidth();
if ((origHeight != docHeight) || (origWidth != docWidth))
{
var deltaHeight = docHeight - origHeight;
var deltaWidth = docWidth - origWidth;
$(this.svgDoc).velocity(
{
tween: 1
},
{
progress: function(elements, complete, remaining, start, tweenValue)
{
var newWidth = origWidth + complete * deltaWidth;
var newHeight = origHeight + complete * deltaHeight;
elements[0].setAttribute('viewBox', '0 0 ' + newWidth + ' ' + newHeight);
}
});
} 发布于 2015-11-20 10:39:18
我认为SMIL在Chrome中受到反对(如果我错了,请纠正我),所以我猜其他方法的依赖程度会越来越高。
你没有理由不能使用速度,但是用你自己的计算和进度/tweenValue参数(如有点像这样).
$("#mysvg").velocity(
{ tween: 200 },
{ progress: animViewbox }
)
function animViewbox (elements, complete, remaining, start, tweenValue) {
elements[0].setAttribute('viewBox', '0 0 ' + tweenValue + ' ' + tweenValue);
}发布于 2015-11-20 07:57:56
您可以通过SMIL平稳地动画一个viewBox。像这样..。
<svg width="100%" height="100%" viewBox="0 0 200 200">
<animate attributeName="viewBox" to="0 0 400 400" dur="5s" fill="freeze" />
<circle cx="100" cy="100" r="100" fill="green" />
</svg>
https://stackoverflow.com/questions/33820012
复制相似问题