移动端VUE对于PDF图片手势缩放和移动(结合hammer.js)

2023-10-27 18:40

本文主要是介绍移动端VUE对于PDF图片手势缩放和移动(结合hammer.js),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

最后的效果是这样的 ,关于PDF文件显示就交给后端了,因为这个项目需要显示电子章和后端生成的文字所以直接后端生成图片更省事。

首先第一个坑

直接引入hammer.js手势没触发

要用npm安装"vue2-hammer": "^2.1.2"

关于手势启用的方法   一定要放在mounted里而且PDF图片要已经返回,这里主要监听pan(移动)和pinch(缩放)

// 启动缩放功能setPinch() {let that = this;let oldScale = 1;let oBox = document.querySelector("#inHtml");//获取到需要缩放的元素let hammer = new Hammer(oBox);//设置移动的方向hammer.get("pan").set({direction: Hammer.DIRECTION_ALL});
//移动开始时  先计算限制的范围hammer.on("panstart", function(event) {clearTimeout(that.timerOut);that.showFixed = true;let minX = that.$refs.pdfBox.offsetWidth * that.nowScale * that.OriginX - that.$refs.pdfBox.offsetWidth * that.OriginX;let maxX = that.$refs.pdfBox.offsetWidth * that.nowScale - minX - that.$refs.pdfBox.offsetWidth;let minY = that.$refs.pdfBox.offsetHeight * that.nowScale * that.OriginY - that.$refs.pdfBox.offsetHeight * that.OriginY;let maxY = that.$refs.pdfBox.offsetHeight * that.nowScale - minY - ((that.$refs.pdfBox.offsetHeight / that.page_count) - 50);that.minY = minY;that.maxY = maxY;oBox.style.transitionDuration = "0s";hammer.on("panmove", function(event) {let x = 0;let y = 0;if (parseFloat(that.lastdeltaX + event.deltaX) > minX) {x = minX;} else if (parseFloat(that.lastdeltaX + event.deltaX) < -maxX) {x = -maxX;} else {x = parseFloat(that.lastdeltaX + event.deltaX);}if (parseFloat(that.lastdeltaY + event.deltaY) > minY) {y = minY;} else if (parseFloat(that.lastdeltaY + event.deltaY) < -maxY) {y = -maxY;} else {y = parseFloat(that.lastdeltaY + event.deltaY);}
//用transform 来进行元素的偏移oBox.style.transform = "translate(" + x + "px," + y + "px)" + "scale(" + that.nowScale + ")";});});
//移动结束后  计算velocityY手指滑动的速度  时间是500ms移动到最后的位置hammer.on("panend", function(event) {that.timerOut = setTimeout(() => {if (that.showFixed) {that.showFixed = false;}}, 3000);let y = 0;let x = 0;let minX = that.$refs.pdfBox.offsetWidth * that.nowScale * that.OriginX - that.$refs.pdfBox.offsetWidth * that.OriginX;let maxX = that.$refs.pdfBox.offsetWidth * that.nowScale - minX - that.$refs.pdfBox.offsetWidth;let minY = that.$refs.pdfBox.offsetHeight * that.nowScale * that.OriginY - that.$refs.pdfBox.offsetHeight * that.OriginY;let maxY = that.$refs.pdfBox.offsetHeight * that.nowScale - minY - ((that.$refs.pdfBox.offsetHeight / that.page_count) - 50);that.minY = minY;that.maxY = maxY;that.lastdeltaX = parseFloat(that.lastdeltaX + event.deltaX);that.lastdeltaY = parseFloat(that.lastdeltaY + event.deltaY);let distance = event.velocityY * 500;oBox.style.transitionDuration = "500ms";if (that.lastdeltaX > minX) {x = minX;} else if (that.lastdeltaX < -maxX) {x = -maxX;} else {x = that.lastdeltaX;}if (that.lastdeltaY + distance > minY) {y = minY;} else if (that.lastdeltaY + distance < -maxY) {y = -maxY;} else {y = that.lastdeltaY + distance;}oBox.style.transform = "translate(" + x + "px," + y + "px)" + "scale(" + that.nowScale + ")";that.lastdeltaY = y;that.lastdeltaX = x;});
//开启缩放hammer.get("pinch").set({enable: true});
//缩放开始时计算缩放的原点transformOriginhammer.on("pinchstart", function(event) {oldScale = that.nowScale || 1;oBox.style.transitionDuration = "200ms";oBox.style.transitionTimingFunction = "cubic-bezier(0.23, 1, 0.32, 1)";that.OriginX = Math.abs(event.center.x - that.lastdeltaX) / (that.$refs.pdfBox.offsetWidth);that.OriginY = Math.abs(event.center.y - that.lastdeltaY) / (that.$refs.pdfBox.offsetHeight);oBox.style.transformOrigin = Math.abs(event.center.x - that.lastdeltaX) / (that.$refs.pdfBox.offsetWidth) * 100 + "%" + " "+ Math.abs(event.center.y - that.lastdeltaY) / (that.$refs.pdfBox.offsetHeight) * 100 + "%";
//限制缩放范围是2.5~1hammer.on("pinchmove", function(event) {if (2.5 > oldScale * event.scale && oldScale * event.scale > 0.9) {that.nowScale = oldScale * event.scale;oBox.style.transform = "translate(" + parseFloat(that.lastdeltaX) + "px," + parseFloat(that.lastdeltaY) + "px)" + "scale(" + oldScale * event.scale + ")";}});});hammer.on("pinchend", function(event) {oBox.style.transitionDuration = "0s";if (oldScale * event.scale < 1) {that.nowScale = 1;oBox.style.transform = "translate(" + parseFloat(that.lastdeltaX) + "px," + parseFloat(that.lastdeltaY) + "px)" + "scale(" + 1 + ")";}if (oldScale * event.scale > 2) {that.nowScale = 2;oBox.style.transform = "translate(" + parseFloat(that.lastdeltaX) + "px," + parseFloat(that.lastdeltaY) + "px)" + "scale(" + 2 + ")";}});},
<div id="inHtml" ref="pdfBox" v-if="!signSuccess && !signWait"><div class="pdf-container"><div class="pdf-box"><div class="pdfPage_1yRne" v-for="page in contract_doc_imgs" :key="page.page"><img :src="page.imgData" class="pdf-item" :id="'the-canvas'+page.page" alt=""><!--<canvas class="pdf-item" :id="'the-canvas'+page"></canvas>--><div @click="clearAll" class="dragLayer_3ccsq" :id="'can'+ page.page"></div></div></div><div class="componentSign"><p class="sealCompInfo_3v9iQ"><span class="need">*</span><span><span>甲方</span><em>(<span>签署区</span>)</em></span></p><div class="itemConentStyle_2MWEL"><div class="infoMsg_3NkPg">签署区</div><i class="iconfont icon-pm_auction_success"></i></div><div class="bubble top"><div id="field-change" class="bubble-item">换章</div></div></div></div></div>
#inHtml {position: absolute;left: 0;width: 100%;/*border: 1px solid red;*/.arrow {padding-top: 1rem;display: flex;}p {flex: 1;}.link {flex: 1;}.pdfPage_1yRne {position: relative;}.dragLayer_3ccsq {position: absolute;left: 0;top: 0;right: 0;bottom: 0;}.pdf-item {width: 100%;}}

 

