html5实现canvas阴影效果示例


Posted in HTML / CSS onMay 07, 2014

在HTML5中实现Canvas阴影效果

html5实现canvas阴影效果示例

复制代码
代码如下:

<!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</h1>
<pre>Fill And Stroke Clip</pre>
<div id="my_painter">
<canvas id="text_canvas"></canvas>
</div>
</body>
</html>

HTML / CSS 相关文章推荐
CSS3动画之流彩文字效果+图片模糊效果+边框伸展效果实现代码合集
Aug 18 HTML / CSS
css3背景图片透明叠加属性cross-fade简介及用法实例
Jan 08 HTML / CSS
详解CSS3中nth-child与nth-of-type的区别
Jan 05 HTML / CSS
CSS3,线性渐变(linear-gradient)的使用总结
Jan 09 HTML / CSS
HTML5表格_动力节点Java学院整理
Jul 11 HTML / CSS
突破canvas语法限制 让他支持链式语法
Dec 24 HTML / CSS
css3 transform 3d 使用css3创建动态3d立方体(html5实践)
Jan 06 HTML / CSS
一款利用html5和css3动画排列人物头像的实例演示
Dec 05 HTML / CSS
详解HTML5中div和section以及article的区别
Jul 14 HTML / CSS
HTML5本地数据库基础操作详解
Apr 26 HTML / CSS
AmazeUI 加载进度条的实现示例
Aug 20 HTML / CSS
解析CSS 提取图片主题色功能(小技巧)
May 12 HTML / CSS
html5 利用重力感应实现摇一摇换颜色可用来做抽奖等等
May 07 #HTML / CSS
HTML5资源预加载(Link prefetch)详细介绍(给你的网页加速)
May 07 #HTML / CSS
HTML5中5个简单实用的API(第二篇,含全屏、可见性、拍照、预加载、电池状态)
May 07 #HTML / CSS
html5 datalist标签使用示例(自动完成组件)
May 04 #HTML / CSS
HTML5 visibilityState属性详细介绍和使用实例
May 03 #HTML / CSS
HTML5中判断用户是否正在浏览页面的方法
May 03 #HTML / CSS
在HTML5 Canvas中放入图片和保存为图片的方法
May 03 #HTML / CSS
You might like
一个基于PDO的数据库操作类(新) 一个PDO事务实例
2011/07/03 PHP
PHP读取配置文件类实例(可读取ini,yaml,xml等)
2015/07/28 PHP
php生成mysql的数据字典
2016/07/07 PHP
javascript 闭包疑问
2010/12/30 Javascript
JavaScript高级程序设计(第3版)学习笔记2 js基础语法
2012/10/11 Javascript
js文本框输入点回车触发确定兼容IE、FF等
2013/11/19 Javascript
javascript if条件判断方法小结
2014/05/17 Javascript
javascript中clipboardData对象用法详解
2015/05/13 Javascript
jquery中的常见问题及快速解决方法小结
2016/06/14 Javascript
前端框架Vue.js中Directive知识详解
2016/09/12 Javascript
JavaScript面试题(指针、帽子和女朋友)
2016/11/23 Javascript
详解用webpack2搭建angular2的项目
2017/06/22 Javascript
基于zepto.js实现手机相册功能
2017/07/11 Javascript
layui中使用jquery控制radio选中事件的示例代码
2018/08/15 jQuery
小程序实现录音上传功能
2019/11/22 Javascript
JS实现canvas简单小画板功能
2020/06/23 Javascript
Python正则表达式经典入门教程
2017/05/22 Python
python matplotlib 注释文本箭头简单代码示例
2018/01/08 Python
Python使用pickle模块存储数据报错解决示例代码
2018/01/26 Python
widows下安装pycurl并利用pycurl请求https地址的方法
2018/10/15 Python
Python 将Matrix、Dict保存到文件的方法
2018/10/30 Python
浅谈python的dataframe与series的创建方法
2018/11/12 Python
python 堆和优先队列的使用详解
2019/03/05 Python
python下PyGame的下载与安装过程及遇到问题
2019/08/04 Python
图解Python中深浅copy(通俗易懂)
2020/09/03 Python
django跳转页面传参的实现
2020/09/17 Python
paramiko使用tail实时获取服务器的日志输出详解
2020/12/06 Python
法国女性内衣购物网站:Glamuse
2019/05/13 全球购物
如何利用find命令查找文件
2016/11/18 面试题
《木笛》教学反思
2014/03/01 职场文书
护理专业毕业生自荐信
2014/06/15 职场文书
《周恩来的四个昼夜》观后思想汇报范文两篇
2014/09/10 职场文书
2014学校领导四风对照检查材料思想汇报
2014/09/23 职场文书
工作疏忽检讨书500字
2014/10/26 职场文书
在校证明模板
2015/06/17 职场文书
检讨书范文
2019/04/16 职场文书