c7e05304cc2d95e50222a2f06d204af9a22c7b2d52103f03782281c7957972e42f2bb244f41a6f2aacfd5580507220494e7dc854391b10f7441ddbc91f8808 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. [中文](./README.md)
  2. <h1 align="center">Bezier Curve Extension</h1>
  3. <p align="center">
  4. <a href="https://travis-ci.com/jiaming743/bezierCurve"><img src="https://img.shields.io/travis/com/jiaming743/bezierCurve.svg" alt="Travis CI"></a>
  5. <a href="https://github.com/jiaming743/BezierCurve/blob/master/LICENSE"><img src="https://img.shields.io/github/license/jiaming743/bezierCurve.svg" alt="LICENSE" /> </a>
  6. <a href="https://www.npmjs.com/package/@jiaminghi/bezier-curve"><img src="https://img.shields.io/npm/v/@jiaminghi/bezier-curve.svg" alt="LICENSE" /> </a>
  7. </p>
  8. ### This plugin provides three extension methods for Bezier curves.
  9. - **[bezierCurveToPolyline](#bezierCurveToPolyline)**
  10. Ability to abstract a Bezier curve into a polyline consisting of N **uniformly distributed** points.
  11. - **[getBezierCurveLength](#getBezierCurveLength)**
  12. Get the length of bezier curve.
  13. - **[polylineToBezierCurve](#polylineToBezierCurve)**
  14. Abstracting a polyline consisting of N points into a Bezier curve.
  15. ### Install with npm
  16. ```shell
  17. $ npm install @jiaminghi/bezier-curve
  18. ```
  19. ### Use
  20. ```javascript
  21. import { bezierCurveToPolyline } from '@jiaminghi/bezier-curve'
  22. // do something
  23. ```
  24. ### Quick experience
  25. ```html
  26. <!--Resources are located on personal servers for experience and testing only, do not use in production environments-->
  27. <!--Debug version-->
  28. <script src="http://lib.jiaminghi.com/bezierCurve/bezierCurve.map.js"></script>
  29. <!--Compression version-->
  30. <script src="http://lib.jiaminghi.com/bezierCurve/bezierCurve.min.js"></script>
  31. <script>
  32. const { bezierCurveToPolyline, getBezierCurveLength, polylineToBezierCurve } = window.bezierCurve
  33. // do something
  34. </script>
  35. ```
  36. ------
  37. <h3 align="center">Examples</h3>
  38. #### bezierCurve
  39. ```javascript
  40. // Bezier curve data structure
  41. const bezierCurve = [
  42. // Start point
  43. [20, 20],
  44. // Multiple sets of bezier curve
  45. [
  46. // controlPoint1,controlPoint2,endPoint
  47. [100, 20],[100, 80],[180,80]
  48. ],
  49. // The starting point of the next bezier curve is the end point of the previous bezier curve
  50. // [...],[...]
  51. ]
  52. ```
  53. <p align="center">
  54. <img width="200px" src="./exampleImgs/bezierCurve.png" />
  55. </p>
  56. <p align="center"><i>bezierCurve</i> in <b>SVG</b></p>
  57. #### bezierCurveToPolyline
  58. ```javascript
  59. /**
  60. * @description Get the polyline corresponding to the Bezier curve
  61. * @param {Array} bezierCurve BezierCurve data
  62. * @param {Number} precision Calculation accuracy. Recommended for 5-10. Default = 5
  63. * @return {Array|Boolean} Point data that constitutes a polyline after calculation (Invalid input will return false)
  64. */
  65. function bezierCurveToPolyline (bezierCurve, precision = 5) {
  66. // ...
  67. }
  68. const precision = 5
  69. const polyline = bezierCurveToPolyline(bezierCurve, precision)
  70. // polyline = [
  71. // [[20,20],
  72. // [25.998752507628243,20.11632023466343],[31.698106846035834,20.457189096242345],
  73. // [37.11424670004552,21.010468821119716],[42.263355754480024,21.764021645678454],
  74. // ...]
  75. ```
  76. <p align="center">
  77. <img width="200px" src="./exampleImgs/bezierCurveToPolyline.png" />
  78. </p>
  79. <p align="center"><i>polyline</i> in <b>SVG</b></p>
  80. #### Notice
  81. - The calculation result of *bezierCurveToPolyline* consists of N points, and N depends on the precision you set.
  82. - Ideally, the distance between two adjacent points in the calculation result is equal to the set accuracy (unit px).
  83. - Recommended precision is 5-10.
  84. - If the setting precision is less than 1 or too large, the calculation result may be abnormal.
  85. - Sometimes it is **impossible** to achieve precision.
  86. #### getBezierCurveLength
  87. ```js
  88. /**
  89. * @description Get the bezier curve length
  90. * @param {Array} bezierCurve bezierCurve data
  91. * @param {Number} precision calculation accuracy. Recommended for 5-10. Default = 5
  92. * @return {Number|Boolean} BezierCurve length (Invalid input will return false)
  93. */
  94. export function getBezierCurveLength (bezierCurve, precision = 5) {
  95. // ...
  96. }
  97. // Normally the default precision can achieve better visual effects.
  98. const length = bezierCurveToPolyline(bezierCurve)
  99. ```
  100. #### polyline
  101. ```javascript
  102. // polyline data structure
  103. const polyline = [
  104. [20, 70],
  105. [50, 30],
  106. [100, 70],
  107. [150, 30],
  108. [180, 70]
  109. ]
  110. ```
  111. <p align="center">
  112. <img width="200px" src="./exampleImgs/polyline.png" />
  113. </p>
  114. <p align="center"><i>polyline</i> in <b>SVG</b></p>
  115. #### polylineToBezierCurve
  116. ```javascript
  117. /**
  118. * @description Abstract the polyline formed by N points into a set of bezier curve
  119. * @param {Array} polyline A set of points that make up a polyline
  120. * @param {Boolean} close Closed curve
  121. * @param {Number} offsetA Smoothness
  122. * @param {Number} offsetB Smoothness
  123. * @return {Array|Boolean} A set of bezier curve (Invalid input will return false)
  124. */
  125. function polylineToBezierCurve (polyline, close = false, offsetA = 0.25, offsetB = 0.25) {
  126. // ...
  127. }
  128. const bezierCurve = polylineToBezierCurve(polyline)
  129. // bezierCurve = [
  130. // [
  131. // [20,70],
  132. // [[27.5,60],[30,30],[50,30]],
  133. // [[70,30],[75,70],[100,70]],
  134. // [[125,70],[130,30],[150,30]],
  135. // [[170,30],[172.5,60],[180,70]]]
  136. //]
  137. const closedBezierCurve = polylineToBezierCurve(polyline, true)
  138. // closedBezerCurve = [
  139. // [20,70],
  140. // [[-12.5,60],[30,30],[50,30]],
  141. // [[70,30],[75,70],[100,70]],
  142. // [[125,70],[130,30],[150,30]],
  143. // [[170,30],[212.5,60],[180,70]],
  144. // [[147.5,80],[52.5,80],[20,70]]
  145. // ]
  146. ```
  147. <p align="center">
  148. <img width="200px" src="./exampleImgs/polylineToBezierCurve.png" />
  149. </p>
  150. <p align="center"><i>bezierCurve</i> in <b>SVG</b></p>
  151. <p align="center">
  152. <img width="200px" src="./exampleImgs/polylineToClosedBezierCurve.png" />
  153. </p>
  154. <p align="center"><i>closedBezierCurve</i> in <b>SVG</b></p>