Heim  >  Artikel  >  Web-Frontend  >  HTML5 在canvas中绘制文本附效果图

HTML5 在canvas中绘制文本附效果图

PHPz
PHPzOriginal
2016-05-16 15:47:321512Durchsuche

这篇文章主要介绍了HTML5 在canvas中如何绘制文本,有两种方法在canvas中绘制文本strokeText与fillText,可以根据需求选择。

一、绘制文本 

在绘图环境中提供了两种方法在canvas中绘制文本。 

strokeText(text,x,y) : 在(x,y)处绘制空心的文本。 

fillText(text,x,y) : (x,y)处绘制实心的文本。 

二、在canvas中绘制文本 

代码如下:

<!DOCTYPE html> 
<html> 
<head> 
<meta http-equiv="Content-type" content="text/html; charset = utf-8"> 
<title>HTML5</title> 
<script type="text/javascript" charset = "utf-8"> 
//这个函数将在页面完全加载后调用 
function pageLoaded() 
{ 
//获取canvas对象的引用,注意tCanvas名字必须和下面body里面的id相同 
var canvas = document.getElementById(&#39;tCanvas&#39;); 
//获取该canvas的2D绘图环境 
var context = canvas.getContext(&#39;2d&#39;); 
//绘制代码将出现在这里 
//绘制文本 
context.fillText(&#39;Welcome to DuJun Blog&#39;,100,40); 
//修改字体 
context.font = &#39;20px Arial&#39;; 
context.fillText(&#39;Welcome to DuJun Blog&#39;,100,100); 
//绘制空心的文本 
context.font = &#39;36px 隶书&#39;; 
context.strokeText(&#39;欢迎来到笃军的博客&#39;,100,200); 
} 
</script> 
</head> 
<body onload="pageLoaded();"> 
<canvas width = "500" height = "300" id = "tCanvas" style = "border:black 1px solid;"> 
<!--如果浏览器不支持则显示如下字体--> 
提示:你的浏览器不支持<canvas>标签 
</canvas> 
</body> 
</html>

三、绘制效果 

1.gif

以上就是本章的全部内容,更多相关教程请访问Html5视频教程

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn