I need to use custom fonts and render them on off-screen canvas, I've done this already, but I cannot set the size of the font rendering -it results to a tiny rendering.
All sources I've seen including w3schools.com, developer.mozilla.org etc. naively assume that we all use system fonts and define the size in a sloppy way mixed with the font name as: ctx.font = "30px Arial";
T̶h̶e̶ ̶f̶a̶c̶t̶ ̶i̶s̶,̶ ̶t̶h̶a̶t̶ ̶i̶f̶ ̶I̶ ̶u̶s̶e̶ ̶t̶h̶e̶ ̶n̶a̶m̶e̶ ̶o̶f̶ ̶m̶y̶ ̶c̶u̶s̶t̶o̶m̶ ̶f̶o̶n̶t̶ ̶f̶i̶l̶e̶ ̶p̶l̶a̶c̶e̶d̶ ̶o̶n̶ ̶t̶h̶e̶ ̶s̶a̶m̶e̶ ̶f̶o̶l̶d̶e̶r̶ ̶(̶"̶c̶u̶s̶t̶o̶m̶.̶w̶o̶f̶f̶"̶)̶ ̶(̶o̶r̶ ̶.̶o̶t̶f̶)̶ ̶i̶t̶ ̶w̶o̶r̶k̶s̶,̶ ̶b̶u̶t̶ if I add the size in pixels or in points ("30px custom.woff") that does NOT work!
Frustratingly enough, other parameters such as font weight seem to be defined separately (talk about unbelievable HTML5 inconsistency that reminds us the past). So, is there a way to change the rendering size of a custom font for an off-screen canvas created in JS?
I create the off-screen canvas like this:
var offScreenCanvas = document.createElement('canvas');
offScreenCanvas.width = 256;
offScreenCanvas.height = 256;
var ctx = offScreenCanvas.getContext("2d");
later, I render the font like this:
ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);
ctx.font = 'custom.woff';
ctx.fillStyle = 'white';
ctx.fillRect(0, 0, offScreenCanvas.width, 256);
ctx.fillStyle = 'black';
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillText(txt, offScreenCanvas.width / 2, offScreenCanvas.height / 2);
Then I make a texture of that rendering and use it on a three.js surface.
three.js r96.0