一、图片加载控件

在canvas效果制作中常常要使用多张图片,为了提高用户体验,需要给用户提供一个图片加载中的过度页面效果。过度效果,我在项目中使用的是Sonic.js,它在git上的地址为https://github.com/padolsey/Sonic,个人认为它的样式和扩展性都还不错,使用方式也比较简单。效果图为:

调用代码也比较简单

<script type="text/javascript" src="js/sonic.js"></script>
<script>
var loading = new Sonic({
width: 100,
height: 100,
stepsPerFrame: 1,
trailLength: 1,
pointDistance: .1,
fps: 15,
padding: 10,
fillColor: '#fff',
setup: function() {
this._.lineWidth = 20;
},
path: [
['line', 0, 20, 100, 20],
['line', 100, 20, 0, 20]
]
});
loading.play();
document.body.appendChild(loading.canvas);
</script>

下面是自己写的一个图片加载的工具方法

(function(window,undefind){    //图片加载控件
function imgLoad(options){
var defaults = {
imgArr : [] , //图片数组
loadOverCallback : null //加载完成的回调函数
};
var opts = $.extend(true , defaults , options || {}) ,
imgSize = opts.imgArr.length , //需要加载图片的个数
completeSize = 0;
function beginLoad() {
for (var index in opts.imgArr) {
var src = opts.imgArr[index];
src && loadImg(src);
}
}
function loadImg(src) { //加载图片
var image = new Image(),
handleLoadOver = function() {
completeSize++;
checkLoadOver();
};
image.src = src;
if (image.complete) { //图片有缓存
handleLoadOver();
} else {
image.onload = function() { //图片获取成功
handleLoadOver();
};
}
}
function checkLoadOver() { //查询是否已经加载完毕
if (completeSize != imgSize) return;
if(opts.loadOverCallback && typeof opts.loadOverCallback === "function"){
opts.loadOverCallback();
}
}
beginLoad()//开始执行
}
window.imgLoad = imgLoad;
})(window);

代码的逻辑就是把需要加载的图片url,以数组的形式传入,然后通过new Image()对象进行加载,每加载完毕一个执行一次检查是否完毕的函数,当所有图片全部加载完则执行结束的回调函数。调用方法为:

window.imgLoad({
imgArr : [
"img/1.jpg", "img/2.jpg", "img/3.jpg", "img/4.jpg", "img/5.jpg"
],
loadOverCallback : function(){
//结束操作....
}
});

二、指定元素上飘雪、彩带、星星的效果

