1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
   | <!DOCTYPE html> <html>
  <head>     <title>绘制曲线和路径(quadraticCurveTo)</title>     <meta charset="UTF-8">     <meta name="viewport" content="width=device-width, initial-scale=1">     <meta name="description" content="图形系统开发实战:基础篇 示例">     <meta name="author" content="hjq">     <meta name="keywords" content="canvas,ladder,javascript,图形">     <script src="/examples/canvas-qa/canvas_1a/js/helper.js"></script> </head>
  <body style="overflow: hidden; margin:10px;">     <canvas id="canvas" width="500" height="300" style="border:solid 1px #CCCCCC;"></canvas> </body> <script>          let canvas = document.getElementById('canvas');          let ctx = canvas.getContext('2d');
      let minX = 50;     let maxX = 450;     let x = minX;     let toLeft = false;
      function loopDraw() {         if(x > maxX || x < minX) toLeft = ! toLeft;         if(toLeft){             x -= 2;         } else {             x += 2;         }         draw({ x: x, y: 20 }, { x: (maxX - x + 50), y: 280 })     }
      window.requestAnimationFrame(loopDraw);
      function draw(ctrlPoint1, ctrlPoint2) {
          ctx.clearRect(0, 0, canvas.width, canvas.height);
                   drawGrid('lightgray', 10, 10);
          ctx.save();         ctx.lineWidth = 4;
          ctx.beginPath();                  ctx.moveTo(50, 150)         ctx.quadraticCurveTo(ctrlPoint1.x, ctrlPoint1.y, 250, 150)         ctx.strokeStyle = "red";         ctx.stroke();
                   ctx.beginPath();         ctx.moveTo(250, 150)         ctx.quadraticCurveTo(ctrlPoint2.x, ctrlPoint2.y, 450, 150)         ctx.strokeStyle = "blue";         ctx.stroke();         ctx.restore();
                   ctx.save();                 ctx.beginPath();         ctx.arc(50, 150, 8, 0, Math.PI * 2);         ctx.moveTo(250, 150);         ctx.arc(250, 150, 8, 0, Math.PI * 2);         ctx.moveTo(450, 150);         ctx.arc(450, 150, 8, 0, Math.PI * 2);         ctx.fillStyle = "#5C35FF";         ctx.fill();
                   ctx.beginPath();         ctx.arc(ctrlPoint1.x, ctrlPoint1.y, 8, 0, Math.PI * 2);         ctx.moveTo(350, 280);         ctx.arc(ctrlPoint2.x, ctrlPoint2.y, 8, 0, Math.PI * 2);         ctx.fillStyle = "#A2A2A2";         ctx.fill();
                   ctx.beginPath();         ctx.moveTo(50, 150);         ctx.lineTo(ctrlPoint1.x, ctrlPoint1.y);         ctx.lineTo(250, 150);         ctx.lineTo(ctrlPoint2.x, ctrlPoint2.y);         ctx.lineTo(450, 150);         ctx.setLineDash([4, 4]);         ctx.strokeStyle = "#A2A2A2";         ctx.stroke();         ctx.restore();
                   window.requestAnimationFrame(loopDraw);     }     </script>
  </html>
   |