javascript - 使用Snap动画化分组/分层的SVG

原文 标签 javascript jquery animation svg snap.svg

在天才@Sphinxxx的帮助下,我设法创建了一个由三个阶段组成的SVG路径动画。初始化,单击并悬停。变量在Snap.svg的帮助下巧妙地创建
有关有效的演示,请参见下面的Codepen:

http://codepen.io/Sphinxxxx/pen/EPzmgO

我想通过添加根据状态调整大小的线条来改进此动画。基本上应该是什么样子:

enter image description here enter image description here enter image description here

<path d="M33.1,18.2c1.1,3.9-4.3,8.6-10.6,9.3c-5.4,0.6-1.8,6.9-7.1,8.9c-6.7,2.6-9-0.7-8.5-6c0.5-4.6-9.6-6-3.5-12
C6.7,15.3,0.6,10.1,4.9,7c4-2.9,4,7.6,13-4.1c2.7-3.5,11.4-0.1,7.4,8.2C22.4,17.2,31.2,11.6,33.1,18.2z"/>

<line x1="1.9" y1="20.5" x2="33.1" y2="20.5"/>
<line x1="4" y1="13.5" x2="24.9" y2="13.5"/>
<line x1="5.9" y1="27.5" x2="22.5" y2="27.5"/>


如何将不同种类的SVG素材(路径和线条)组合在一起进行动画处理?任何帮助都非常有用!

最佳答案

pattern fill网站上查看我的演示,更多详细信息:



//SVG-snap
var none = "none",
    sausRed = "#ff1a29",
    strokeWidth = 3;

var initLangSvg = "M21.5,11.5c0,2.8-1.1,5.3-2.9,7.1c-1.8,1.8-4.3,2.9-7.1,2.9c-2.8,0-5.3-1.1-7.1-2.9c-1.8-1.8-2.9-4.3-2.9-7.1c0-2.8,1.1-5.3,2.9-7.1c1.8-1.8,4.3-2.9,7.1-2.9c2.8,0,5.3,1.1,7.1,2.9C20.4,6.2,21.5,8.7,21.5,11.5z",
    hoverLangSvg = "M21.5,13.5c0,2.8-2.1,3.8-4.6,5c-4.7,2.3-3.7,5.7-6.3,7s-8.4,0.8-9.1-3.9c-0.6-4.1,1.1-5.3,1.1-8.1c0-2.8-2.5-4.9-0.1-8.1c1.8-2.3,3.6-3.9,9.1-3.9c5.2,0,4.1,4.2,7,6C20.9,8.8,21.5,10.7,21.5,13.5z",
    clickLangSvg = "M33.1,18.2c1.1,3.9-4.3,8.6-10.6,9.3c-5.4,0.6-1.8,6.9-7.1,8.9c-6.7,2.6-9-0.7-8.5-6c0.5-4.6-9.6-6-3.5-12C6.7,15.3,0.6,10.1,4.9,7c4-2.9,4,7.6,13-4.1c2.7-3.5,11.4-0.1,7.4,8.2C22.4,17.2,31.2,11.6,33.1,18.2z";
/* Triangle -> circle -> square:
var initLangSvg = "M20,9 l11,19 h-22z",
    hoverLangSvg = "M20,10 a10,10 0 1,1 -.1,0z",
    clickLangSvg = "M29,11 v18 h-18 v-18z";
*/

//Adjust path positions (recalculate path points) before we use them:
//http://snapsvg.io/docs/#Snap.path.map
var matrix = new Snap.Matrix();
initLangSvg  = Snap.path.map(initLangSvg, matrix.translate(4,7));
hoverLangSvg = Snap.path.map(hoverLangSvg, matrix.translate(1,-1));

var chooseLang = Snap("#chooseLang svg").attr({viewBox:"0,0,40,40", preserveAspectRatio:"xMidYMin"}),
    boundingBox = chooseLang.rect(0, 0, 40, 40).attr({fill: none, stroke: none, strokeWidth: none}),
    outerCircle = chooseLang.path(initLangSvg).attr({fill: "url(#lines)", stroke: sausRed, strokeWidth: strokeWidth}),
    boundingBoxGroup = chooseLang.group(outerCircle, boundingBox);

