1

I'm working on an variable type test for a project with a variable font i created.

The 'wght' axis contains a range of between 0-6600.

I had no problems animating the full range of values above as a DOM element with HTML and CSS.

But while trying out the same thing on HTML canvas, i realised the font weight resets itself to 0 once its passes 1000.

Is there a way to get around this? I tried looking into P5.js but the examples i found still manipulates DOM elements instead of using a canvas.

An excerpt of my code below, but i cant quite share the working demo with the font as the font is meant to be a proprietary font eventually.

@font-face {
   font-family: 'TestFont';
   src: url('/assets/TestFont_v2.ttf') format('truetype-variations');
   font-weight: 0 6600;
}




<canvas id="canvas-text" width="1000" height="500"> </canvas>



/*** js***/

var c = document.getElementById('canvas-text');
var ctx = c.getContext('2d');
function drawCanvas() {

    ctx.fillStyle = 'black';
    ctx.fillRect(0, 0, c.width, c.height);

    ctx.fillStyle = 'white';
    ctx.font = (scroll_percentage * 6600) + ' '  + '20px TestFont';
    const string_ = 'Lorem ipsum';
    ctx.fillText(string_, 20, 20);

}

let scroll_percentage = 0
function updateScrollProgress() {
    // get percentage of total scroll 
    var winScroll = document.body.scrollTop || document.documentElement.scrollTop;
    var height = document.documentElement.scrollHeight - document.documentElement.clientHeight;
    scroll_percentage = winScroll / height;

    // update canvas on scroll
    drawCanvas() 

}
wsgg
  • 934
  • 1
  • 20
  • 43
  • 1
    *"I had no problems animating the full range of values above as a DOM element with HTML and CSS."* Really? I can't set `font-weight` above `1000` even through "just" CSS, and actually [the specs](https://w3c.github.io/csswg-drafts/css-fonts/#valdef-font-weight-number-1-1000) expect a number between 1 and 1000. Could you show how you managed to do that? I guess you made use of the `font-variation-settings: 'wght'` property instead, right? Unfortunately we still can't play with that in a canvas... – Kaiido Jan 27 '23 at 05:35
  • Ah i see . Yes i was referring to `font-variation-settings: 'wght'`. – wsgg Jan 27 '23 at 05:43

0 Answers0