使用canvas实现画中画效果的H5
最近看到一个挺有趣的H5,主要效果就是通过不断的放缩来展示画中画,网上找了一下并没有这方面的实现代码,故决定原创一下,并分享出来
主要的思路就是通过canvas不断的写入图片,考虑到每一层的图片的位置和大小不一样,于是通过最外层的图片来尺寸和位置来控制里面的图片,然后通过循环写入canvas的方式实现最终的效果。
需要注意的是使用canvas写入图片需要预加载图片,通过回调函数来写入,同时,由于图片加载需要一定的时候,所以一般的H5会有一个加载的过程,我也加上了一个回调函数来处理加载图片以后其他的操作,比如隐藏加载页面等操作。
此项目已经放到码云上了,传送门,第一次分享项目,共勉之。
主要代码如下:
let $ = require('jquery');
let tools = require('./utils.js');
$.fn.longPress = function() {
let timeout = undefined;
let start;
let beginHandel, endHandel;
let length = arguments.length;
beginHandel = arguments[0];
if (length = 2) {
endHandel = arguments[1];
}
$(this).on('touchstart', function(e) {
e.preventDefault();
timeout = setTimeout(beginHandel, 500);
});
$(this).on('touchend', function(e) {
e.preventDefault();
tools.execCB(endHandel);
clearTimeout(timeout);
});
};
let TWEEN = require('@tweenjs/tween.js');
(function(global, factory) {
if (typeof define === 'function' && define.amd) {
define(function() {
return factory(global, global.document);
})
} else if (typeof module !== 'undefined' && module.exports) {
module.exports = factory(global, global.document);
} else {
global.Pip = factory(global, global.document);
}
}(typeof window !== 'undefined' ? window : this, function(window, document) {
'use strict'
//常量
const CW = $(window).width();
const CH = $(window).height();
let container = $('#pip');
let Pip = function(options) {
let _this = this;
this.defaultOps = {
model: 'bts',
duration: 1000,
}
if (typeof options == 'object') {
Object.keys(options).map(function(key) {
_this.defaultOps[key] = options[key];
})
}
//创建canvas
let canvas = document.createElement('canvas');
let length = _this.defaultOps.content.length;
canvas.width = CW;
canvas.height = CH;
_this.defaultOps.context = canvas.getContext('2d');
container.append(canvas);
};
Pip.prototype.start = function(cb) {
let opts = this.defaultOps;
let ctx = opts.context;
let content = opts.content;
let length = content.length;
let model = opts.model;
let beginStatus = {};
let endStatus = {};
let i = 0;
if (ctx) {
// 加载所有的图片
loadimages(content, function(images) {
//去掉加载提示页面
tools.execCB(cb)
// 总时间
let duration = opts.duration;
let timestamp1 = 0
let timestamp2 = 0
// 补间动画
let tween = null
// 初始化状态
if (model == 'stb') {
i = length - 2;
let width = CW;
let height = CH;
let left = 0;
let top = 0;
for (let k = i + 1; k >= 0; k--) {
if (k !== i + 1) {
left = left + width * content[length - 1 - k].left;
top = top + height * content[length - 1 - k].top;
width = width * content[length - 1 - k].width;
height = height * content[length - 1 - k].height;
}
let image = images[length - 1 - k];
ctx.drawImage(image, left, top, width, height);
}
//定义开始和结束状态
endStatus = {
width: CW / content[length - 1 - i].width,
height: CH / content[length - 1 - i].height,
left: -CW / content[length - 1 - i].width * content[length - 1 - i].left,
top: -CH / content[length - 1 - i].height * content[length - 1 - i].top
}
beginStatus = {
width: CW,
height: CH,
left: 0,
top: 0
}
} else {
ctx.drawImage(images[length - 1 - i], 0, 0, container.width(), container.height());
//定义开始和结束状态
beginStatus = {
width: CW / content[length - 1 - i].width,
height: CH / content[length - 1 - i].height,
left: -CW / content[length - 1 - i].width * content[length - 1 - i].left,
top: -CH / content[length - 1 - i].height * content[length - 1 - i].top
}
endStatus = {
width: CW,
height: CH,
left: 0,
top: 0
}
}
function go() {
tween = new TWEEN.Tween(beginStatus)
.to(endStatus, duration)
.onUpdate(function() {
let _this = this;
ctx.clearRect(0, 0, container.width(), container.height());
let width = _this.width;
let height = _this.height;
let left = _this.left;
let top = _this.top;
for (let k = i + 1; k >= 0; k--) {
if (k !== i + 1) {
left = left + width * content[length - 1 - k].left;
top = top + height * content[length - 1 - k].top;
width = width * content[length - 1 - k].width;
height = height * content[length - 1 - k].height;
}
let image = images[length - 1 - k];
ctx.drawImage(image, left, top, width, height)
}
})
.onComplete(function() {
if (model == 'bts') {
i++;
} else {
i--;
}
if (i > length - 2 || i < 0) {
if (model == 'bts') {
i--;
model = 'stb';
endStatus = {
width: CW / content[length - 1 - i].width,
height: CH / content[length - 1 - i].height,
left: -CW / content[length - 1 - i].width * content[length - 1 - i].left,
top: -CH / content[length - 1 - i].height * content[length - 1 - i].top
};
beginStatus = {
width: CW,
height: CH,
left: 0,
top: 0
};
} else {
i++;
model = 'bts';
beginStatus = {
width: CW / content[length - 1 - i].width,
height: CH / content[length - 1 - i].height,
left: -CW / content[length - 1 - i].width * content[length - 1 - i].left,
top: -CH / content[length - 1 - i].height * content[length - 1 - i].top
};
endStatus = {
width: CW,
height: CH,
left: 0,
top: 0
};
}
return;
}
//定义开始和结束状态
if (model == 'bts') {
beginStatus = {
width: CW / content[length - 1 - i].width,
height: CH / content[length - 1 - i].height,
left: -CW / content[length - 1 - i].width * content[length - 1 - i].left,
top: -CH / content[length - 1 - i].height * content[length - 1 - i].top
};
endStatus = {
width: CW,
height: CH,
left: 0,
top: 0
};
} else {
endStatus = {
width: CW / content[length - 1 - i].width,
height: CH / content[length - 1 - i].height,
left: -CW / content[length - 1 - i].width * content[length - 1 - i].left,
top: -CH / content[length - 1 - i].height * content[length - 1 - i].top
};
beginStatus = {
width: CW,
height: CH,
left: 0,
top: 0
};
}
duration = opts.duration;
go();
})
.onStop(function() {
timestamp2 = new Date().getTime()
duration = duration - (timestamp2 - timestamp1)
if (duration < 0) {
duration = 0
}
})
.onStart(function() {
timestamp1 = new Date().getTime()
})
.start()
requestAnimationFrame(animate)
function animate() {
TWEEN.update()
requestAnimationFrame(animate)
}
}
container.longPress(function() {
go()
}, function() {
if (tween) {
tween.stop()
}
})
})
} else {
console.log("context is not defined")
return
}
function loadimages(content, cb) {
let images = [];
let num = 0;
let length = content.length;
for (let c in content) {
images[c] = new Image();
images[c].onload = function() {
if (num === length) {
num = 0;
tools.execCB(cb, images)
}
}
images[c].src = content[length - 1 - num].src;
num++;
}
}
};
return Pip
}));
用法如下:
var pip = new Pip({
content: [{
src: require('../src/images/2.jpg'),
top: 0,
left: 0,
width: 1,
height: 1
}, {
src: require('../src/images/3.png'),
top: .1,
left: .05,
width: .08,
height: .08
}, {
src: require('../src/images/4.jpg'),
top: .4,
left: .6,
width: .08,
height: .08
}, {
src: require('../src/images/5.jpg'),
top: .4,
left: .6,
width: .08,
height: .08
}, {
src: require('../src/images/6.jpg'),
top: .9,
left: .4,
width: .08,
height: .08
}, {
src: require('../src/images/7.jpg'),
top: .1,
left: .05,
width: .08,
height: .08
}, {
src: require('../src/images/8.jpg'),
top: .4,
left: .6,
width: .08,
height: .08
}, {
src: require('../src/images/9.jpg'),
top: .1,
left: .05,
width: .08,
height: .08
}, {
src: require('../src/images/4.jpg'),
top: .4,
left: .6,
width: .08,
height: .08
}, {
src: require('../src/images/5.jpg'),
top: .4,
left: .6,
width: .08,
height: .08
}, {
src: require('../src/images/6.jpg'),
top: .9,
left: .4,
width: .08,
height: .08
}],
model: 'stb',
});
pip.start(function() {
//此处加载完图片以后隐藏遮罩
$('.mask').hide();
});
使用canvas实现画中画效果的H5的更多相关文章
- 使用Canvas实现动画效果 | DKlogs -- 设计 | 生活
使用Canvas实现动画效果 | DKlogs -- 设计 | 生活 使用Canvas实现动画效果
- canvas实现倒计时效果示例(vue组件内编写)
前言: 此事例是在vue组件中,使用canvas实现倒计时动画的效果.其实,实现效果的逻辑跟vue没有关系,只要读懂canvas如何实现效果的这部分逻辑就可以了 canvas动画的原理:利用定时器,给 ...
- 原生js实现canvas气泡冒泡效果
说明: 本文章主要分为ES5和ES6两个版本 ES5版本是早期版本,后面用ES6重写优化的,建议使用ES6版本. 1, 原生js实现canvas气泡冒泡效果的插件,api丰富,使用简单2, 只需引入J ...
- canvas/CSS仪表盘效果
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- 基于canvas与原生JS的H5动画引擎
前一段时间项目组里有一些H5动画的需求,由于没有专业的前端人员,便交由我这个做后台的研究相关的H5动画技术. 通过初步调研,H5动画的实现大概有以下几种方式: 1.基于css实现 这种方式比较简单易学 ...
- canvas弹动效果
弹动效果,用物体与目标的距离乘上系数再累加至速度上,让物体呈加速度运动,再让速度乘与摩擦力系数,让物体最终停止运动 代码如下所示 var canvas = document.getElementByI ...
- 用Canvas制作剪纸效果
在做剪纸效果之前,先介绍剪纸效果运用到的一些知识: 1.阴影: 在Canvas之中进行绘制时,可以通过修改绘图环境中的如下4个属性值来指定阴影效果: shadowColor:CSS格式的颜色字串.默认 ...
- HTML5之Canvas时钟(网页效果--每日一更)
今天,带来的是使用HTML5中Canvas标签实现的动态时钟效果. 话不多说,先看效果:亲,请点击这里 众所周知,Canvas标签是HTML5中的灵魂,HTML5 Canvas是屏幕上的一个由Java ...
- 【HTML5】Canvas 实现放大镜效果
图片放大镜 效果 在线演示 源码 原理 首先选择图片的一块区域,然后将这块区域放大,然后再绘制到原先的图片上,保证两块区域的中心点一致, 如下图所示: 初始化 <canvas id=&qu ...
随机推荐
- centos6.5系统hadoop2.7安装sqoop
一.sqoop简介 Sqoop是一款开源的工具,主要用于在Hadoop(Hive)与传统的数据库(mysql.postgresql...)间进行数据的传递,可以将一个关系型数据库(例如 : MySQL ...
- Shiro 集成Spring 使用 redis时 使用redisTemplate替代jedisPool(五)
1.添加依赖架包: <dependency> <groupId>org.springframework.data</groupId> <artifactId& ...
- sqlserver 文件与文件组的使用和优化
文件和文件组填充策略 文件组对组内的所有文件都使用按比例填充策略.当数据写入文件组时,SQL Server 数据库引擎按文件中的可用空间比例将数据写入文件组中的每个文件,而不是将所有数据都写入 ...
- UEFI下win10+Ubuntu双启动后完全纯净卸载Ubuntu,重建BCD
以下内容操作具有风险,操作前请提前备份数据.建议由有丰富经验的人使用,需要掌握diskpart. 背景 使用ubuntu+win10 dual boot后,需要重置回纯净win10系统. BCD是Bo ...
- ui界面使用 DialogMonitorOPS 问题
-- 是类主要是实现对界面上元素的处理.实现效果的处理 struct gt_cl_hp_uiName ( fn help = ( gt_10000_help = " 类主要是实现对界面上元素 ...
- 【#】Spring3 MVC 注解(二)---@RequestMapping
博客分类: spring MVC 1 问题:有多个 @RequestMapping @controller @RequestMapping("/aaa") ...
- C#中 Thread,Task,Async/Await,IAsyncResult 的那些事儿![转载]
说起异步,Thread,Task,async/await,IAsyncResult 这些东西肯定是绕不开的,今天就来依次聊聊他们 1.线程(Thread) 多线程的意义在于一个应用程序中,有多个执行部 ...
- HDU 3361 ASCII (水题)
题意: 析:不说话. #include <cstdio> #include <string> #include <cstdlib> #include <cma ...
- 编写高质量代码改善C#程序的157个建议——建议70:避免在调用栈较低的位置记录异常
建议70:避免在调用栈较低的位置记录异常 并不是所有的异常都要被记录到日志,一类情况是异常发生的场景需要被记录,还有一类就是未被捕获的异常.未被捕获的异常通常被视为一个Bug,所以,对于它的记录,应该 ...
- 学习tomcat(一)----用IDEA调试tomcat源码
一直在使用tomcat,但却不怎么熟悉tomcat的"运作流程",今天就 参照参考文章进行了代码搭建(代码的github在文末),并修改了一些操作.学习下tomcat的" ...