HTML5 Canvas如何实现纹理填充与描边(Fill And Stroke)
发布
本文为大家详细介绍下HTML5 Canvas Fill 与Stroke文字效果,基于Canvas如何实现纹理填充与描边、颜色填充与描边,具体代码如下,感兴趣的朋友可以参考下哈,希望对大家有所帮助
演示HTML5 Canvas Fill 与Stroke文字效果,基于Canvas如何实现纹理填充与描边。 一:颜色填充与描边 颜色填充可以通过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); 纹理图片: 三:运行效果 代码: 复制代码代码如下: !DOCTYPE html html head meta http-equiv="X-UA-Compatible" content="chrome=IE8" meta http-equiv="Content-type" content="text/html;charset=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 install patible browser."); return; } // get 2D context of canvas and draw rectangel ctx = canvas.getContext("2d"); ctx.fill ; ctx.fillRect(0, 0, canvas.width, canvas.height); // 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); // fill and stroke by pattern imageTexture = document.createElement('img'); imageTexture.src = "../pattern.png"; imageTexture.onload = loaded(); } function loaded() { // delay to image loaded setTimeout(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 h1 HTML5 Canvas Text Demo - By Gloomy Fish /h1 pre Fill And Stroke /pre div id="my_painter" canvas id="text_canvas" /canvas /div /body /html 这篇文章主要介绍了HTML+CSS+JavaScript实现图片3D展览的示例代码。文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着 这篇文章主要介绍了HTML5逐步分析实现拖放功能的方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下 这篇文章主要介绍了移动端HTML5 input常见问题(小结),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习 这篇文章主要介绍了HTML5超文本标记语言的实现方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下 这篇文章主要介绍了Html5 webRTC简单实现视频调用的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来 这篇文章主要介绍了Html5之webcoekt播放JPEG图片流,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学 这篇文章主要介绍了如何在Canvas上的图形/图像绑定事件监听的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着 这篇文章主要介绍了canvas 绘图时位置偏离的问题解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学 这篇文章主要介绍了关于canvas.toDataURL 在iOS运行失败的问题解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着 这篇文章主要介绍了canvas绘制图片drawImage使用方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学 (责任编辑:admin) |