HTML5 Canvas阴影使用方法实例演示
程序员文章站
2023-11-17 14:52:34
HTML5 Canvas中提供了设置阴影的四个属性值可以实现阴影文字、3D拉影效果、边缘模糊效果文字,具体的演示代码如下,想学习的朋友可以参考下... 13-08-02...
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拉影效果的基础上在四个方向重复,就得到了边缘羽化的文字效果。
运行效果:
序代码:
<!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 clip 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 a html5 compatible browser.");
return;
}
var context = canvas.getcontext('2d');
// section one - shadow and blur
context.fillstyle="black";
context.fillrect(0, 0, canvas.width, canvas.height/4);
context.font = '60pt calibri';
context.shadowcolor = "white";
context.shadowoffsetx = 0;
context.shadowoffsety = 0;
context.shadowblur = 20;
context.filltext("blur canvas", 40, 80);
context.strokestyle = "rgba(0, 255, 0, 1)";
context.linewidth = 2;
context.stroketext("blur canvas", 40, 80);
// section two - shadow font
var hh = canvas.height/4;
context.fillstyle="white";
context.fillrect(0, hh, canvas.width, canvas.height/4);
context.font = '60pt calibri';
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.filltext("blur canvas", 40, 80+hh);
// section three - down shadow effect
var hh = canvas.height/4 + hh;
context.fillstyle="black";
context.fillrect(0, hh, canvas.width, canvas.height/4);
for(var i = 0; i < 10; i++)
{
context.shadowcolor = "rgba(255, 255, 255," + ((10-i)/10) + ")";
context.shadowoffsetx = i*2;
context.shadowoffsety = i*2;
context.shadowblur = i*2;
context.fillstyle = "rgba(127, 127, 127, 1)";
context.filltext("blur canvas", 40, 80+hh);
}
// section four - fade effect
var hh = canvas.height/4 + hh;
context.fillstyle="green";
context.fillrect(0, hh, canvas.width, canvas.height/4);
for(var i = 0; i < 10; i++)
{
context.shadowcolor = "rgba(255, 255, 255," + ((10-i)/10) + ")";
context.shadowoffsetx = 0;
context.shadowoffsety = -i*2;
context.shadowblur = i*2;
context.fillstyle = "rgba(127, 127, 127, 1)";
context.filltext("blur canvas", 40, 80+hh);
}
for(var i = 0; i < 10; i++)
{
context.shadowcolor = "rgba(255, 255, 255," + ((10-i)/10) + ")";
context.shadowoffsetx = 0;
context.shadowoffsety = i*2;
context.shadowblur = i*2;
context.fillstyle = "rgba(127, 127, 127, 1)";
context.filltext("blur canvas", 40, 80+hh);
}
for(var i = 0; i < 10; i++)
{
context.shadowcolor = "rgba(255, 255, 255," + ((10-i)/10) + ")";
context.shadowoffsetx = i*2;
context.shadowoffsety = 0;
context.shadowblur = i*2;
context.fillstyle = "rgba(127, 127, 127, 1)";
context.filltext("blur canvas", 40, 80+hh);
}
for(var i = 0; i < 10; i++)
{
context.shadowcolor = "rgba(255, 255, 255," + ((10-i)/10) + ")";
context.shadowoffsetx = -i*2;
context.shadowoffsety = 0;
context.shadowblur = i*2;
context.fillstyle = "rgba(127, 127, 127, 1)";
context.filltext("blur canvas", 40, 80+hh);
}
}
</script>
</head>
<body>
<h1>html5 canvas clip demo - by gloomy fish</h1>
<pre>fill and stroke clip</pre>
<div id="my_painter">
<canvas id="text_canvas"></canvas>
</div>
</body>
</html>
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拉影效果的基础上在四个方向重复,就得到了边缘羽化的文字效果。
运行效果:
序代码:
复制代码
代码如下:<!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 clip 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 a html5 compatible browser.");
return;
}
var context = canvas.getcontext('2d');
// section one - shadow and blur
context.fillstyle="black";
context.fillrect(0, 0, canvas.width, canvas.height/4);
context.font = '60pt calibri';
context.shadowcolor = "white";
context.shadowoffsetx = 0;
context.shadowoffsety = 0;
context.shadowblur = 20;
context.filltext("blur canvas", 40, 80);
context.strokestyle = "rgba(0, 255, 0, 1)";
context.linewidth = 2;
context.stroketext("blur canvas", 40, 80);
// section two - shadow font
var hh = canvas.height/4;
context.fillstyle="white";
context.fillrect(0, hh, canvas.width, canvas.height/4);
context.font = '60pt calibri';
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.filltext("blur canvas", 40, 80+hh);
// section three - down shadow effect
var hh = canvas.height/4 + hh;
context.fillstyle="black";
context.fillrect(0, hh, canvas.width, canvas.height/4);
for(var i = 0; i < 10; i++)
{
context.shadowcolor = "rgba(255, 255, 255," + ((10-i)/10) + ")";
context.shadowoffsetx = i*2;
context.shadowoffsety = i*2;
context.shadowblur = i*2;
context.fillstyle = "rgba(127, 127, 127, 1)";
context.filltext("blur canvas", 40, 80+hh);
}
// section four - fade effect
var hh = canvas.height/4 + hh;
context.fillstyle="green";
context.fillrect(0, hh, canvas.width, canvas.height/4);
for(var i = 0; i < 10; i++)
{
context.shadowcolor = "rgba(255, 255, 255," + ((10-i)/10) + ")";
context.shadowoffsetx = 0;
context.shadowoffsety = -i*2;
context.shadowblur = i*2;
context.fillstyle = "rgba(127, 127, 127, 1)";
context.filltext("blur canvas", 40, 80+hh);
}
for(var i = 0; i < 10; i++)
{
context.shadowcolor = "rgba(255, 255, 255," + ((10-i)/10) + ")";
context.shadowoffsetx = 0;
context.shadowoffsety = i*2;
context.shadowblur = i*2;
context.fillstyle = "rgba(127, 127, 127, 1)";
context.filltext("blur canvas", 40, 80+hh);
}
for(var i = 0; i < 10; i++)
{
context.shadowcolor = "rgba(255, 255, 255," + ((10-i)/10) + ")";
context.shadowoffsetx = i*2;
context.shadowoffsety = 0;
context.shadowblur = i*2;
context.fillstyle = "rgba(127, 127, 127, 1)";
context.filltext("blur canvas", 40, 80+hh);
}
for(var i = 0; i < 10; i++)
{
context.shadowcolor = "rgba(255, 255, 255," + ((10-i)/10) + ")";
context.shadowoffsetx = -i*2;
context.shadowoffsety = 0;
context.shadowblur = i*2;
context.fillstyle = "rgba(127, 127, 127, 1)";
context.filltext("blur canvas", 40, 80+hh);
}
}
</script>
</head>
<body>
<h1>html5 canvas clip demo - by gloomy fish</h1>
<pre>fill and stroke clip</pre>
<div id="my_painter">
<canvas id="text_canvas"></canvas>
</div>
</body>
</html>
上一篇: html5记忆翻牌游戏实现思路及代码