(function(window , document , undefind){        //canvas特效
function canvasEffect(options) {
var defaults = {
ele : document.getElementById("body"), //覆盖元素
type : "snow" , //snow为雪花,band为彩带,star为星星
particleMax : 200 , //元素最大数
particleCount : 100 , //使用元素数目
bandColorArr : ["#82F269", "#F61326", "#F6F313", "#518DF6"] , //彩带颜色数组
vy : 1 , //Y轴上的运动速度
vyFloat : 1.5 , //Y轴速度浮动的系数
vx : 0.5 , //X轴上的运动速度
vxFloat : 1 , //X轴速度浮动的系数
w : 8 , //元素的宽度
wFloat : 2 , //宽度的浮动系数
h : 12 , //元素的高度
hFloat : 4 , //高度的浮动系数
o : 0.4 , //元素的透明度
oFloat : 0.5 , //透明度的浮动系数
r : 1 , //半径
rFloat : 2 ,//半径的浮动值
snowColor : "#FFF" //雪花的颜色
}
var opts = $.extend(true, defaults, options || {});
var canvas = document.createElement('canvas'),
ctx = canvas.getContext('2d'),
width = opts.ele.clientWidth,
height = opts.ele.clientHeight,
i = 0 , active = false , objFlakes = [] , objFlake,
lastDate = 0 , dateDel = 0 , isChange = false;
var starPic = new Image();
starPic.src = "img/star.png";
canvas.style.position = 'absolute';
canvas.style.left = canvas.style.top = '0';
var Objflake = function() {
this.reset(); //初始化
};
Objflake.prototype.reset = function() {
this.x = Math.random() * width;
this.y = Math.random() * -height;
this.vy = opts.vy + Math.random()*opts.vyFloat;
this.vx = opts.vx - Math.random()*opts.vxFloat;
this.w = opts.w + Math.random()*opts.wFloat;
this.h = opts.h + Math.random()*opts.hFloat;
this.o = opts.o + Math.random()*opts.oFloat;
this.color = opts.bandColorArr[parseInt(Math.random() * opts.bandColorArr.length)];
this.c = Math.random() > 0.5 ? 1 : -1;
this.r = opts.r + Math.random()*opts.rFloat;
this.picNo = Math.floor(Math.random() * 7);
};
function generatebandFlakes() { //初始化元素个数
objFlakes = [];
for (i = 0; i < opts.particleMax; i++) {
objFlake = new Objflake();
objFlake.reset();
objFlakes.push(objFlake);
}
}
function update() { //更新
ctx.clearRect(0, 0, width, height);
if (!active) {
return;
}
if(opts.type == "star"){
var nowDate = Date.now();
dateDel += nowDate - lastDate;
lastDate = nowDate;
isChange = (dateDel > 60);
if(isChange){
dateDel = 0;
}
}
for (i = 0; i < opts.particleCount; i++) {
objFlake = objFlakes[i];
objFlake.y += objFlake.vy;
objFlake.x += objFlake.vx;
if(opts.type == "snow"){
drawSnow(objFlake.x, objFlake.y, objFlake.r , objFlake.o);
}else if(opts.type == "band"){
drawBand(objFlake.x, objFlake.y, objFlake.color, objFlake.w,
objFlake.h, objFlake.c, objFlake.o);
}else if(opts.type == "star"){
if(isChange){
objFlake.picNo += 1;
objFlake.picNo = objFlake.picNo%7;
}
ctx.drawImage(starPic, objFlake.picNo * 7, 0, 7, 7, objFlake.x, objFlake.y, 7, 7);
}
if (objFlake.y > height) {
objFlake.reset();
}
}
requestAnimFrame(update);
}
//画彩带
function drawBand(x, y, color, w, h, c, alpha, isRotate, rotatePI) {
var leakdep = h < 10 ? 2 : 8;
var leak = c > 0 ? leakdep : -(leakdep);
ctx.save();
ctx.fillStyle = color;
ctx.globalAlpha = alpha;
ctx.beginPath();
ctx.moveTo(x, y - h);
ctx.lineTo(x + w, y - h);
ctx.quadraticCurveTo(x + w + c, y - h / 2, x + w + leak, y);
ctx.lineTo(x + leak, y);
ctx.quadraticCurveTo(x + c, y - h / 2, x, y - h);
ctx.fill();
ctx.closePath();
ctx.restore();
}
//画雪花
function drawSnow(x , y , r , o){
ctx.save();
ctx.fillStyle = opts.snowColor;
ctx.globalAlpha = o;
ctx.beginPath();
ctx.arc(x , y , r , 0, Math.PI * 2, false);
ctx.fill();
ctx.closePath();
ctx.restore();
}
function onResize() {
width = opts.ele.clientWidth;
height = opts.ele.clientHeight;
canvas.width = width;
canvas.height = height;
lastDate = Date.now();
var wasActive = active;
active = width > 300;
if (!wasActive && active) {
requestAnimFrame(update);
}
}
window.requestAnimFrame = (function() {
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
function(callback) {
window.setTimeout(callback, 1000 / 60);
};
})();
generatebandFlakes();
onResize();
opts.ele.appendChild(canvas);
window.addEventListener('resize', onResize, false);
}
window.canvasEffect = canvasEffect;
})(window,document);

代码如上,逻辑就是在指定的ele元素上,创建一个canvas标签,高宽同ele元素,在这个画布上绘制雪花、彩带或者星星图片,根据动画函数requestAnimationFrame函数以及预先定义好的x,y轴方向的速度,进行一帧一帧的绘制,每一帧的x,y坐标发生变化,进行产生动画效果。星星的图片为

调用方式如下:

1、星星

window.canvasEffect({
ele : document.getElementsByTagName("body")[0] ,
type : "star" ,
particleMax : 300 ,
particleCount : 150 ,
vy : 0.3 ,
vyFloat : 0.3 ,
vx : 0.3 ,
vxFloat : 0.6 ,
o : 0.3 ,
oFloat : 0.3 ,
});

效果图为:

2、彩带

window.canvasEffect({
ele : $("body")[0] ,
type : "band" ,
particleMax : 200 ,
particleCount : 100 ,
bandColorArr : ["#82F269", "#F61326", "#F6F313", "#518DF6"] ,
w : 8 ,
wFloat : 2 ,
h : 12 ,
hFloat : 4 ,
o : 0.4 ,
oFloat : 0.5 ,
r : 1 ,
rFloat : 2
});

效果图为:

3、雪花

window.canvasEffect({
ele : $("body")[0] ,
type : "snow" ,
particleMax : 200 ,
particleCount : 100 ,
o : 0.4 ,
oFloat : 0.5 ,
r : 1 ,
rFloat : 2
});

效果图为:

demo的下载放到csdn上去了,地址为

http://download.csdn.net/detail/hufengsky123/9337827

