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
| <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>三次贝塞尔曲线示例</title> <style> canvas { border: 1px solid black; } </style> </head> <body> <h1>三次贝塞尔曲线示例</h1> <canvas id="bezierCanvas" width="500" height="500"></canvas> <script> var canvas = document.getElementById('bezierCanvas'); var ctx = canvas.getContext('2d');
var startX = 50, startY = 400; var cp1X = 150, cp1Y = 50; var cp2X = 350, cp2Y = 50; var endX = 450, endY = 400;
ctx.beginPath();
ctx.moveTo(startX, startY);
ctx.bezierCurveTo(cp1X, cp1Y, cp2X, cp2Y, endX, endY);
ctx.lineWidth = 2; ctx.strokeStyle = 'blue'; ctx.stroke();
ctx.fillStyle = 'red'; ctx.beginPath(); ctx.arc(startX, startY, 5, 0, Math.PI * 2); ctx.fill();
ctx.beginPath(); ctx.arc(cp1X, cp1Y, 5, 0, Math.PI * 2); ctx.fill();
ctx.beginPath(); ctx.arc(cp2X, cp2Y, 5, 0, Math.PI * 2); ctx.fill();
ctx.beginPath(); ctx.arc(endX, endY, 5, 0, Math.PI * 2); ctx.fill(); </script> </body> </html>
|