阴影文字:
只要设置shadowOffsetX与shadowOffsetY的值,当值都正数时,阴影相对文字的右下
方偏移。当值都为负数">

Home  >  Article  >  Web Front-end  >  HTML5 Canvas阴影使用方法实例演示_html5教程技巧

HTML5 Canvas阴影使用方法实例演示_html5教程技巧

WBOY
WBOYOriginal
2016-05-16 15:49:021421browse

HTML5 Canvas中提供了设置阴影的四个属性值分别为:
context.shadowColor = “red” 表示设置阴影颜色为红色
context.shadowOffsetX = 0表示阴影相对TEXT的水平距离,0表示两者水平位置重合
context.shadowOffsetY = 0表示阴影相对TEXT的垂直距离,0表示两者垂直位置重合
context.shadowBlur = 10 阴影模糊效果,值越大模糊越厉害。
一个最简单的带有阴影的矩形代码如下:
context.shadowColor = "RGBA(127,127,127,1)";
context.shadowOffsetX = 3;
context.shadowOffsetY = 3;
context.shadowBlur = 0;
context.fillStyle = "RGBA(0, 0, 0, 0.8)";
context.fillRect(10, hh+10, 200,canvas.height/4-20);
效果如下:
 
阴影文字:
只要设置shadowOffsetX与shadowOffsetY的值,当值都正数时,阴影相对文字的右下
方偏移。当值都为负数时,阴影相对文字的左上方偏移。
3D拉影效果:
在同一位置不断的重复绘制文字同时改变shadowOffsetX、shadowOffsetY、shadowBlur
的值,从小到大不断偏移不断增加,透明度也不断增加。就得到了拉影效果文字。
边缘模糊效果文字:
在3D拉影效果的基础上在四个方向重复,就得到了边缘羽化的文字效果。
运行效果:
 
序代码:

复制代码
代码如下:






Canvas Clip Demo

<script> <br />var ctx = null; // global variable 2d context <br />var imageTexture = null; <br />window.onload = function() { <br />var canvas = document.getElementById("text_canvas"); <br />console.log(canvas.parentNode.clientWidth); <br />canvas.width = canvas.parentNode.clientWidth; <br />canvas.height = canvas.parentNode.clientHeight; <br />if (!canvas.getContext) { <br />console.log("Canvas not supported. Please install a HTML5 compatible browser."); <br />return; <br />} <br />var context = canvas.getContext('2d'); <br />// section one - shadow and blur <br />context.fillStyle="black"; <br />context.fillRect(0, 0, canvas.width, canvas.height/4); <br />context.font = '60pt Calibri'; <br />context.shadowColor = "white"; <br />context.shadowOffsetX = 0; <br />context.shadowOffsetY = 0; <br />context.shadowBlur = 20; <br />context.fillText("Blur Canvas", 40, 80); <br />context.strokeStyle = "RGBA(0, 255, 0, 1)"; <br />context.lineWidth = 2; <br />context.strokeText("Blur Canvas", 40, 80); <br />// section two - shadow font <br />var hh = canvas.height/4; <br />context.fillStyle="white"; <br />context.fillRect(0, hh, canvas.width, canvas.height/4); <br />context.font = '60pt Calibri'; <br />context.shadowColor = "RGBA(127,127,127,1)"; <br />context.shadowOffsetX = 3; <br />context.shadowOffsetY = 3; <br />context.shadowBlur = 0; <br />context.fillStyle = "RGBA(0, 0, 0, 0.8)"; <br />context.fillText("Blur Canvas", 40, 80+hh); <br />// section three - down shadow effect <br />var hh = canvas.height/4 + hh; <br />context.fillStyle="black"; <br />context.fillRect(0, hh, canvas.width, canvas.height/4); <br />for(var i = 0; i < 10; i++) <br />{ <br />context.shadowColor = "RGBA(255, 255, 255," + ((10-i)/10) + ")"; <br />context.shadowOffsetX = i*2; <br />context.shadowOffsetY = i*2; <br />context.shadowBlur = i*2; <br />context.fillStyle = "RGBA(127, 127, 127, 1)"; <br />context.fillText("Blur Canvas", 40, 80+hh); <br />} <br />// section four - fade effect <br />var hh = canvas.height/4 + hh; <br />context.fillStyle="green"; <br />context.fillRect(0, hh, canvas.width, canvas.height/4); <br />for(var i = 0; i < 10; i++) <br />{ <br />context.shadowColor = "RGBA(255, 255, 255," + ((10-i)/10) + ")"; <br />context.shadowOffsetX = 0; <br />context.shadowOffsetY = -i*2; <br />context.shadowBlur = i*2; <br />context.fillStyle = "RGBA(127, 127, 127, 1)"; <br />context.fillText("Blur Canvas", 40, 80+hh); <br />} <br />for(var i = 0; i < 10; i++) <br />{ <br />context.shadowColor = "RGBA(255, 255, 255," + ((10-i)/10) + ")"; <br />context.shadowOffsetX = 0; <br />context.shadowOffsetY = i*2; <br />context.shadowBlur = i*2; <br />context.fillStyle = "RGBA(127, 127, 127, 1)"; <br />context.fillText("Blur Canvas", 40, 80+hh); <br />} <br />for(var i = 0; i < 10; i++) <br />{ <br />context.shadowColor = "RGBA(255, 255, 255," + ((10-i)/10) + ")"; <br />context.shadowOffsetX = i*2; <br />context.shadowOffsetY = 0; <br />context.shadowBlur = i*2; <br />context.fillStyle = "RGBA(127, 127, 127, 1)"; <br />context.fillText("Blur Canvas", 40, 80+hh); <br />} <br />for(var i = 0; i < 10; i++) <br />{ <br />context.shadowColor = "RGBA(255, 255, 255," + ((10-i)/10) + ")"; <br />context.shadowOffsetX = -i*2; <br />context.shadowOffsetY = 0; <br />context.shadowBlur = i*2; <br />context.fillStyle = "RGBA(127, 127, 127, 1)"; <br />context.fillText("Blur Canvas", 40, 80+hh); <br />} <br />} <br /></script>


HTML5 Canvas Clip Demo - By Gloomy Fish


Fill And Stroke Clip






Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn