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
| <!DOCTYPE html> <html lang="zh_CN">
<head> <title>绘制曲线和路径(bezierCurveTo)</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,anygraph,javascript,图形"> <script src="../js/helper.js"></script> </head>
<body style="margin:10px;"> <canvas id="canvas" width="550" height="300" style="border:solid 1px #CCCCCC;"></canvas> </body> <script> let canvas = document.getElementById('canvas'); let ctx = canvas.getContext('2d');
function drawBackground() {
ctx.beginPath(); ctx.moveTo(30, 260); ctx.bezierCurveTo(90, 260, 60, 60, 160, 60); ctx.bezierCurveTo(230, 60, 220, 260, 280, 260); ctx.bezierCurveTo(340, 260, 310, 60, 400, 60); ctx.bezierCurveTo(480, 60, 470, 260, 520, 260); ctx.lineWidth = 4; ctx.strokeStyle = "red"; ctx.stroke(); } let idx = 0;
function draw() { ctx.lineWidth = 4; ctx.strokeStyle = "blue"; if (idx == 0) { ctx.clearRect(0, 0, canvas.width, canvas.height); drawBackground(); } else if (idx == 1) { ctx.beginPath(); ctx.arc(30, 260, 6, 0, 2 * Math.PI, false); ctx.fillStyle = "blue"; ctx.fill(); } else if (idx == 2) { ctx.beginPath(); ctx.moveTo(30, 260); ctx.bezierCurveTo(90, 260, 60, 60, 160, 60); ctx.stroke(); } else if (idx == 3) { ctx.beginPath(); ctx.moveTo(160, 60); ctx.bezierCurveTo(230, 60, 220, 260, 280, 260); ctx.stroke(); } else if (idx == 4) { ctx.beginPath(); ctx.moveTo(280, 260); ctx.bezierCurveTo(340, 260, 310, 60, 400, 60); ctx.stroke(); } else if (idx == 5) { ctx.beginPath(); ctx.moveTo(400, 60); ctx.bezierCurveTo(480, 60, 470, 260, 520, 260); ctx.stroke(); }
idx = idx < 5 ? idx + 1 : 0; window.setTimeout(draw, 2000); }
draw(); </script>
</html>
|