vue3实现横屏签字

<template>
  <div id="app">
    <div style="background: #fff">
      <vue-signature-pad
        id="signature"
        width="95%"
        height="400px"
        ref="signaturePad"
        :options="options"
      />
    </div>
    <div v-for="(item, index) in imgList" :key="index">
      <img :src="item.src" alt="" width="100" />
    </div>
    <button @click="save">保存</button>
    <button @click="resume">重置</button>
  </div>
</template>

<script setup>
import { ref } from 'vue';

    const options=ref( {
        penColor: '#000',
      },)
      const signaturePad=ref(null)
      // const tu=ref(null)
      const imgList=ref([])
      const fileList=ref([])

  
  
    //将base64转换为文件
   const dataURLtoFile=(dataUrl,filename)=>{
    console.log(dataUrl,"路径",filename,"文件名");
    var arr = dataUrl.split(','),
        mime = arr[0].match(/:(.*?);/)[1],
        bstr = atob(arr[1]),
        n = bstr.length,
        u8arr = new Uint8Array(n)

      while (n--) {
        u8arr[n] = bstr.charCodeAt(n)
      }

      return new File([u8arr], filename, { type: mime })
    }

         // 通过canvas旋转图片
   const   rotateBase64Img=(src, edg, callback) =>{
      var canvas = document.createElement('canvas')
      var ctx = canvas.getContext('2d')

      var imgW //图片宽度
      var imgH //图片高度
      var size //canvas初始大小

      if (edg % 90 != 0) {
        console.error('旋转角度必须是90的倍数!')
        throw '旋转角度必须是90的倍数!'
      }
      edg < 0 && (edg = (edg % 360) + 360)
      const quadrant = (edg / 90) % 4 //旋转象限
      const cutCoor = { sx: 0, sy: 0, ex: 0, ey: 0 } //裁剪坐标

      var image = new Image()

      image.crossOrigin = 'anonymous'
      image.src = src

      image.onload = function () {
        imgW = image.width
        imgH = image.height
        size = imgW > imgH ? imgW : imgH

        canvas.width = size * 2
        canvas.height = size * 2
        switch (quadrant) {
          case 0:
            cutCoor.sx = size
            cutCoor.sy = size
            cutCoor.ex = size + imgW
            cutCoor.ey = size + imgH
            break
          case 1:
            cutCoor.sx = size - imgH
            cutCoor.sy = size
            cutCoor.ex = size
            cutCoor.ey = size + imgW
            break
          case 2:
            cutCoor.sx = size - imgW
            cutCoor.sy = size - imgH
            cutCoor.ex = size
            cutCoor.ey = size
            break
          case 3:
            cutCoor.sx = size
            cutCoor.sy = size - imgW
            cutCoor.ex = size + imgH
            cutCoor.ey = size + imgW
            break
        }

        ctx.translate(size, size)
        ctx.rotate((edg * Math.PI) / 180)
        ctx.drawImage(image, 0, 0)

        var imgData = ctx.getImageData(
          cutCoor.sx,
          cutCoor.sy,
          cutCoor.ex,
          cutCoor.ey
        )

        if (quadrant % 2 == 0) {
          canvas.width = imgW
          canvas.height = imgH
        } else {
          canvas.width = imgH
          canvas.height = imgW
        }
        ctx.putImageData(imgData, 0, 0)
        callback(canvas.toDataURL())
      }
    }
        //保存
        const save=()=>{
          const { isEmpty, data } = signaturePad.value.saveSignature()
        console.log(signaturePad.value);
        rotateBase64Img(data, 90, (res) => {
        console.log(res) // 旋转后的base64图片src
        fileList.value.push({
          file: dataURLtoFile(res, 'sign'),
          name: 'sign',
        })
        imgList.value.push({src:res})
      
      })



      

        // let res=dataURLtoFile(data,"demo")
        // console.log(res);
      }
     // //清除重置
     const  resume=()=> {
      signaturePad.value.clearSignature()
     
    }
  




</script>

<style  scoped>
body {
  padding: 0;
  margin: 0;
}
#app {
  width: 100vw;
  height: 100vh;
  background: #ececec;
}
</style>

简易效果

  • 10
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现强制横屏或强制竖屏,可以使用 Vue 3 Composition API 提供的 `watchEffect` 和 `onMounted` 等方法来监听屏幕方向变化,并通过修改样式来实现强制横屏或强制竖屏。 以下是一个简单的示例代码,供参考: ```html <template> <div class="fullscreen"> <div class="rotate-info" v-if="showRotateInfo">{{rotateInfo}}</div> <div class="content"> <!-- your content here --> </div> </div> </template> <script> import { ref, watchEffect, onMounted } from 'vue'; export default { setup() { const showRotateInfo = ref(false); const rotateInfo = ref(''); // 监听屏幕方向变化 watchEffect(() => { const orientation = window.orientation; if (orientation === 90 || orientation === -90) { // 横屏 showRotateInfo.value = true; rotateInfo.value = '请将手机竖过来'; } else if (orientation === 0 || orientation === 180) { // 竖屏 showRotateInfo.value = true; rotateInfo.value = '请将手机横过来'; } }); // 强制横屏或强制竖屏 onMounted(() => { const orientation = window.orientation; if (orientation === 90 || orientation === -90) { // 横屏 document.documentElement.style.transform = 'rotate(-90deg)'; document.documentElement.style.width = '100vh'; document.documentElement.style.height = '100vw'; document.documentElement.style.overflow = 'hidden'; } else if (orientation === 0 || orientation === 180) { // 竖屏 document.documentElement.style.transform = 'rotate(0deg)'; document.documentElement.style.width = '100vw'; document.documentElement.style.height = '100vh'; document.documentElement.style.overflow = 'hidden'; } }); return { showRotateInfo, rotateInfo }; } }; </script> <style> .fullscreen { width: 100%; height: 100%; position: fixed; top: 0; left: 0; } .rotate-info { width: 100%; height: 100%; display: flex; justify-content: center; align-items: center; font-size: 24px; color: #fff; background-color: rgba(0, 0, 0, 0.8); position: absolute; top: 0; left: 0; z-index: 999; } .content { width: 100%; height: 100%; padding: 20px; box-sizing: border-box; } </style> ``` 在上面的示例代码中,我们通过监听 `window.orientation` 属性来判断屏幕方向,然后根据屏幕方向来显示提示信息和修改样式。在 `onMounted` 钩子函数中,我们通过修改 `transform`、`width`、`height` 和 `overflow` 等样式来实现强制横屏或强制竖屏。 注意,由于不同浏览器的实现方式可能不同,上述代码的兼容性可能存在问题,需要在具体情况下进行测试和调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值