How can I write text on a HTML5 canvas element?

前端 未结 8 942
没有蜡笔的小新
没有蜡笔的小新 2020-11-28 21:36

Is it possible to write text on HTML5 canvas?

相关标签:
8条回答
  • 2020-11-28 22:04

    var canvas = document.getElementById("my-canvas");
    var context = canvas.getContext("2d");
    
    context.fillStyle = "blue";
    context.font = "bold 16px Arial";
    context.fillText("Zibri", (canvas.width / 2) - 17, (canvas.height / 2) + 8);
    #my-canvas {
      background: #FF0;
    }
    <canvas id="my-canvas" width="200" height="120"></canvas>

    0 讨论(0)
  • 2020-11-28 22:09

    Canvas text support is actually pretty good - you can control font, size, color, horizontal alignment, vertical alignment, and you can also get text metrics to get the text width in pixels. In addition, you can also use canvas transforms to rotate, stretch and even invert text.

    0 讨论(0)
  • 2020-11-28 22:15

    Drawing text on a Canvas

    Markup:

    <canvas id="myCanvas" width="300" height="150"></canvas>
    

    Script (with few different options):

    <script>
        var canvas = document.getElementById('myCanvas');
        var ctx = canvas.getContext('2d');
        ctx.font = 'italic 18px Arial';
        ctx.textAlign = 'center';
        ctx. textBaseline = 'middle';
        ctx.fillStyle = 'red';  // a color name or by using rgb/rgba/hex values
        ctx.fillText('Hello World!', 150, 50); // text and position
    </script>
    

    Check out the MDN documentation and this JSFiddle example.

    0 讨论(0)
  • 2020-11-28 22:17

    I found a good tutorial on oreilly.com.

    Example code:

    <canvas id="canvas" width ='600px'></canvas><br />
    Enter your Text here .The Text will get drawn on the canvas<br />
    <input type="text" id="text" onKeydown="func();"></input><br />
    </body><br />
    <script>
    function func(){
    var e=document.getElementById("text"),t=document.getElementById("canvas"),n=t.getContext("2d");
    n.fillStyle="#990000";n.font="30px futura";n.textBaseline="top";n.fillText(e.value,150,0);n.fillText("thank you, ",200,100);
    n.fillText("Created by ashish",250,120)
    }
    </script>
    

    courtesy: @Ashish Nautiyal

    0 讨论(0)
  • 2020-11-28 22:22

    Depends on what you want to do with it I guess. If you just want to write some normal text you can use .fillText().

    0 讨论(0)
  • 2020-11-28 22:25

    Yes of course you can write a text on canvas with ease, and you can set the font name, font size and font color. There are two method to build a text on Canvas, i.e. fillText() and strokeText(). fillText() method is used to make a text that can only be filled with color, whereas strokeText() is used to make a text that can only be given an outline color. So if we want to build a text that filled with color and have outline color, we must use both of them.

    here the full example, how to write text on canvas :

    <canvas id="Canvas01" width="400" height="200" style="border:2px solid #bbb; margin-left:10px; margin-top:10px;"></canvas>
    
    <script>
      var canvas = document.getElementById('Canvas01');
      var ctx = canvas.getContext('2d');
    
      ctx.fillStyle= "red";
      ctx.font = "italic bold 35pt Tahoma";
      //syntax : .fillText("text", x, y)
      ctx.fillText("StacOverFlow",30,80);
    
    </script>
    

    Here the demo for this, and you can try your self for any modification: http://okeschool.com/examples/canvas/html5-canvas-text-color

    0 讨论(0)
提交回复
热议问题