[英]HTML5 canvas: Draw rectangle around text?
我希望能夠在文本標簽(人的全名)周圍繪制一個合適的矩形。 邊距並不是很重要,但是我不能只畫固定大小的矩形,因為標簽的長度是變化的。 我該怎么做呢?
這是繪制文本標簽的代碼:
var ctx = document.getElementById('map').getContext('2d');
for (i=0; i < num_people; i++) {
var pos = get_position(i, num_people);
ctx.fillText(names[i], pos.x, pos.y);
}
您可以使用measureText()獲得繪制文本的大小。
畫布文本的有用鏈接:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.