HTML5 Canvas Circle Text

前端 未结 6 1221
感动是毒
感动是毒 2020-11-29 07:30

How do I create circle text (text in a circle shape) with canvas?

\"Like

6条回答
  •  暖寄归人
    2020-11-29 08:06

    Letters should now be properly oriented:

    CanvasRenderingContext2D.prototype.fillTextCircle = function(text,x,y,radius,startRotation){
       var numRadsPerLetter = 2*Math.PI / text.length;
       this.save();
       this.translate(x,y);
       this.rotate(startRotation);
    
       for(var i=0;i

    Sample usage:

    var ctx = document.getElementById('canvas').getContext('2d');
    ctx.font = "bold 30px Serif";
    ctx.fillTextCircle("Circle Text ",150,150,75,Math.PI / 2);
    

    The extra space at the end of the string adds some extra padding.

    Sample output:

    Sample Output

提交回复
热议问题