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

效果图:

旋转

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

代码实现:

html

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

js代码

html 复制代码
<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(); // 创建一个img
              img.onload = () => {
                // 创建一个 canvas
                const 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

javascript 复制代码
  <script
      type="text/javascript"
      src="../../../common/js/pinchzoom.js"
    ></script>

css 样式:

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;
    }
相关推荐
bai27311 小时前
path.join('C:\a', '\b') 给我 C:\a\b,path.resolve 给 C:\b:Windows 上 51 行路径实测
javascript·后端
kyriewen11 小时前
我写的 12 个 AI Skill,三个月后 8 个在吃空饷
javascript·人工智能·程序员
北城笑笑12 小时前
Vue 112 ,下篇:前端 dist 还原成 Vue3 + Vite 项目( krpano 全景接入与页面适配实战,Vue2 → Vue3 )
前端·javascript·vue.js·3d·前端框架·vue·webgl
我叫黑大帅13 小时前
搜“张三”把备注也带出来了?不用加高级筛选,如何让一个搜索框实现「选项直达」!
前端·javascript·面试
KevinWang_13 小时前
iOS 开发计划(三) - 打印 JavaScript 调试日志
javascript
如夏倾城15 小时前
WAF (Web Application Firewall,Web应用防火墙)
前端·网络·安全·web安全·web app
图扑可视化15 小时前
基于图扑软件智慧制硅厂 Web SCADA 开发实践
前端·三维可视化·scada·hmi·组态软件
前端fighter16 小时前
2026年前端面试题总结(pdf版)
前端·pdf
谢小飞16 小时前
运维加了个参数,我们的网页挂掉了
前端·浏览器
CoderYanger17 小时前
Java EE 进阶:12.Spring AOP(第二弹)
java·前端·数据库·spring boot·spring·java-ee·mybatis