使用二次贝塞尔曲线画曲线
如图:
image.png

  1. <canvas class="canvas" width="500" height="500"
  2. style="width: 500px; height: 500px; border: 1ex solid black;"></canvas>
  3. <script>
  4. var canvas = document.getElementsByClassName('canvas')[0];
  5. var ctx = canvas.getContext('2d');
  6. // 二次贝塞尔曲线
  7. ctx.beginPath()
  8. ctx.moveTo(100,100)
  9. ctx.quadraticCurveTo(200,200,300,100)
  10. ctx.stroke()
  11. </script>

二次贝塞尔曲线
二次贝塞尔曲线.webp
使用三次贝塞曲线画曲线
image.png
代码如下

  1. <canvas class="canvas" width="500" height="500"
  2. style="width: 500px; height: 500px; border: 1ex solid black;"></canvas>
  3. <script>
  4. var canvas = document.getElementsByClassName('canvas')[0];
  5. var ctx = canvas.getContext('2d');
  6. ctx.beginPath()
  7. ctx.moveTo(100,100)
  8. ctx.bezierCurveTo(200,200,300,100,400,200)
  9. ctx.stroke()
  10. </script>

三次贝塞尔曲线
三次贝塞尔曲线.webp

利用二次贝塞尔曲线画波浪

代码如下

  1. <canvas class="canvas" width="500" height="500"
  2. style="width: 500px; height: 500px; border: 1ex solid black;"></canvas>
  3. <script>
  4. var canvas = document.getElementsByClassName('canvas')[0];
  5. var ctx = canvas.getContext('2d');
  6. ctx.beginPath()
  7. var offset = 0;
  8. var width = 500;
  9. var height = 500;
  10. var num = 0;
  11. ctx.moveTo(0 + offset, height / 2)
  12. setInterval(function () {
  13. ctx.clearRect(0, 0, 500, 500);
  14. ctx.beginPath();
  15. ctx.moveTo(0 + offset - 500, height / 2);
  16. ctx.quadraticCurveTo(width / 4 + offset - 500, height / 2 + Math.sin(num) * 120, width / 2 + offset - 500, height / 2);
  17. ctx.quadraticCurveTo(width / 4 * 3 + offset - 500, height / 2 - Math.sin(num) * 120, width + offset - 500, height / 2);
  18. ctx.moveTo(0 + offset, height / 2);
  19. ctx.quadraticCurveTo(width / 4 + offset, height / 2 + Math.sin(num) * 120, width / 2 + offset, height / 2);
  20. ctx.quadraticCurveTo(width / 4 * 3 + offset, height / 2 - Math.sin(num) * 120, width + offset, height / 2);
  21. ctx.stroke();
  22. offset += 1;
  23. offset %= 500
  24. num += 0.02;
  25. }, 1000 / 30)
  26. </script>

四阶贝塞尔曲线 s阶贝塞尔曲线
四次贝塞尔曲线.webps阶贝塞尔曲线.webp