HTML教程
HTML5教程
HTML参考手册

HTML canvas fillText() 方法

使用 fillText(),在画布上写文本 "Hello world!" 和 "Big smile!":
您的浏览器不支持HTML5 canvas标签.
var c=document.getElementById("myCanvas");
var ctx=c.getContext("2d");

ctx.font="20px Georgia";
ctx.fillText("Hello World!",10,50);

ctx.font="30px Verdana";
// Create gradient
var gradient=ctx.createLinearGradient(0,0,c.width,0);
gradient.addColorStop("0","magenta");
gradient.addColorStop("0.5","blue");
gradient.addColorStop("1.0","red");
// Fill with gradient
ctx.fillStyle=gradient;
ctx.fillText("Big smile!",10,90);

浏览器支持

Internet Explorer Firefox Opera Google Chrome Safari
Internet Explorer 9、Firefox、Opera、Chrome 和 Safari 支持 fillText() 方法。
注意:Internet Explorer 8 及之前的版本不支持 <canvas> 元素。
注意:Safari 不支持 maxWidth 参数。

定义和用法

fillText() 方法在画布上绘制填色的文本。文本的默认颜色是黑色。
JavaScript 语法: context.fillText(text,x,y,maxWidth);

参数值

参数 描述
text 规定在画布上输出的文本。
x 开始绘制文本的 x 坐标位置(相对于画布)。
y 开始绘制文本的 y 坐标位置(相对于画布)。
maxWidth 可选。允许的最大文本宽度,以像素计。
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4