首页 > 作文

实例讲解使用HTML5 Canvas绘制阴影效果的方法

更新时间:2023-04-03 05:23:28 阅读: 评论:0

创建阴影效果需要操作以下4个属性:

1.context.shadowcolor:阴影颜色。
2.context.shadowofftx:阴影x轴位移。正值向右,负值向左。
3.context.shadowoffty:阴影y轴位移。正值向下,负值向上。
4.context.shadowblur:阴影模糊滤镜。数据越大,扩散程度越大。
这四个属性只要设置了第一个和剩下三个中的任意一个就有阴影效果。不过通常情况下,四个属性都要设置。

例如,创建一个向右下方位移各5px的红色阴影,模糊2px,可以这样写。

javascript code
复制内容到剪贴板

context.shadowcolor=“red”; context.shadowofftx=5; context.shadowoffty=5; context.shadowblur=2;

需要注意的是,这里的阴影同其他属性设置一样,都是基于状态的设置。因此,如果只想为某一个对象应用阴影而不是全局阴影,需要在下次绘制前重置阴影的这四个属性。
运行结果:

阴影文字:

只要设置shadowofftx与shadowoffty的值,当值都正数时,阴影相对文字的右下

方偏移。当值都为负数时,阴影相对文字的左上方偏移。

3d拉影效果:

在同一位置不断的重复绘制文字同时改变shadowofftx、shadowoffty、shadowblur

的值,从小到大不断偏移不断增加,透明度也不断增加。就得到了拉影效果文字。

边缘模糊效果文字:

在3d拉影效果的基础上在四个方向重复,就得到了边缘羽化的文字效果。

运行效果:

程序代码:

javascript code
复制内容到剪贴板

<!doctypehtml> <html> <head> <metahttp-equiv=“x-ua-compatible”content=“chrome=ie8”> 中国近代史读后感<metahttp-equiv=“content-type”content=“text/html;chart=utf-8”> <title>canvasclipdemo</title> <linkhref=“default.css̶事与愿违的反义词1;rel=“stylesheet”/> <script> varctx=null;//globalvariable2dcontext varimagetexture=null; window.onload=function(){ varcanvas=document.getelementbyid(“text_canvas”); console.log(canvas.parentnode.clientwidth); canvas.width=canvas.parentnode.clientwidth; canvas.height=canvas.parentnode.clientheight; if(!canvas.getcontext){ console.log(“canvasnotsupported.pleainstallahtml5compatiblebrowr.”); return; } varcontext=canvas.getcontext(‘2d’); //ctionone–shadowandblur context.fillstyle=“black”; context.fillrect(0,0,canvas.width,canvas.height/4); context.font=’60ptcalibri’; context.shadowcolor=“white”; context.shadowofftx=0; context.shadowoffty=0; context.shadowblur=20; context.filltext(“blurcanvas”,40,80); context.strokestyle=“rgba(0,255,0,1)”; context.linewidth=2法制在我心中; context.stroketext(“blurcanvas”,40,80); //ctiontwo–shadowfont varhh=canvas.height/4; context.fillstyle=“white”; context.fillrect(0,hh,canvas.width,canvas.height/4); context.font=’60ptcalibri’; context.shadowcolor=“rgba(127,127,127,1)”; context.shadowo学习态度怎么写fftx=3; context.shadowoffty=3; context.shadowblur=0; context.fillstyle=“rgba(0,0,0,0.8)”; context.filltext(“blurcanvas”,40,80+hh); //ctionthree–downshadoweffect varhh=canvas.height/4+hh; context.fillstyle=“black”; context.fillrect(0,hh,canvas.width,canvas.height/4); for(vari=0;i<10;i++) { context.shadowcolor=“rgba(255,255,255,”+((10-i)/10)+“)”; context.shadowofftx=i*2; context.shadowoffty=i*2; context.shadowblur=i*2; context.fillstyle=“rgba(127,127,127,1)”; context.filltext(“blurcanvas”,40,80+hh); } //ctionfour–fadeeffect varhh=canvas.height/4+hh; context.fillstyle=“green”; context.fillrect(0,hh,canvas.width,canvas.height/4); for(vari=0;i<10;i++) { context.shadowcolor=“rgba(255,255,255,”+((10-i)/10)+“)”; context.shadowofftx=0; context.shadowoffty=-i*2; context.shadowblur=i*2; context.fillstyle=“rgba(127,127,127,1)”; context.filltext(“blurcanvas”,40,80+hh); } for(vari=0;i<10;i++) { context.shadowcolor=“rgba(255,255,255,”+((10-i)/10)+“)”; context.shadowofftx=0; context.shadowoffty=i*2; context.shadowblur=i*2; context.fillstyle=“rgba(127,127,127,1)”; context.filltext(“blurcanvas”,40,80+hh); } for(vari=0;i<10;i++) { context.shadowcolor=“rgba(255,255,255,”+((10-i)/10)+“)”; context.shadowofftx=i*2; context.shadowoffty=0; context.shadowblur=i*2; context.fillstyle=“rgba(127,127,127,1)读书会策划”; context.filltext(“blurcanvas”,40,80+hh); } for(vari=0;i<10;i++) { context.shadowcolor=“rgba(255,255,255,”+((10-i)/10)+“)”; context.shadowofftx=-i*2; context.shadowoffty=0; context.shadowblur=i*2; context.fillstyle=“rgba(127,127,127,1)”; context.filltext(“blurcanvas”,40,80+hh); } } </script> </head> <body> <h1>html5canvasclipdemo–bygloomyfish</h1> <pre>fillandstrokeclip</pre> <divid=“my_painter”> <canvasid=“text_canvas”></canvas> </div> </body> </html>

本文发布于:2023-04-03 05:23:26,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/zuowen/2411f0df7280794fc6e30f8437b4188e.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

本文word下载地址:实例讲解使用HTML5 Canvas绘制阴影效果的方法.doc

本文 PDF 下载地址:实例讲解使用HTML5 Canvas绘制阴影效果的方法.pdf

标签:阴影   效果   文字   属性
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图