简体   繁体   中英

Length of Bezier curve with javascript

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?

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM