当前位置: 首页 > news >正文

vue.js 原生js app端实现图片旋转、放大、缩小、拖拽

效果图:

旋转

放大:手机上可以双指放大缩小 拖拽

代码实现:

html

 <div id="home" class="" v-cloak><!-- 上面三个按钮 图片自己解决 --><div class="headImage" v-if="showBtn"><div class="leftPhoto" @click="leftPhoto()"><imgstyle="width: 2.2rem; height: 2.2rem"src="../../../common/image/left_xuanzhuan.png"alt=""/></div><div class="rightPhoto" @click="rightPhoto()"><imgstyle="width: 2.2rem; height: 2.2rem"src="../../../common/image/right_xuanzhuan.png"alt=""/></div><div class="comeBack" @click="comeBack()"><imgstyle="width: 2.4rem; height: 2.4rem"src="../../../common/image/fanhui.png"alt=""/></div></div><!-- 展示图片位置 --><div class="page"> <!--  缩放 --><div class="pinch-zoom"><imgv-show="showBtn"id="imageId"style="height: 95% !important; width: 100%; object-fit: contain":src="graphPhoto"/></div></div></div>

js代码

<script type="text/javascript">let vm = new Vue({el: "#home",data() {return {graphPhoto: "", //图片地址showBtn: false, //判断是否显示按钮};},methods: {// 返回comeBack() {// 根据逻辑返回就行},leftPhoto() {// 使用示例this.rotateBase64Image(this.graphPhoto, 90) // 旋转90度.then((rotatedBase64) => {this.graphPhoto = rotatedBase64;});},rightPhoto() {this.rotateBase64Image(this.graphPhoto, -90) // 旋转90度.then((rotatedBase64) => {this.graphPhoto = rotatedBase64;});},// 接口查询getPhoto() {let that = this;//.... 你的接口返回的数据 base 如果没有可以暂时用本地的图片代替一下if (base) {that.graphPhoto = "../../../common/image/WechatIMG624.jpg";that.showBtn = true; //是否展示按钮that.rotateBase64Image(that.graphPhoto, 0) // 默认调用 旋转0度.then((rotatedBase64) => {that.graphPhoto = rotatedBase64;});} else {that.showBtn = false;mui.confirm("未获取到图形", "提示", ["返回"], function (e) {if (e.index == 0) {//根据自己逻辑写就行 }});}});},rotateBase64Image(base64ImageData, degree) {let that = this;return new Promise((resolve, reject) => {const img = new Image(); // 创建一个imgimg.onload = () => {// 创建一个 canvasconst canvas = document.createElement("canvas");const ctx = canvas.getContext("2d");// 计算旋转后的尺寸const maxDim = Math.max(img.width, img.height);canvas.width = maxDim;canvas.height = maxDim;// 将图片绘制到canvas上,并旋转指定的角度ctx.translate(maxDim / 2, maxDim / 2);ctx.rotate((degree * Math.PI) / 180);ctx.drawImage(img,-img.width / 2,-img.height / 2,img.width,img.height);// 将旋转后的canvas转换回Base64编码的图片数据const rotatedBase64 = canvas.toDataURL("image/png");resolve(rotatedBase64);};img.onerror = () => {that.showBtn = false;mui.confirm("图形解析异常", "提示", ["返回"], function (e) {if (e.index == 0) {//根据自己逻辑写就行 }});};img.src = base64ImageData;});},},mounted() {this.getPhoto();},});
</script>
// 单独把双指放大缩小之类的引入 我用的jq 你可以直接用js获取
<script type="text/javascript">$(function () {$("div.pinch-zoom").each(function () {new RTP.PinchZoom($(this), {});});});
</script>

还需要引入缩放这个:具体文件需要的自提:

通过网盘分享的文件:pinchzoom.js
链接: https://pan.baidu.com/s/1p83enqDMmrNOHyH8W4gUtQ?pwd=ies7 提取码: ies7

  <scripttype="text/javascript"src="../../../common/js/pinchzoom.js"></script>

css 样式:

.headImage {margin-top: 1rem;margin-right: 0.5rem;margin-bottom: 1rem;display: flex;align-items: center;justify-content: flex-end;height: 3.8rem;}.headImage > div {width: 3.8rem;height: 3.8rem;border-radius: 0.6rem;margin: 0.4rem;background-color: #e8eff1;line-height: 2rem;text-align: center;}.page img {width: 100%;height: auto;}.page {height: 100%;width: 100%;}
// 这块比较重要 不设置的话 默认会根据你的宽或者高 生成一个正方形区域.pinch-zoom {height: 95% !important;width: 100% !important;}.page > div {height: 100% !important;width: 100% !important;}.leftPhoto,.rightPhoto,.comeBack {display: flex;align-items: center;justify-content: center;}

相关文章:

  • 服务器的地址如何伪装起来
  • PWM基础与信号控制
  • python 图像绘制问题: 使用turtle库绘制蟒蛇
  • RAG+Agent人工智能平台:RAGflow实现GraphRA知识库问答,打造极致多模态问答与AI编排流体验
  • 【Vue.js基础】
  • Maven笔记(一):基础使用【记录】
  • pyproject.toml文件相关
  • 【第二轮通知】第二届网络、通信与智能计算国际会议(NCIC 2024)
  • 三维扫描 | 解锁低成本、高效率的工作秘籍
  • 2024.9.26 作业 +思维导图
  • 【DP解密多重背包问题】:优化策略与实现
  • 零基础入门AI:一键本地运行各种开源大语言模型 - Ollama
  • [大语言模型-论文精读] ACL2024-长尾知识在检索增强型大型语言模型中的作用
  • el-table+el-form实现表单校验和解决不垂直居中导致的问题
  • IT技术的发展及应用
  • JavaScript-如何实现克隆(clone)函数
  • 2018以太坊智能合约编程语言solidity的最佳IDEs
  • Eureka 2.0 开源流产,真的对你影响很大吗?
  • Flex布局到底解决了什么问题
  • HashMap ConcurrentHashMap
  • java正则表式的使用
  • js写一个简单的选项卡
  • Making An Indicator With Pure CSS
  • PAT A1092
  • 阿里云应用高可用服务公测发布
  • 测试开发系类之接口自动化测试
  • 电商搜索引擎的架构设计和性能优化
  • 反思总结然后整装待发
  • 机器学习中为什么要做归一化normalization
  • 开发基于以太坊智能合约的DApp
  • 日剧·日综资源集合(建议收藏)
  • 在weex里面使用chart图表
  • 哈罗单车融资几十亿元,蚂蚁金服与春华资本加持 ...
  • (19)夹钳(用于送货)
  • (2024,Flag-DiT,文本引导的多模态生成,SR,统一的标记化,RoPE、RMSNorm 和流匹配)Lumina-T2X
  • (C)一些题4
  • (day6) 319. 灯泡开关
  • (poj1.3.2)1791(构造法模拟)
  • (Python) SOAP Web Service (HTTP POST)
  • (Spark3.2.0)Spark SQL 初探: 使用大数据分析2000万KF数据
  • (二) Windows 下 Sublime Text 3 安装离线插件 Anaconda
  • (二)pulsar安装在独立的docker中,python测试
  • (附源码)springboot掌上博客系统 毕业设计063131
  • (附源码)ssm基于web技术的医务志愿者管理系统 毕业设计 100910
  • (附源码)基于ssm的模具配件账单管理系统 毕业设计 081848
  • (十二)springboot实战——SSE服务推送事件案例实现
  • (一)spring cloud微服务分布式云架构 - Spring Cloud简介
  • (转)程序员疫苗:代码注入
  • (转)重识new
  • (轉)JSON.stringify 语法实例讲解
  • .Net 6.0--通用帮助类--FileHelper
  • .net 设置默认首页
  • .NET开源全面方便的第三方登录组件集合 - MrHuo.OAuth
  • ::
  • @Bean注解详解