html - 通过 SVG 路径为 "div"设置动画

标签 html svg jquery-svg svg-animate

我是 HTML5、SVG 的菜鸟。基本上我需要通过我创建的 SVG 路径移动一个 html“div”对象。

我的 HTML 看起来像这样

<div class="movepath">

        <div class="car" id="car"></div>


<svg id="canvas" width="1000px" class="content" xmlns="http://www.w3.org/2000/svg" height="370px">
<style type="text/css"><![CDATA[
    .lineC {
        fill:#fff;stroke:#000;
    }
]]></style>


</svg>

    </div>

CSS

.movepath {
width:"1000px";
height:350px;
position:relative;
float:left;
}

.car {
width:100px;
height:50px;
background-color:red;
position:absolute;
left:0;top:0;
}

js

var width=getDocWidth();
    $('#canvas').width(width+'px');
    $('.movepath').width(width+'px');



    var shape = document.createElementNS(svgNS, "path");

    var points = 'M0 10 Q -27 10, 95 80 T'+width+' 40';
    shape.setAttributeNS(null, "d", points);
    shape.setAttributeNS(null, "class", "lineC");
    shape.setAttributeNS(null, "id", 'road');
    document.getElementById("canvas").appendChild(shape);

创建了这样的路径。我完全不知道如何通过创建的路径移动 $('#car') ?

请多多指教

最佳答案

基本上你需要一个循环(一个以相等的时间步长触发的函数,所以没有 »for« 或 »while« 循环),其中你需要从路径中获取一个点,如:

length = path.getTotalLength() * i;
point = path.getPointAtLength( length );

比:

div.style.left = point.x + 'px';
div.style.top = point.y + 'px';

其中 i 是一个介于 0 和 1 之间的值,表示动画的进度。总而言之,还有更多需要了解的内容,并且有不同的方法来移动 div(即 css 转换)或获取值,但基本上就是这样。

关于html - 通过 SVG 路径为 "div"设置动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14826305/

相关文章:

json - 如何将 svg 文件从 inkscape 转换为 geoJson 格式

svg - 是否可以在 svg 矩形中设置负宽度?

javascript - 在 SVG 中弯曲图像

javascript - 如何使用 Javascript 和 Jquery 分别验证通过 PHP 循环创建的每个简单的 HTML 单选表单

javascript - iOS 不在 <img> 标签中显示 SVG

html - 尝试将容器用于图像但想根据屏幕分辨率调整图像大小

javascript - 从 highcharts 中的 svg 中删除内联样式

html - 如何在编码像素完美设计时处理行高

javascript - 在 Canvas 折线图中显示负值