如何在HTML5画布元素上写文字?(How can I write text on a HTML5 canvas element?)

是否可以在HTML5 canvas上书写文字?

Is it possible to write text on HTML5 canvas?

最满意答案

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> 
  
 

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> 
  
 

如何在HTML5画布元素上写文字?(How can I write text on a HTML5 canvas element?)

是否可以在HTML5 canvas上书写文字?

Is it possible to write text on HTML5 canvas?

最满意答案

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> 
  
 

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>