function chooseLangInit() {
    outerCircle.attr({style: "fill: url(#lines); stroke-width: 3"})
    outerCircle.animate({d: initLangSvg}, 400);
}
function chooseLangHover() {
        outerCircle.attr({style: "fill: url(#lines2); stroke-width: 3"})
    outerCircle.animate({d: hoverLangSvg}, 400);
} 
function chooseLangClick() {
        outerCircle.attr({style: "fill: url(#lines3); stroke-width: 3"})
    outerCircle.animate({d: clickLangSvg}, 400);
} 


////
$(function() {
    $('#chooseLang').hover(chooseLangHover, chooseLangInit)
                    .click(chooseLangClick);
});

.hotCornersBtn, svg {
    display: block;
    width:  40px;
    height: 40px;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/snap.svg/0.4.1/snap.svg-min.js"> </script>

<a href="#" id="chooseLang" class="hotCornersBtn">
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
  <defs>
    <pattern id="lines" patternUnits="userSpaceOnUse" width="8" height="8">
      <image xlink:href="data:image/svg+xml;base64,PHN2ZyB4bWxucz0naHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmcnIHdpZHRoPSc4JyBoZWlnaHQ9JzgnPiAgPHJlY3Qgd2lkdGg9JzEwJyBoZWlnaHQ9JzEwJyBmaWxsPScjZmZmJyAvPiAgPHJlY3QgeD0nMCcgeT0nMCcgd2lkdGg9JzEwJyBoZWlnaHQ9JzInIGZpbGw9JyNDRjFENkQnIC8+ICA8cmVjdCB4PScwJyB5PSc0JyB3aWR0aD0nMTAnIGhlaWdodD0nMicgZmlsbD0nI0NGMUQ2RCcgLz48L3N2Zz4="
        x="0" y="0" width="8" height="8">
      </image>
      
      </pattern>
      <pattern id="lines2" patternUnits="userSpaceOnUse" width="10" height="10">
      <image xlink:href="data:image/svg+xml;base64,PHN2ZyB4bWxucz0naHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmcnIHdpZHRoPScxMCcgaGVpZ2h0PScxMCc+ICA8cmVjdCB3aWR0aD0nMTAnIGhlaWdodD0nMTAnIGZpbGw9JyNmZmYnIC8+ICA8cmVjdCB4PScwJyB5PScwJyB3aWR0aD0nMTAnIGhlaWdodD0nMicgZmlsbD0nI0NGMUQ2RCcgLz4gIDxyZWN0IHg9JzAnIHk9JzUnIHdpZHRoPScxMCcgaGVpZ2h0PScyJyBmaWxsPScjQ0YxRDZEJyAvPjwvc3ZnPg=="
        x="0" y="0" width="10" height="10">
      </image>
      
    </pattern>
      <pattern id="lines3" patternUnits="userSpaceOnUse" width="12" height="12">
      <image xlink:href="data:image/svg+xml;base64,PHN2ZyB4bWxucz0naHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmcnIHdpZHRoPScxMicgaGVpZ2h0PScxMic+ICA8cmVjdCB3aWR0aD0nMTInIGhlaWdodD0nMTInIGZpbGw9JyNmZmYnIC8+ICA8cmVjdCB4PScwJyB5PScwJyB3aWR0aD0nMTInIGhlaWdodD0nMicgZmlsbD0nI0NGMUQ2RCcgLz4gIDxyZWN0IHg9JzAnIHk9JzYnIHdpZHRoPScxMicgaGVpZ2h0PScyJyBmaWxsPScjQ0YxRDZEJyAvPjwvc3ZnPg=="
        x="0" y="0" width="12" height="12">
      </image>
      
    </pattern>
  </defs>
</svg
</a>

关于javascript - 使用Snap动画化分组/分层的SVG,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36919817/

相关文章:

javascript - 将多个音频单击JS功能组合在一起吗?

android - 在android中使用按钮加载动画

javascript - jquery中的选择器问题

javascript - 什么是 'true'和 'false'和 'null'?

javascript - 对日期格式为 'Year-Month-Day T Hour-Minute - Seconds Z' 的字符串数组进行排序的最简单方法

javascript - 单击<a>标签时, Bootstrap 模态无法正确显示

javascript - 在javascript中点击图片后更改图片(不使用下拉菜单)

javascript - 如何使用jQuery在悬停时临时放大字体大小

css - 从 0 不透明度交错淡入 div

ios - 如何在小游戏中正确复用 UIView?