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
| <!DOCTYPE html> <html>
<head> <title>绘制基本图形(文本水平对齐)</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,图形"> </head>
<body style="overflow: hidden; margin:10px;"> <canvas id="canvas" width="600" height="440" style="border:solid 1px #CCCCCC; box-shadow: 4px 4px 8px rgba(0, 0, 0, 0.5)" /> </body> <script> let canvas = document.getElementById('canvas'); let ctx = canvas.getContext('2d');
ctx.font = "24px Arial, sans-serif";
ctx.textAlign = "left"; ctx.fillText("文字的水平对齐(left)", 300, 60); ctx.textAlign = "center"; ctx.fillText("文字的水平对齐(center)", 300, 110); ctx.textAlign = "right"; ctx.fillText("文字的水平对齐(right)", 300, 160);
ctx.textAlign = "start"; ctx.fillText("文字的水平对齐(start)", 300, 230); ctx.textAlign = "end"; ctx.fillText("文字的水平对齐(end)", 300, 280);
ctx.textAlign = "start"; ctx.fillText("文字的水平对齐(start)", 300, 350); ctx.textAlign = "end"; ctx.fillText("文字的水平对齐(end)", 300, 400);
ctx.beginPath(); ctx.moveTo(300, 0); ctx.lineTo(300, canvas.height); ctx.moveTo(0, 180) ctx.lineTo(canvas.width, 180); ctx.moveTo(0, 300) ctx.lineTo(canvas.width, 300); ctx.strokeStyle = "green"; ctx.stroke();
ctx.font = "16px Arial, sans-serif"; ctx.textAlign = "left"; ctx.fillStyle = "red"; ctx.fillText("红点为坐标(x,y)位置", 5, 20); ctx.fillStyle = "blue";
ctx.beginPath(); ctx.arc(300, 60, 4, 0, 2 * Math.PI); ctx.arc(300, 110, 4, 0, 2 * Math.PI); ctx.arc(300, 160, 4, 0, 2 * Math.PI); ctx.arc(300, 210, 4, 0, 2 * Math.PI); ctx.arc(300, 260, 4, 0, 2 * Math.PI); ctx.arc(300, 350, 4, 0, 2 * Math.PI); ctx.arc(300, 400, 4, 0, 2 * Math.PI); ctx.fillStyle = "red"; ctx.fill(); </script>
</html>
|