zoukankan      html  css  js  c++  java
  • canvas图片合并,多张图合并成一张图,图片与二维码图片合成

    <template>
      <div class="wap-poster" ref="imageWrapper" id="target" style=" 100%;">
        <canvas id="mycanvas" width="100%" height="100%" v-if="!infactQrCode" style="transform: scale(2);display: none"></canvas>
        <!-- 背景图1 -->
        <img  v-if="!infactQrCode"  src="../../../assets/imgs/posterbg.jpg" id="bgImg" alt="" style=" 100%;height:100%;">
        <!-- 背景图2 -->
        <img  v-if="!infactQrCode"  src="../../../assets/imgs/poster0.png" id="poster" alt="" style="position: absolute;">
        <!-- 二维码图片 -->
        <img  v-if="!infactQrCode" id="qrcode" :src="qrCodeImg" alt="" style="position: absolute;top:247px; 140px;height: 140px;display: none">
        <!-- canvas画图转成的图片 -->
        <img :src="infactQrCode" alt="" v-if="infactQrCode" style=" 100%;height:100%">
      </div>
    </template>
    
    <script>
    
    
      export default {
        name: 'Poster',
        data() {
          return {
            qrCodeImg: "",
            screenWidth:"",
            agentUserId:'',
            infactQrCode:"",
            curUrl:''
          }
        },
        created(){
          var host=window.location.href.slice(0,window.location.href.indexOf("/#/"));
          var url=this.$route.query.qrCodeImg;
          var curUrl=url.slice(url.indexOf('mp.weixin.qq.com')+16);
          this.curUrl=curUrl;
          this.qrCodeImg=host+''+curUrl;
        },
        methods: {
          drawImg(){
            let _self=this;
            var canvas = document.getElementById("mycanvas");
            var a = setInterval(() =>{
              // 重复获取
              canvas = document.getElementById("mycanvas");
              if(!canvas){
                return false
              } else {
                clearInterval(a);
                var context = canvas.getContext('2d');
                context.scale(2,2)
                var img = new Image();
                img.setAttribute('crossOrigin', 'anonymous');
                let bgUrl=document.getElementById("bgImg").src;
                img.src=bgUrl;
                img.onload = function(){
                  if(img.complete){
                    context.drawImage(img,0,0,window.screeWidth,window.screeHeight);
                    var img1 = new Image();
                    let bgUrl1=document.getElementById("poster");
                    img1.src=bgUrl1.src;
                    img1.setAttribute('crossOrigin', 'anonymous');
                    img1.onload = function(){
                      if(img1.complete){
                        var left =  (window.screeWidth-660*window.rateWidth)/2;                                       context.drawImage(img1,left,110*window.rateWidth,600*window.rateWidth,1100*window.rateHeight);
             
    
                        var img2 = new Image();
                        img2.src=document.getElementById("qrcode").src;
                        img2.crossOrigin="*";
                        img2.onload = function(){
                          if(img2.complete){
                            var left =  (window.screeWidth-290*window.rateWidth)/2;
    
                              var left =  (window.screeWidth-240*window.rateWidth)/2;
                              context.drawImage(img2,left,460*window.rateHeight,240*window.rateWidth,240*window.rateWidth);
                              var image = new Image();
                              _self.infactQrCode=canvas.toDataURL("image/png");
                          
                          }
                        }
                      }
                    }
                  }
                }
              }
            },1)
          },
        },
        mounted(){
          this.drawImg();
          const that = this;
          window.screeWidth=window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
          window.screeHeight= window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;
          var left =  (window.screeWidth-140)/2;
          var qrcode=document.getElementById("qrcode");
          qrcode.style.left=left+'px';
          this.screeWidth=window.screeWidth;
          var canvas = document.getElementById("mycanvas");
          window.rateWidth = window.screeWidth/ 750;
          window.rateHeight = window.screeHeight/1334;
          canvas.setAttribute("width",window.screeWidth*2);
          canvas.setAttribute("height",window.screeHeight*2);
    
          let bgUrl1=document.getElementById("poster");
          var left =  (window.screeWidth-600*window.rateWidth)/2;
          if(window.screeHeight>=812&&window.screeWidth<768){
            bgUrl1.style.left=left+"px";
            bgUrl1.style.top=200*window.rateWidth+"px";
            bgUrl1.style.width=600*window.rateWidth+"px";
            bgUrl1.style.height=890*window.rateHeight+"px";
          }else{
            bgUrl1.style.left=left+"px";
            bgUrl1.style.top=110*window.rateWidth+"px";
            bgUrl1.style.width=600*window.rateWidth+"px";
            bgUrl1.style.height=1100*window.rateHeight+"px";
          }
    
        },
      }
    </script>
    <style lang="scss">
      button {
        position: absolute;
        top: 10px;
      }
    </style>

    图片合成,多张图片的合成,通过canvas将多张图片及二维码合成一张图片,然后再转换成图片展示

    完整代码

    canvas画图主要代码

        drawImg(){
            //  window.screeWidth,window.screeHeight 适配比例
            let _self=this;
            var canvas = document.getElementById("mycanvas");
            var a = setInterval(() =>{
              // 重复获取
              canvas = document.getElementById("mycanvas");
              if(!canvas){
                return false
              } else {
                clearInterval(a);
                var context = canvas.getContext('2d');
                context.scale(2,2)
                var img = new Image();
                img.setAttribute('crossOrigin', 'anonymous');
                let bgUrl=document.getElementById("bgImg").src;
                img.src=bgUrl;
                img.onload = function(){
                  if(img.complete){
                    // 画第一张背景图
                    context.drawImage(img,0,0,window.screeWidth,window.screeHeight);
                    //画第二章背景图
                    var img1 = new Image();
                    let bgUrl1=document.getElementById("poster");
                    img1.src=bgUrl1.src;
                    img1.setAttribute('crossOrigin', 'anonymous');
                    img1.onload = function(){
                      if(img1.complete){
                        var left =  (window.screeWidth-660*window.rateWidth)/2;
                        drawImage("图片对象","x轴位置","y轴位置","图片的宽度","图片的高度")
                        context.drawImage(img1,left,110*window.rateWidth,600*window.rateWidth,1100*window.rateHeight);
                        //画二维码
                        var img2 = new Image();
                        img2.src=document.getElementById("qrcode").src;
                        img2.crossOrigin="*";
                        img2.onload = function(){
                          if(img2.complete){
                            var left =  (window.screeWidth-240*window.rateWidth)/2;
                            context.drawImage(img2,left,460*window.rateHeight,240*window.rateWidth,240*window.rateWidth);
                            // var image = new Image();
                            // 将两张背景图和一张二维码画完的canvas再转换成图片
                            _self.infactQrCode=canvas.toDataURL("image/png");
                          }
                        }
                      }
                    }
                  }
                }
              }
            },1)
          },
  • 相关阅读:
    分类与监督学习,朴素贝叶斯分类算法
    K-means算法应用:图片压缩
    聚类--K均值算法:自主实现与sklearn.cluster.KMeans调用
    numpy统计分布显示
    10.11作业numpy数据集练习
    9.29作业
    CAGradientlayer设置视图背景的渐变效果
    dyld: Library not loaded: @rpath/libswiftCore.dylib
    解读NSString之性能分析
    iOS UIButton超出父视图无法点击解决方法
  • 原文地址:https://www.cnblogs.com/shuihanxiao/p/13719182.html
Copyright © 2011-2022 走看看