【发布时间】:2017-08-02 23:54:35
【问题描述】:
我开始学习 SVG,遇到了一个我想修改的代码笔 - 它最初有一个三角形,沿着动画 SVG 路径。我设法删除了中间循环,并用实际图像替换了三角形。但是,如果您看一下我的笔 (http://codepen.io/hybride/pen/pewzrO),您会发现图像实际上并没有平滑/正确地沿着动画路径移动。我该怎么做
- 获取路径上的图片,
- 让图像平面沿着那个方向的路径移动,而不是平面现在正在做的这种奇怪的曲线?
谢谢!
还提供了代码: HTML
<div id="svg-container">
<svg id="svgC" width="100%" height="100%" viewBox="0 0 820 220" preserveAspectRatio="xMidYMid meet"></svg>
CSS
#svg-container {
position:absolute;
z-index:99;
width: 90%;
margin: 0 auto;}
#svgC {padding: 20px;}
jQuery
// Pen JS Starts Here
jQuery(document).ready(function(){
// ---------
// SVG
var snapC = Snap("#svgC");
// SVG C - "Squiggly" Path
var myPathC = snapC.path("M62.9 14.9c-25-7.74-56.6 4.8-60.4 24.3-3.73 19.6 21.6 35 39.6 37.6 42.8 6.2 72.9-53.4 116-58.9 65-18.2 191 101 215 14.9c-25-7.74-56.6 14.9c-25-7.74-56.6 4.8-60.4").attr({
id: "squiggle",
fill: "none",
strokeWidth: "4",
stroke: "#ffffff",
strokeMiterLimit: "10",
strokeDasharray: "12 6",
strokeDashOffset: "180"
});
// SVG C - Triangle (As Polyline)
var Triangle = snapC.image("https://stage.livetext.com/wp-content/uploads/2017/02/Paper_Plane_Shadow.png", 0, 0, 150, 150);
initTriangle();
// Initialize Triangle on Path
function initTriangle(){
var triangleGroup = snapC.g( Triangle ); // Group polyline
movePoint = myPathC.getPointAtLength(length);
triangleGroup.transform( 't' + parseInt(movePoint.x - 15) + ',' + parseInt( movePoint.y - 15) + 'r' + (movePoint.alpha - 90));
}
// SVG C - Draw Path
var lenC = myPathC.getTotalLength();
// SVG C - Animate Path
function animateSVG() {
myPathC.attr({
stroke: '#fff',
strokeWidth: 4,
fill: 'none',
// Draw Path
"stroke-dasharray": "12 6",
"stroke-dashoffset": "180"
}).animate({"stroke-dashoffset": 10}, 4500,mina.easeinout);
var triangleGroup = snapC.g( Triangle ); // Group polyline
setTimeout( function() {
Snap.animate(0, lenC, function( value ) {
movePoint = myPathC.getPointAtLength( value );
triangleGroup.transform( 't' + parseInt(movePoint.x - 15) + ',' + parseInt( movePoint.y - 15) + 'r' + (movePoint.alpha - 90));
}, 4500,mina.easeinout, function(){
alertEnd();
});
});
}
// Animate Button
function kapow(){
$("#nin").click(function(event){
// Disable Button
$(this).attr('disabled','disabled');
// Animate Ball
if ($('#ball').hasClass('red')){
$('#ball').removeClass('red');
}
// Run SVG
animateSVG();
});
}
kapow();
});
【问题讨论】:
标签: javascript jquery css svg snap.svg