canvas基础学习(三)的更多相关文章

  1. Python入门基础学习 三

    Python入门基础学习 三 数据类型 Python区分整型和浮点型依靠的是小数点,有小数点就是浮点型. e记法:e就是10的意思,是一种科学的计数法,15000=1.5e4 布尔类型是一种特殊的整形 ...

  2. Python基础学习三

    Python基础学习三 1.列表与元组 len()函数:可以获取列表的元素个数. append()函数:用于在列表的最后添加元素. sort()函数:用于排序元素 insert()函数:用于在指定位置 ...

  3. Django基础学习三_路由系统

    今天主要来学习一下Django的路由系统,视频中只学了一些皮毛,但是也做下总结,主要分为静态路由.动态路由.二级路由 一.先来看下静态路由 1.需要在project中的urls文件中做配置,然后将匹配 ...

  4. canvas基础学习

    /** * Created by ty on 2016/7/11. * canvas 基础 */ window.onload = function() { var canvas = document. ...

  5. canvas一周一练 -- canvas基础学习

    从上个星期开始,耳朵就一直在生病,里面长了个疙瘩,肿的一碰就疼,不能吃饭不能嗨 (┳_┳)……在此提醒各位小伙伴,最近天气炎热,一定要注意防暑上火,病来如山倒呀~ 接下来我正在喝着5块一颗的药学习ca ...

  6. canvas基础学习(二)

    一.图像绘制 canvas绘制图像的方法是ctx.drawImage();该方法有三种使用方式 1.ctx.drawImage(img,x,y); img是指图像对象,x和y分别是这个图像左上角在ca ...

  7. HTML5 <canvas> 基础学习

    HTML5 <canvas> 元素用于图形的绘制,通过脚本 (通常是JavaScript)来完成. <canvas> 标签只是图形容器,您必须使用脚本来绘制图形 创建一个画布( ...

  8. Java基础学习(三)—面向对象(上)

    一.理解面向对象       面向对象是一种思想,是基于面向过程而言的,就是说面向对象是将功能等通过对象来实现,将功能封装进对象之中,让对象去实现具体的细节:这种思想是将数据作为第一位,而方法或者说是 ...

  9. salesforce lightning零基础学习(三) 表达式的!(绑定表达式)与 #(非绑定表达式)

    在salesforce的classic中,我们使用{!expresion}在前台页面展示信息,在lightning中,上一篇我们也提及了,如果展示attribute的值,可以使用{!v.expresi ...

随机推荐

  1. openstack ocata版(脚本)控制节点安装

    一.初始化环境: 1.更换yum源: yum install -y wget mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS- ...

  2. Django 之models进阶操作

    到目前为止,当我们的程序涉及到数据库相关操作时,我们一般都会这么搞: 创建数据库,设计表结构和字段 使用 MySQLdb 来连接数据库,并编写数据访问层代码 业务逻辑层去调用数据访问层执行数据库操作 ...

  3. iOS oc 调用 swift

    如股票oc要调用swift里面的代码 需要包含固定这个头文件 项目名称 LiqunSwiftDemo-Swift.h #ProjectName#-Swift.h 固定的写法 swift 目的 是取代o ...

  4. Oracle索引(1)概述与创建索引

    索引是为了提高数据检索效率而创建的一种独立于表的存储结构,由Oracle系统自动进行维护. 索引的概述        索引是一种可选的与表或簇相关的数据库对象,能够为数据的查询提供快捷的存储路径,减少 ...

  5. Linux下SPI测试程序

    /** 说明:SPI通讯实现* 方式一: 同时发送与接收实现函数: SPI_Transfer()* 方式二:发送与接收分开来实现* SPI_Write() 只发送* SPI_Read() 只接收* 两 ...

  6. wifidog源码分析 - wifidog原理 tiger

    转:http://www.cnblogs.com/tolimit/p/4223644.html wifidog源码分析 - wifidog原理 wifidog是一个用于配合认证服务器实现无线网页认证功 ...

  7. UVA11383 Golden Tiger Claw

    题目 UVA11383 Golden Tiger Claw 做法 \(KM\)好题啊,满足所有边\(l(x)+l(y)≥w(x,y)\)(个人理解,如不对请及时留言),这样能满足\(\sum\limi ...

  8. curl扩展代码

    /** * * curl 支持post * @param string $base_url 基础链接 * @param array $query_data 需要请求的数据 * @param strin ...

  9. CSS3 3D旋转下拉菜单

    在线演示 本地下载

  10. NoSQL数据库memcache和redis区别

    在web后台发开面试中,经常会被问道memcache和redis的区别和使用情况. 其中memcache和redis都是基于内存存储的缓存系统,存储形式key--value键值对的形式. 区别: 1. ...