Use Javascript to get Maximum font size in canvas

后端 未结 2 1860
滥情空心
滥情空心 2020-11-27 22:53

I am drawing a canvas that needs to be on full available screen (100% width and height). I set the width and height of canvas using javascript like this

  va         


        
2条回答
  •  温柔的废话
    2020-11-27 23:22

    Try this.

    FIDDLE DEMO

    var canvas = document.getElementById('canvas');
    var context = canvas.getContext('2d');
    var x = canvas.width / 2;
    var y = canvas.height / 2 - 10;
    var text = 'I M @ CENTER!';
    context.font = '10pt Calibri'; //try changing values 10,15,20
    context.textAlign = 'center';
    context.fillStyle = 'red';
    context.fillText(text, x, y);
    

    You can provide your conditions depending on the window size.Create a simple function like following:

    function Manipulate_FONT(window.widht, window.height) {
        if (window.widht == something && window.height == something) {
            font_size = xyz //put this in ---> context.font = '10pt Calibri'
        }
    }
    

提交回复
热议问题