开发者

HTML5 Canvas font size

开发者 https://www.devze.com 2023-03-11 01:15 出处:网络
When I translate and scale the canvas, the text put on the canvas corresponds to the canvas scale.How can I make it fixed size regardless of the scale of the canvas?

When I translate and scale the canvas, the text put on the canvas corresponds to the canvas scale. How can I make it fixed size regardless of the scale of the canvas?

Say I have a canvas that I used scale so even though the size is 640x480 pixels, the scaling i开发者_如何学Gos such that it is 8x5 canvas units. If I now use context.fillText, the text will be HUGE. I need the text to always be same visual size, say 12px.


you have to temporarily reset the scale (aka set the transformation matrix back to identity) before drawing your text.

// I have lots of transforms right now
ctx.save();
ctx.setTransform(1, 0, 0, 1, 0, 0);
// Will always draw text in normal scale
ctx.fillText("lalalala", x, y);
ctx.restore();
// Still have my old transforms
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号