移动端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

相关文章

CSS place-items: center解析与用法详解

《CSSplace-items:center解析与用法详解》place-items:center;是一个强大的CSS简写属性,用于同时控制网格(Grid)和弹性盒(Flexbox)... place-items: center; 是一个强大的 css 简写属性,用于同时控制 网格(Grid) 和 弹性盒(F

CSS实现元素撑满剩余空间的五种方法

《CSS实现元素撑满剩余空间的五种方法》在日常开发中,我们经常需要让某个元素占据容器的剩余空间,本文将介绍5种不同的方法来实现这个需求,并分析各种方法的优缺点,感兴趣的朋友一起看看吧... css实现元素撑满剩余空间的5种方法 在日常开发中,我们经常需要让某个元素占据容器的剩余空间。这是一个常见的布局需求

CSS Anchor Positioning重新定义锚点定位的时代来临(最新推荐)

《CSSAnchorPositioning重新定义锚点定位的时代来临(最新推荐)》CSSAnchorPositioning是一项仍在草案中的新特性,由Chrome125开始提供原生支持需... 目录 css Anchor Positioning:重新定义「锚定定位」的时代来了! 什么是 Anchor Pos

CSS中的Static、Relative、Absolute、Fixed、Sticky的应用与详细对比

《CSS中的Static、Relative、Absolute、Fixed、Sticky的应用与详细对比》CSS中的position属性用于控制元素的定位方式,不同的定位方式会影响元素在页面中的布... css 中的 position 属性用于控制元素的定位方式,不同的定位方式会影响元素在页面中的布局和层叠关

HTML5 getUserMedia API网页录音实现指南示例小结

《HTML5getUserMediaAPI网页录音实现指南示例小结》本教程将指导你如何利用这一API,结合WebAudioAPI,实现网页录音功能,从获取音频流到处理和保存录音,整个过程将逐步... 目录1. html5 getUserMedia API简介1.1 API概念与历史1.2 功能与优势1.3

全面解析HTML5中Checkbox标签

《全面解析HTML5中Checkbox标签》Checkbox是HTML5中非常重要的表单元素之一,通过合理使用其属性和样式自定义方法,可以为用户提供丰富多样的交互体验,这篇文章给大家介绍HTML5中C... 在html5中,Checkbox(复选框)是一种常用的表单元素,允许用户在一组选项中选择多个项目。本

HTML5 搜索框Search Box详解

《HTML5搜索框SearchBox详解》HTML5的搜索框是一个强大的工具,能够有效提升用户体验,通过结合自动补全功能和适当的样式,可以创建出既美观又实用的搜索界面,这篇文章给大家介绍HTML5... html5 搜索框(Search Box)详解搜索框是一个用于输入查询内容的控件,通常用于网站或应用程

Python实现精准提取 PDF中的文本,表格与图片

《Python实现精准提取PDF中的文本,表格与图片》在实际的系统开发中,处理PDF文件不仅限于读取整页文本,还有提取文档中的表格数据,图片或特定区域的内容,下面我们来看看如何使用Python实... 目录安装 python 库提取 PDF 文本内容:获取整页文本与指定区域内容获取页面上的所有文本内容获取

CSS3中的字体及相关属性详解

《CSS3中的字体及相关属性详解》:本文主要介绍了CSS3中的字体及相关属性,详细内容请阅读本文,希望能对你有所帮助... 字体网页字体的三个来源:用户机器上安装的字体,放心使用。保存在第三方网站上的字体,例如Typekit和Google,可以link标签链接到你的页面上。保存在你自己Web服务器上的字

Python基于微信OCR引擎实现高效图片文字识别

《Python基于微信OCR引擎实现高效图片文字识别》这篇文章主要为大家详细介绍了一款基于微信OCR引擎的图片文字识别桌面应用开发全过程,可以实现从图片拖拽识别到文字提取,感兴趣的小伙伴可以跟随小编一... 目录一、项目概述1.1 开发背景1.2 技术选型1.3 核心优势二、功能详解2.1 核心功能模块2.