当前位置:   article > 正文

赛贝尔曲线工具

赛贝尔曲线工具
 

因为需要使用到赛贝尔曲线的相关工具。去做些特效或者路径什么的 然后发现了一个工具同不错的 就是赛贝尔曲线工具 又因为感觉这个不错。于是我就....把他扒下来了。你们可以自行修改这个的相关参数调解canvas的宽与高活再右边添加不同选项。下载一个webStorm 那下面的复制粘贴一下 你就可以使用了 下面是代码

此代码进过了自己的 一些修改添加的。里面又有赛贝尔函数,也有使用说明 。大家只需要下载一个webStorm把这个页面。然后创建一个新的空工程,再创建一个html的页面。然后把下面的全部复制。放到刚刚创建好的html的页面上就可以了。具体如何创建可以webStrom的可以百度一下

因为是在之前的作者基础之上修改过了 翻译了一下。同时添加了原网址,与使用方法的介绍。与赛贝尔函数

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Title</title>
  6. <style>
  7. /* CSS */
  8. body
  9. {
  10. font-family: arial, helvetica, sans-serif;
  11. font-size: 85%;
  12. margin: 10px 15px;
  13. color: #333;
  14. background-color: #ddd;
  15. }
  16. h1
  17. {
  18. font-size: 1.6em;
  19. font-weight: normal;
  20. margin: 0 0 0.3em 0;
  21. }
  22. h2
  23. {
  24. font-size: 1.4em;
  25. font-weight: normal;
  26. margin: 1.5em 0 0 0;
  27. }
  28. p
  29. {
  30. margin: 1em 0;
  31. }
  32. #canvas
  33. {
  34. display: inline;
  35. float: left;
  36. margin: 0 10px 10px 0;
  37. background-color: #fff;
  38. /*width: 750px;*/
  39. /*height: 1206px;*/
  40. }
  41. #code
  42. {
  43. display: block;
  44. width: auto;
  45. height: 100%;
  46. font-family: monospace;
  47. font-size: 1em;
  48. padding: 2px 4px;
  49. margin: 0;
  50. color: #555;
  51. background-color: #eee;
  52. border: 1px solid #999;
  53. overflow: auto;
  54. }
  55. </style>
  56. </head>
  57. <body>
  58. <h1>赛贝尔曲线</h1>
  59. <canvas id="canvas" width="750" height="1206" class="bezier"></canvas>
  60. <pre id="code">code</pre>
  61. <p>他演示展示了如何在画布元素上绘制B_zier曲线。拖动线条端点或控制点以更改曲线。</p>
  62. <p>更多的内容请前往:<br />
  63. <a href="http://blogs.sitepoint.com/html5-canvas-draw-bezier-curves/">如何使用塞北贝尔函数在canvas画出图形</a></p>
  64. <p>又或者是这个:<br />
  65. <a href="http://blogs.sitepoint.com/html5-canvas-draw-quadratic-curves/">如何使用塞北贝尔函数在canvas画出图形</a></p>
  66. <h2>Disclaimer (免责声明)</h2>
  67. <p>代码是由 <a href="http://twitter.com/craigbuckler">Craig Buckler</a> of <a href="http://optimalworks.net/">OptimalWorks.net</a> for <a href="http://sitepoint.com/">SitePoint.com</a>.</p>
  68. <p>代码可以在没有任何限制的情况下使用,但请不要期望24小时支持!请链接至sitepoint.com.</p>
  69. <p>!!我是之前抓下来的网址 如果你认为这个页面很好使用,这边是之前官方版本的网页 <a href="http://blogs.sitepointstatic.com/examples/tech/canvas-curves/bezier-curve.html">请点击这里</a></a>跳转到原创作者的网页.</p>
  70. <script>
  71. /*
  72. * Canvas curves example
  73. *
  74. * By Craig Buckler, http://twitter.com/craigbuckler
  75. * of OptimalWorks.net http://optimalworks.net/
  76. * for SitePoint.com http://sitepoint.com/
  77. *
  78. * Refer to:
  79. * http://blogs.sitepoint.com/html5-canvas-draw-quadratic-curves/
  80. * http://blogs.sitepoint.com/html5-canvas-draw-bezier-curves/
  81. *
  82. * This code can be used without restriction.
  83. */
  84. (function() {
  85. var canvas, ctx, code, point, style, drag = null, dPoint;
  86. // define initial points
  87. function Init(quadratic) {
  88. point = {
  89. p1: { x:100, y:250 },
  90. p2: { x:400, y:250 }
  91. };
  92. if (quadratic) {
  93. point.cp1 = { x: 250, y: 100 };
  94. }
  95. else {
  96. point.cp1 = { x: 150, y: 100 };
  97. point.cp2 = { x: 350, y: 100 };
  98. }
  99. // default styles
  100. style = {
  101. curve: { width: 6, color: "#333" },
  102. cpline: { width: 1, color: "#C00" },
  103. point: { radius: 10, width: 2, color: "#900", fill: "rgba(200,200,200,0.5)", arc1: 0, arc2: 2 * Math.PI }
  104. }
  105. // line style defaults
  106. ctx.lineCap = "round";
  107. ctx.lineJoin = "round";
  108. // event handlers
  109. canvas.onmousedown = DragStart;
  110. canvas.onmousemove = Dragging;
  111. canvas.onmouseup = canvas.onmouseout = DragEnd;
  112. DrawCanvas();
  113. }
  114. // draw canvas
  115. function DrawCanvas() {
  116. ctx.clearRect(0, 0, canvas.width, canvas.height);
  117. // control lines
  118. ctx.lineWidth = style.cpline.width;
  119. ctx.strokeStyle = style.cpline.color;
  120. ctx.beginPath();
  121. ctx.moveTo(point.p1.x, point.p1.y);
  122. ctx.lineTo(point.cp1.x, point.cp1.y);
  123. if (point.cp2) {
  124. ctx.moveTo(point.p2.x, point.p2.y);
  125. ctx.lineTo(point.cp2.x, point.cp2.y);
  126. }
  127. else {
  128. ctx.lineTo(point.p2.x, point.p2.y);
  129. }
  130. ctx.stroke();
  131. // curve
  132. ctx.lineWidth = style.curve.width;
  133. ctx.strokeStyle = style.curve.color;
  134. ctx.beginPath();
  135. ctx.moveTo(point.p1.x, point.p1.y);
  136. if (point.cp2) {
  137. ctx.bezierCurveTo(point.cp1.x, point.cp1.y, point.cp2.x, point.cp2.y, point.p2.x, point.p2.y);
  138. }
  139. else {
  140. ctx.quadraticCurveTo(point.cp1.x, point.cp1.y, point.p2.x, point.p2.y);
  141. }
  142. ctx.stroke();
  143. // control points
  144. for (var p in point) {
  145. ctx.lineWidth = style.point.width;
  146. ctx.strokeStyle = style.point.color;
  147. ctx.fillStyle = style.point.fill;
  148. ctx.beginPath();
  149. ctx.arc(point[p].x, point[p].y, style.point.radius, style.point.arc1, style.point.arc2, true);
  150. ctx.fill();
  151. ctx.stroke();
  152. }
  153. ShowCode();
  154. }
  155. // show canvas code
  156. function ShowCode() {
  157. if (code) {
  158. code.firstChild.nodeValue =
  159. "他演示展示了如何在画布元素上绘制B_zier曲线。拖动线条端点或控制点以更改曲线。\n"+
  160. "canvas = document.getElementById(\"canvas\");\n"+
  161. "ctx = canvas.getContext(\"2d\")\n"+
  162. "ctx.lineWidth = " + style.curve.width +
  163. ";\nctx.strokeStyle = \"" + style.curve.color +
  164. "\";\nctx.beginPath();\n" +
  165. "ctx.moveTo(" + point.p1.x + ", " + point.p1.y +");\n" +
  166. (point.cp2 ?
  167. "ctx.bezierCurveTo("+point.cp1.x+", "+point.cp1.y+", "+point.cp2.x+", "+point.cp2.y+", "+point.p2.x+", "+point.p2.y+");" :
  168. "ctx.quadraticCurveTo("+point.cp1.x+", "+point.cp1.y+", "+point.p2.x+", "+point.p2.y+");"
  169. ) +
  170. "ctx.stroke();"+
  171. "\n"+
  172. "\n起始点坐标:(" + point.p1.x + ", " + point.p1.y +")"+
  173. "\n起始点的参数坐标("+point.cp1.x +","+point.cp1.y +")"+
  174. "\n终点的参数坐标("+point.cp2.x +","+point.cp2.y +")"+
  175. "\n终点的坐标("+point.p2.x +","+point.p2.y +")"+
  176. "\n"+
  177. "\n三次赛贝尔的公式 传入的是anchorpoints=([{起始点坐标},{起始点的参数坐标x},{终点的参数坐标},{终点的坐标}])"+
  178. "\n三次赛贝尔的公式 格式是bser([{x,y},{x,y},{x,y},{x,y}],speed)"+
  179. "\n 传入的是anchorpoints(第一个数组是起始点的xy也就是ctx.moveTo后面的数组,第二个数组就是第四个参数是ctx.bezierCurveTo中的数组参数,最后一个单独的参数是速度是多少)"+
  180. "\n下面的是三次赛贝尔的公式 格式是bser([{" + point.p1.x + "," + point.p1.y +"},{"+point.cp1.x +","+point.cp1.y +"},{"+point.cp2.x +","+point.cp2.y +"},{"+point.p2.x +","+point.p2.y +"}],100)"+
  181. "\n static bser(anchorpoints, pointsAmount) {\n" +
  182. " let points = [];\n" +
  183. " for (let i = 0; i < pointsAmount; i++) {\n" +
  184. " let point = MultiPointBezier(anchorpoints, i / pointsAmount);\n" +
  185. " points.push(point);\n" +
  186. " }\n" +
  187. " return points;\n" +
  188. " function MultiPointBezier(points_, t) {//t贞数\n" +
  189. " let len = points_.length;\n" +
  190. " let x = 0, y = 0;\n" +
  191. " let erxiangshi = function (start, end) {\n" +
  192. " let cs = 1, bcs = 1;\n" +
  193. " while (end > 0) {\n" +
  194. " cs *= start;\n" +
  195. " bcs *= end;\n" +
  196. " start--;\n" +
  197. " end--;\n" +
  198. " }\n" +
  199. " return (cs / bcs);\n" +
  200. " };\n" +
  201. " for (let i = 0; i < len; i++) {\n" +
  202. " let point = points_[i];\n" +
  203. " x +=/*Math.round()*/ point.x * Math.pow((1 - t), (len - 1 - i)) * Math.pow(t, i) * (erxiangshi(len - 1, i));//x弧度\n" +
  204. " y +=/*Math.round()*/ point.y * Math.pow((1 - t), (len - 1 - i)) * Math.pow(t, i) * (erxiangshi(len - 1, i));//y弧度\n" +
  205. " }\n" +
  206. " return { e: x, f: y };\n" +
  207. " }\n" +
  208. " }"
  209. ;
  210. }
  211. }
  212. // start dragging
  213. function DragStart(e) {
  214. e = MousePos(e);
  215. var dx, dy;
  216. for (var p in point) {
  217. dx = point[p].x - e.x;
  218. dy = point[p].y - e.y;
  219. if ((dx * dx) + (dy * dy) < style.point.radius * style.point.radius) {
  220. drag = p;
  221. dPoint = e;
  222. canvas.style.cursor = "move";
  223. return;
  224. }
  225. }
  226. }
  227. // dragging
  228. function Dragging(e) {
  229. if (drag) {
  230. e = MousePos(e);
  231. point[drag].x += e.x - dPoint.x;
  232. point[drag].y += e.y - dPoint.y;
  233. dPoint = e;
  234. DrawCanvas();
  235. }
  236. }
  237. // end dragging
  238. function DragEnd(e) {
  239. drag = null;
  240. canvas.style.cursor = "default";
  241. DrawCanvas();
  242. }
  243. // event parser
  244. function MousePos(event) {
  245. event = (event ? event : window.event);
  246. return {
  247. x: event.pageX - canvas.offsetLeft,
  248. y: event.pageY - canvas.offsetTop
  249. }
  250. }
  251. // start
  252. canvas = document.getElementById("canvas");
  253. code = document.getElementById("code");
  254. if (canvas.getContext) {
  255. ctx = canvas.getContext("2d");
  256. Init(canvas.className == "quadratic");
  257. }
  258. })();
  259. // static bser(anchorpoints, pointsAmount) {
  260. // let points = [];
  261. // for (let i = 0; i < pointsAmount; i++) {
  262. // let point = MultiPointBezier(anchorpoints, i / pointsAmount);
  263. // points.push(point);
  264. // }
  265. // return points;
  266. // function MultiPointBezier(points_, t) {//t贞数
  267. // let len = points_.length;
  268. // let x = 0, y = 0;
  269. // let erxiangshi = function (start, end) {
  270. // let cs = 1, bcs = 1;
  271. // while (end > 0) {
  272. // cs *= start;
  273. // bcs *= end;
  274. // start--;
  275. // end--;
  276. // }
  277. // return (cs / bcs);
  278. // };
  279. // for (let i = 0; i < len; i++) {
  280. // let point = points_[i];
  281. // x +=/*Math.round()*/ point.x * Math.pow((1 - t), (len - 1 - i)) * Math.pow(t, i) * (erxiangshi(len - 1, i));//x弧度
  282. // y +=/*Math.round()*/ point.y * Math.pow((1 - t), (len - 1 - i)) * Math.pow(t, i) * (erxiangshi(len - 1, i));//y弧度
  283. // }
  284. // return { e: x, f: y };
  285. // }
  286. // }
  287. </script>
  288. </body>
  289. </html>

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/581009
推荐阅读
相关标签
  

闽ICP备14008679号