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;
    }
相关推荐
@大迁世界8 小时前
Firefox 本地模式:localhost 再见了 ?
前端·架构
米米商聊10 小时前
同名附件为什么被 Map 覆盖?重复键与一对多索引
前端·javascript
echome88811 小时前
Python 异常处理:try、else、finally 的正确搭配
开发语言·前端·python·异常处理·编程技巧
时间的情敌11 小时前
OpenLayers 实战|Vue3 + OpenLayers 完整工程案例(加载天地图、要素、弹窗、绘制、WFS)
前端·openlayers
陈随易11 小时前
下载神器!Motrix 停更三年后重写回归
前端·后端·程序员
前端的日常11 小时前
一个人+AI做情侣食谱小程序,0 费用的服务器
前端·后端
计算机魔术师12 小时前
谷歌搜索广告踩刹车,AI广告成为救命稻草
前端
总要学点什么12 小时前
第81篇:数据可视化-PM2.5分布——让圆点的胖瘦替每座城市报出浓度
前端·信息可视化·echarts·cesium·空间分析
IT_陈寒12 小时前
Vue的v-for里用了个临时变量,页面直接挂了
前端·人工智能·后端
hunter_07612 小时前
一次 iOS 真机签名 409 的完整排查:证书、设备、描述文件为什么总是"对不上"
前端