<!DOCTYPE html>
<html lang="en">
<!--
dir 文字方向
rtl 右到左
ltr 左到右
比如:<html lang="en" dir="rtl">
-->
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>canvas文本属性-textAlign</title>
<style>
body{margin: 0;overflow: hidden}
#test{background: antiquewhite;}
</style>
</head>
<body>
<canvas id="test"></canvas>
<script>
const cvs=document.getElementById('test');
cvs.width=window.innerWidth;
cvs.height=window.innerHeight;
const ctx=cvs.getContext('2d');
/*辅助线*/
ctx.strokeStyle='#999';
ctx.setLineDash([8]);
ctx.moveTo(600,50);
ctx.lineTo(600,600);
ctx.stroke();
/*font 设置字号字体*/
ctx.font="24px serif";
/*
* textAlign 文字水平对齐方式
* start:基于文本起始位对齐,默认
* end:基于文本结束位对齐
* left:左对齐
* right:右对齐
* center:居中对齐
* */
ctx.textAlign='start';
ctx.fillText('start',600,100);
ctx.textAlign='left';
ctx.fillText('left',600,150);
ctx.textAlign='end';
ctx.fillText('end',600,200);
ctx.textAlign='right';
ctx.fillText('right',600,250);
ctx.textAlign='center';
ctx.fillText('center',600,300);
</script>
</body>
</html>