This article mainly introduces the Bezier curve algorithm implemented in JavaScript, and analyzes the related implementation techniques of the Bezier curve algorithm based on JavaScript in the form of simple examples. Friends in need can refer to this article
The example describes the Bezier curve algorithm implemented in JavaScript. Share it with everyone for your reference, the details are as follows:
If you use a browser that supports HTML5, you can see the path line drawn with svg.
In all browsers, you can see a small box moving back and forth along the Bezier curve path.
Rendering:
Main code:
<p style="position:absolute;left:0;top:0;width:500px;height:300px;overflow:hidden;"> <svg id="root" width="500" height="300" viewBox="0 0 500 300" xmlns="http://www.w3.org/2000/svg"> <title>svg</title> <path d="M20,100 c80 -200 280 200 380 0 h-400" fill="none" stroke-width="1" stroke="gray" stroke-dasharray="3,3" /> </svg> </p> <p id="dotMove" style="position:absolute;width:6px;height:6px;overflow:hidden;background-color:#FF0000;"></p> <script type="text/javascript"> /* 参考维基百科 http://zh.wikipedia.org/wiki/%E8%B2%9D%E8%8C%B2%E6%9B%B2%E7%B7%9A */ function Point2D(x,y){ this.x=x||0.0; this.y=y||0.0; } /* cp在此是四個元素的陣列: cp[0]為起始點,或上圖中的P0 cp[1]為第一個控制點,或上圖中的P1 cp[2]為第二個控制點,或上圖中的P2 cp[3]為結束點,或上圖中的P3 t為參數值,0 <= t <= 1 */ function PointOnCubicBezier( cp, t ) { var ax, bx, cx; var ay, by, cy; var tSquared, tCubed; var result = new Point2D ; /*計算多項式係數*/ cx = 3.0 * (cp[1].x - cp[0].x); bx = 3.0 * (cp[2].x - cp[1].x) - cx; ax = cp[3].x - cp[0].x - cx - bx; cy = 3.0 * (cp[1].y - cp[0].y); by = 3.0 * (cp[2].y - cp[1].y) - cy; ay = cp[3].y - cp[0].y - cy - by; /*計算位於參數值t的曲線點*/ tSquared = t * t; tCubed = tSquared * t; result.x = (ax * tCubed) + (bx * tSquared) + (cx * t) + cp[0].x; result.y = (ay * tCubed) + (by * tSquared) + (cy * t) + cp[0].y; return result; } /* ComputeBezier以控制點cp所產生的曲線點,填入Point2D結構的陣列。 呼叫者必須分配足夠的記憶體以供輸出結果,其為<sizeof(Point2D) numberOfPoints> */ function ComputeBezier( cp, numberOfPoints, curve ) { var dt; var i; dt = 1.0 / ( numberOfPoints - 1 ); for( i = 0; i < numberOfPoints; i++) curve[i] = PointOnCubicBezier( cp, i*dt ); } var cp=[ new Point2D(20, 0), new Point2D(100, 200), new Point2D(300, -200), new Point2D(400, 0) ]; var numberOfPoints=100; var curve=[]; ComputeBezier( cp, numberOfPoints, curve ); var i=0, dot=document.getElementById("dotMove"); setInterval(function (){ var j = (i<100)?i:(199-i); dot.style.left=curve[j].x+'px'; dot.style.top=100-curve[j].y+'px'; if(++i==200)i=0; }, 50); </script>
The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.
Related articles:
Solution to using axios express local request 404 under Vue 2.5.2
Use vue and react to Achieve expansion, collapse and other effects
How to implement webpack packaging optimization in vue
The above is the detailed content of How to implement Bezier curve algorithm using JavaScript (detailed tutorial). For more information, please follow other related articles on the PHP Chinese website!