Adding Letter Spacing in HTML Canvas

前端 未结 2 904
我在风中等你
我在风中等你 2021-01-06 06:10

I\'ve read a lot of StackOverflow answers and other pages talking about how to do letter spacing in Canvas. One of the more useful ones was Letter spacing in canvas element<

2条回答
  •  南笙
    南笙 (楼主)
    2021-01-06 06:35

    My answer got deleted. So, I'm using chrome and here is my complete code.

    second_image = $('#block_id').first();
    canvas = document.getElementById('canvas');
    canvas.style.letterSpacing = '2px';
    ctx = canvas.getContext('2d');
    canvas.crossOrigin = "Anonymous";
    canvasDraw = function(text, font_size, font_style, fill_or_stroke){
        canvas.width = second_image.width();
        canvas.height = second_image.height();
        ctx.clearRect(0,0,canvas.width,canvas.height);
        ctx.drawImage(second_image.get(0), 0, 0, canvas.width, canvas.height);
        //refill text
        ctx.font = font_size +'px '+ font_style + ',Symbola';
        $test = ctx.font;
        ctx.textAlign = "center";
        if(fill_or_stroke){
            ctx.fillStyle = "#d2b76d";
            ctx.strokeStyle = "#9d8a5e";
            ctx.strokeText(text,canvas.width*$left,canvas.height*$top);
            ctx.fillText(text,canvas.width*$left,canvas.height*$top);
        }
        else{
            ctx.strokeStyle = "#888888";
            ctx.strokeText(text,canvas.width*$left,canvas.height*$top);
        }
    };
    

    And you don't need to use this function this.fillTextWithSpacing. I didn't use and it worked like a charm)

提交回复
热议问题