h5Canvas中Fill与Stroke文字效果如何实现-创新互联

这篇文章主要介绍h5 Canvas中Fill 与Stroke文字效果如何实现,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

成都创新互联成都网站建设定制设计,是成都网站营销推广公司,为发电机租赁提供网站建设服务,有成熟的网站定制合作流程,提供网站定制设计服务:原型图制作、网站创意设计、前端HTML5制作、后台程序开发等。成都网站制作热线:18982081108

 
一:颜色填充与描边
颜色填充可以通过fillStyle来实现,描边颜色可以通过strokeStyle来实现。简单示例
如下:

// fill and stroke text 
ctx.font = '60pt Calibri'; 
ctx.lineWidth = 3; 
ctx.strokeStyle = 'green'; 
ctx.strokeText('Hello World!', 20, 100); 
ctx.fillStyle = 'red'; 
ctx.fillText('Hello World!', 20, 100);

二:纹理填充与描边
HTML5 Canvas还支持纹理填充,通过加载一张纹理图像,然后创建画笔模式,创建纹理模式的API为ctx.createPattern(imageTexture,"repeat");第二参数支持四个值,分别为”repeat-x”, ”repeat-y”, ”repeat”,”no-repeat”意思是纹理分别沿着X轴,Y轴,XY方向沿重复或者不重复。纹理描边与填充的代码如下:

var woodfill = ctx.createPattern(imageTexture,"repeat"); 
ctx.strokeStyle = woodfill; 
ctx.strokeText('Hello World!', 20, 200); 
// fill rectangle 
ctx.fillStyle = woodfill; 
ctx.fillRect(60, 240, 260, 440);

纹理图片:
h5 Canvas中Fill 与Stroke文字效果如何实现
三:运行效果
h5 Canvas中Fill 与Stroke文字效果如何实现

代码如下:

<!DOCTYPE html> 
<html> 
<head> 
<meta http-equiv="X-UA-Compatible" content="
chr
ome=IE8"> 
<meta http-equiv="Content-type" content="text/html;char
set
=UTF-8"> 
<title>Canvas Fill And Stroke Text Demo</title> 
<link href="default.css" rel="stylesheet" /> 
<script> 
var ctx = 
null
; // global variable 2d context 
var imageTexture = null; 
window.
onload
 = function() { 
var canvas = 
document
.getElementById("text_canvas"); 
console.log(canvas.parentNode.clientWidth); 
canvas.width = canvas.parentNode.clientWidth; 
canvas.
height
 = canvas.parentNode.clientHeight; 
if (!canvas.getContext) { 
console.log("Canvas not supported. Please inst
all
 a HTML5 compatible browser."); 
return
; 
} 
// get 2D context of canvas and draw rectangel 
ctx = canvas.getContext("2d"); 
ctx.fillStyle="black"; 
ctx.fillRect(0, 0, canvas.width, canvas.height); 
// fill and stroke text 
ctx.font = '60pt Calibri'; 
ctx.li
neW
idth = 3; 
ctx.strokeStyle = 'green'; 
ctx.strokeText('Hello World!', 20, 100); 
ctx.fillStyle = 'red'; 
ctx.fillText('Hello World!', 20, 100); 
// fill and stroke by pattern 
imageTexture = document.createElement('img'); 
imageTexture.src = "../pattern.png"; 
imageTexture.onload = loaded(); 
} 
function loaded() { 
// delay to image loaded 
set
Time
out(textureFill, 1000/30); 
} 
function textureFill() { 
// var woodfill = ctx.createPattern(imageTexture, "repeat-x"); 
// var woodfill = ctx.createPattern(imageTexture, "repeat-y"); 
// var woodfill = ctx.createPattern(imageTexture, "no-repeat"); 
var woodfill = ctx.createPattern(imageTexture, "repeat"); 
ctx.strokeStyle = woodfill; 
ctx.strokeText('Hello World!', 20, 200); 
// fill rectangle 
ctx.fillStyle = woodfill; 
ctx.fillRect(60, 240, 260, 440); 
} 
</script> 
</head> 
<body> 
<h2>HTML5 Canvas Text Demo - By Gloomy Fish</h2> 
<pre>Fill And Stroke</pre> 
<p id="my_painter"> 
<canvas id="text_canvas"></canvas> 
</p> 
</body> 
</html>

以上是h5 Canvas中Fill 与Stroke文字效果如何实现的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注创新互联成都网站设计公司行业资讯频道!

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。

文章标题:h5Canvas中Fill与Stroke文字效果如何实现-创新互联
当前路径:https://www.cdcxhl.com/article32/epopc.html

成都网站建设公司_创新互联,为您提供定制网站全网营销推广外贸网站建设自适应网站品牌网站建设电子商务

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

成都网站建设公司