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

相关文章

Android使用ImageView.ScaleType实现图片的缩放与裁剪功能

《Android使用ImageView.ScaleType实现图片的缩放与裁剪功能》ImageView是最常用的控件之一,它用于展示各种类型的图片,为了能够根据需求调整图片的显示效果,Android提... 目录什么是 ImageView.ScaleType?FIT_XYFIT_STARTFIT_CENTE

关于MongoDB图片URL存储异常问题以及解决

《关于MongoDB图片URL存储异常问题以及解决》:本文主要介绍关于MongoDB图片URL存储异常问题以及解决方案,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐... 目录MongoDB图片URL存储异常问题项目场景问题描述原因分析解决方案预防措施js总结MongoDB图

python实现svg图片转换为png和gif

《python实现svg图片转换为png和gif》这篇文章主要为大家详细介绍了python如何实现将svg图片格式转换为png和gif,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录python实现svg图片转换为png和gifpython实现图片格式之间的相互转换延展:基于Py

使用Python从PPT文档中提取图片和图片信息(如坐标、宽度和高度等)

《使用Python从PPT文档中提取图片和图片信息(如坐标、宽度和高度等)》PPT是一种高效的信息展示工具,广泛应用于教育、商务和设计等多个领域,PPT文档中常常包含丰富的图片内容,这些图片不仅提升了... 目录一、引言二、环境与工具三、python 提取PPT背景图片3.1 提取幻灯片背景图片3.2 提取

Python实现图片分割的多种方法总结

《Python实现图片分割的多种方法总结》图片分割是图像处理中的一个重要任务,它的目标是将图像划分为多个区域或者对象,本文为大家整理了一些常用的分割方法,大家可以根据需求自行选择... 目录1. 基于传统图像处理的分割方法(1) 使用固定阈值分割图片(2) 自适应阈值分割(3) 使用图像边缘检测分割(4)

HTML5中的Microdata与历史记录管理详解

《HTML5中的Microdata与历史记录管理详解》Microdata作为HTML5新增的一个特性,它允许开发者在HTML文档中添加更多的语义信息,以便于搜索引擎和浏览器更好地理解页面内容,本文将探... 目录html5中的Mijscrodata与历史记录管理背景简介html5中的Microdata使用M

html5的响应式布局的方法示例详解

《html5的响应式布局的方法示例详解》:本文主要介绍了HTML5中使用媒体查询和Flexbox进行响应式布局的方法,简要介绍了CSSGrid布局的基础知识和如何实现自动换行的网格布局,详细内容请阅读本文,希望能对你有所帮助... 一 使用媒体查询响应式布局        使用的参数@media这是常用的

HTML5表格语法格式详解

《HTML5表格语法格式详解》在HTML语法中,表格主要通过table、tr和td3个标签构成,本文通过实例代码讲解HTML5表格语法格式,感兴趣的朋友一起看看吧... 目录一、表格1.表格语法格式2.表格属性 3.例子二、不规则表格1.跨行2.跨列3.例子一、表格在html语法中,表格主要通过< tab

Python结合PyWebView库打造跨平台桌面应用

《Python结合PyWebView库打造跨平台桌面应用》随着Web技术的发展,将HTML/CSS/JavaScript与Python结合构建桌面应用成为可能,本文将系统讲解如何使用PyWebView... 目录一、技术原理与优势分析1.1 架构原理1.2 核心优势二、开发环境搭建2.1 安装依赖2.2 验

C#实现将Excel表格转换为图片(JPG/ PNG)

《C#实现将Excel表格转换为图片(JPG/PNG)》Excel表格可能会因为不同设备或字体缺失等问题,导致格式错乱或数据显示异常,转换为图片后,能确保数据的排版等保持一致,下面我们看看如何使用C... 目录通过C# 转换Excel工作表到图片通过C# 转换指定单元格区域到图片知识扩展C# 将 Excel