html canvas写字并右对齐

function PaintText(x, y, color, size, text, toLeft) {

if (toLeft == undefined) {
toLeft = true;
}

y += size;
ctx.font = "normal " + size+"px arial";
ctx.fillStyle = color;
if (toLeft) {
ctx.fillText(text, x, y);
}
else {

//使用ctx.measureText计算文本宽度,右对齐
ctx.fillText(text, ActualWidth-ctx.measureText(text).width, y);

}
}

posted @ 2023-06-15 16:29  奇迹之耀  阅读(217)  评论(0编辑  收藏  举报