使用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 ...
随机推荐
- Java中文乱码解决方案
Java中文乱码解决方案 1.中文乱码解决方案,确保每个文件的默认编码是UTF-8 加入 URIEncoding="UTF-8" 代码中的设置 1>在se ...
- VS优化编译配置
在使用VS2010编译C++程序的时候,每次修改工程中的某一个文件,点击“生成-仅用于项目-仅生成**”时,往往都是整个工程都需要重新编译一遍.由于这个工程代码量太大,每次编译完成都需要将近10分钟左 ...
- [SoapUI] 在Test Step 下加Script Assertion,用 messageExchange 获取当前步骤的response content
//Get response content of the current request def response = messageExchange.getResponseContent() // ...
- Win10系统SQL数据库安装
Win10系统MySQL数据库安装 1. 安装文件下载 下载地址: https://downloads.mysql.com/archives/community/ 下载版本: mysql-8.0.15 ...
- Golang 之 Base62 编码
Base62 编码用62个可见字符来编码信息,也就是所谓的62进制,可用于缩短地址之类的.实现起来也很简单.当然,这个实现跟别人家的有可能不一样,反正自己能编能解就行. package main im ...
- Java Persistence with MyBatis 3(中文版) 第二章 引导MyBatis
MyBatis最关键的组成部分是SqlSessionFactory,我们可以从中获取SqlSession,并执行映射的SQL语句.SqlSessionFactory对象可以通过基于XML的配置信息或者 ...
- Swift语法快速索引
在WWDC的演示中就可以看出来Swift这个更接近于脚本的语言可以用更少的代码量完成和OC同样的功能.但是对于像我一样在战争中学习战争的同学们来说,天天抱着笨Swift Programming Lan ...
- 编写高质量代码改善C#程序的157个建议——建议156:利用特性为应用程序提供多个版本
建议156:利用特性为应用程序提供多个版本 基于如下理由,需要为应用程序提供多个版本: 应用程序有体验版和完整功能版. 应用程序在迭代过程中需要屏蔽一些不成熟的功能. 假设我们的应用程序共有两类功能: ...
- WorkFlow 工作流 学习笔记
传统ERP为制造业企业产供销人财物的管理提供了一整套优化企业资源利用,集物流.信息流.资金流为一体的现代化管理工具.但是它在过程集成和企业间集成方面存在不足.具体表现在: 1.传统ERP是一个面向功能 ...
- 设计模式8---适配器模式(Adapter)
1. 适配器模式简介 适配器模式(Adapter):将一个类的接口转换成客户希望的另外一个接口.Adapter 模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作. 适用场景: 1.已经存在 ...