fillText() - canvas图形
fillText()
实例
使用 fillText(),在画布上写文本"Hello world!"和"Big smile!":
YourbrowserdoesnotsupporttheHTML5canvastag. var c=document.getElementById("myCanvas"); var canvOK=1; try {c.getContext("2d");} catch (er) {canvOK=0;} if (canvOK==1) { 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); }JavaScript:
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 9、Firefox、Opera、Chrome 和 Safari 支持 fillText()方法。 | ||||
注意:Internet Explorer 8 及之前的版本不支持元素。 注意:Safari 不支持 maxWidth 参数。 |
定义和用法
fillText()方法在画布上绘制填色的文本。文本的默认颜色是黑色。
提示:请使用font属性来定义字体和字号,并使用fillStyle属性以另一种颜色/渐变来渲染文本。
JavaScript 语法: | context.fillText(text,x,y,maxWidth); |
---|
参数值
参数 | 描述 |
---|---|
text | 规定在画布上输出的文本。 |
x | 开始绘制文本的 x 坐标位置(相对于画布)。 |
y | 开始绘制文本的 y 坐标位置(相对于画布)。 |
maxWidth | 可选。允许的最大文本宽度,以像素计。 |
内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)