Ví dụ về thuộc tính textAlign Code: <!DOCTYPE html> <html> <body> <canvas id="myCanvas" width="300" height="150" style="border:1px solid #d3d3d3;">Trình duyệt của bạn không hỗ trợ thẻ canvas trong HTML5.</canvas> <script> var c = document.getElementById("myCanvas"); var ctx = c.getContext("2d"); // Tạo một đường kẻ đỏ ở vị trí 150 ctx.strokeStyle = "red"; ctx.moveTo(150, 20); ctx.lineTo(150, 170); ctx.stroke(); ctx.font = "15px Arial"; // Hiển thị những giá trị textAlign khác nhau ctx.textAlign = "start"; ctx.fillText("textAlign=start", 150, 60); ctx.textAlign = "end"; ctx.fillText("textAlign=end", 150, 80); ctx.textAlign = "left"; ctx.fillText("textAlign=left", 150, 100); ctx.textAlign = "center"; ctx.fillText("textAlign=center",150, 120); ctx.textAlign = "right"; ctx.fillText("textAlign=right",150, 140); </script> </body> </html> Kết quả: