使用javascript计算贝塞尔曲线的长度

发布于 2024-11-26 00:04:55 字数 59 浏览 4 评论 0原文

我使用 Keith Wood 的 jQuery SVG 插件沿着路径绘制文本。 JS如何获取路径的长度?

I draw a text along a path with Keith Wood's jQuery SVG plugin. How can I get a length of the path with JS?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

七颜 2024-12-03 00:04:55

根据 SVG 规范,使用 path.getTotalLength()

以下是如何将其与 jquery-svg 库一起使用的最小示例:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<title>jQuery SVG Basics</title>
<style type="text/css">
@import "jquery.svg.css";

#mydiv { width: 400px; height: 300px; border: 1px solid #484; }
</style>
<script type="text/javascript" src="jquery-1.6.2.js"></script>
<script type="text/javascript" src="jquery.svg.js"></script>
<script type="text/javascript">
$(document).ready(function(){
    $("#mydiv").svg({
        onLoad : function(svg){
            var path = svg.createPath(); 
            var pathNode = svg.path(path.move(100, 200).curveC([[200, 
                100, 300, 0, 400, 100], [500, 200, 600, 300, 700, 
                200], [800, 100, 900, 100, 900, 100]]));

            console.log(pathNode.getTotalLength());

        }
    });
});
</script>
</head>
<body>
<div id="mydiv"></div>
</body>
</html>

From the SVG specification, use path.getTotalLength().

Here's a minimal example of how to use this with the jquery-svg library:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<title>jQuery SVG Basics</title>
<style type="text/css">
@import "jquery.svg.css";

#mydiv { width: 400px; height: 300px; border: 1px solid #484; }
</style>
<script type="text/javascript" src="jquery-1.6.2.js"></script>
<script type="text/javascript" src="jquery.svg.js"></script>
<script type="text/javascript">
$(document).ready(function(){
    $("#mydiv").svg({
        onLoad : function(svg){
            var path = svg.createPath(); 
            var pathNode = svg.path(path.move(100, 200).curveC([[200, 
                100, 300, 0, 400, 100], [500, 200, 600, 300, 700, 
                200], [800, 100, 900, 100, 900, 100]]));

            console.log(pathNode.getTotalLength());

        }
    });
});
</script>
</head>
<body>
<div id="mydiv"></div>
</body>
</html>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文