这篇关于移动端VUE对于PDF图片手势缩放和移动(结合hammer.js)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/287599

相关文章

JS+HTML实现在线图片水印添加工具

《JS+HTML实现在线图片水印添加工具》在社交媒体和内容创作日益频繁的今天,如何保护原创内容、展示品牌身份成了一个不得不面对的问题,本文将实现一个完全基于HTML+CSS构建的现代化图片水印在线工具... 目录概述功能亮点使用方法技术解析延伸思考运行效果项目源码下载总结概述在社交媒体和内容创作日益频繁的

前端CSS Grid 布局示例详解

《前端CSSGrid布局示例详解》CSSGrid是一种二维布局系统,可以同时控制行和列,相比Flex(一维布局),更适合用在整体页面布局或复杂模块结构中,:本文主要介绍前端CSSGri... 目录css Grid 布局详解(通俗易懂版)一、概述二、基础概念三、创建 Grid 容器四、定义网格行和列五、设置行

Node.js 数据库 CRUD 项目示例详解(完美解决方案)

《Node.js数据库CRUD项目示例详解(完美解决方案)》:本文主要介绍Node.js数据库CRUD项目示例详解(完美解决方案),本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考... 目录项目结构1. 初始化项目2. 配置数据库连接 (config/db.js)3. 创建模型 (models/

前端下载文件时如何后端返回的文件流一些常见方法

《前端下载文件时如何后端返回的文件流一些常见方法》:本文主要介绍前端下载文件时如何后端返回的文件流一些常见方法,包括使用Blob和URL.createObjectURL创建下载链接,以及处理带有C... 目录1. 使用 Blob 和 URL.createObjectURL 创建下载链接例子:使用 Blob

使用Node.js制作图片上传服务的详细教程

《使用Node.js制作图片上传服务的详细教程》在现代Web应用开发中,图片上传是一项常见且重要的功能,借助Node.js强大的生态系统,我们可以轻松搭建高效的图片上传服务,本文将深入探讨如何使用No... 目录准备工作搭建 Express 服务器配置 multer 进行图片上传处理图片上传请求完整代码示例

Vuex Actions多参数传递的解决方案

《VuexActions多参数传递的解决方案》在Vuex中,actions的设计默认只支持单个参数传递,这有时会限制我们的使用场景,下面我将详细介绍几种处理多参数传递的解决方案,从基础到高级,... 目录一、对象封装法(推荐)二、参数解构法三、柯里化函数法四、Payload 工厂函数五、TypeScript

Android实现打开本地pdf文件的两种方式

《Android实现打开本地pdf文件的两种方式》在现代应用中,PDF格式因其跨平台、稳定性好、展示内容一致等特点,在Android平台上,如何高效地打开本地PDF文件,不仅关系到用户体验,也直接影响... 目录一、项目概述二、相关知识2.1 PDF文件基本概述2.2 android 文件访问与存储权限2.

基于Python实现高效PPT转图片工具

《基于Python实现高效PPT转图片工具》在日常工作中,PPT是我们常用的演示工具,但有时候我们需要将PPT的内容提取为图片格式以便于展示或保存,所以本文将用Python实现PPT转PNG工具,希望... 目录1. 概述2. 功能使用2.1 安装依赖2.2 使用步骤2.3 代码实现2.4 GUI界面3.效

Spring Boot项目中结合MyBatis实现MySQL的自动主从切换功能

《SpringBoot项目中结合MyBatis实现MySQL的自动主从切换功能》:本文主要介绍SpringBoot项目中结合MyBatis实现MySQL的自动主从切换功能,本文分步骤给大家介绍的... 目录原理解析1. mysql主从复制(Master-Slave Replication)2. 读写分离3.

Python实现AVIF图片与其他图片格式间的批量转换

《Python实现AVIF图片与其他图片格式间的批量转换》这篇文章主要为大家详细介绍了如何使用Pillow库实现AVIF与其他格式的相互转换,即将AVIF转换为常见的格式,比如JPG或PNG,需要的小... 目录环境配置1.将单个 AVIF 图片转换为 JPG 和 PNG2.批量转换目录下所有 AVIF 图