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
| <!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="./js/helper.js"></script> </head>
<body style="overflow: hidden; margin:10px;"> <canvas id="canvas" width="500" height="300" style="border:solid 1px #CCCCCC; box-shadow: 4px 4px 8px rgba(0, 0, 0, 0.5);"></canvas> </body> <script> let canvas = document.getElementById('canvas'); let ctx = canvas.getContext('2d'); drawGrid('lightgray', 10, 10);
ctx.save(); ctx.lineWidth = 4; ctx.beginPath(); ctx.moveTo(50, 150) ctx.quadraticCurveTo(150, 20, 250, 150) ctx.strokeStyle = "red"; ctx.stroke();
ctx.beginPath(); ctx.moveTo(250, 150) ctx.quadraticCurveTo(350, 280, 450, 150) ctx.strokeStyle = "blue"; ctx.stroke(); ctx.restore();
ctx.font = "16px 黑体" ctx.fillText("起点", 60, 150); ctx.fillText("终点", 260, 150); ctx.fillText("控制点", 160, 20);
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(150, 20, 8, 0, Math.PI * 2); ctx.moveTo(350, 280); ctx.arc(350, 280, 8, 0, Math.PI * 2); ctx.fillStyle = "#A2A2A2"; ctx.fill();
ctx.beginPath(); ctx.moveTo(50, 150); ctx.lineTo(150, 20); ctx.lineTo(250, 150); ctx.lineTo(350, 280); ctx.lineTo(450, 150); ctx.setLineDash([4, 4]); ctx.strokeStyle = "#A2A2A2"; ctx.stroke(); </script>
</html>
|