在html5中实现canvas阴影效果
代码如下:
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);
}
}
fill and stroke clip