H5头像完整制作,可拖拽缩放,可添加装饰图标(装饰图标支持缩放、旋转、拖拽)

H5头像制作类的程序本以为到处都可以搜索到,可是真写这个项目的时候才发现太困难了,因为根本没有现成的功能模块可以拼凑,着实费了不少力气,好在最终圆满交工。

还是先看效果

在这里插入图片描述

源码获取渠道

小程序内联系客服
在这里插入图片描述

项目中功能点

看完效果是不是觉得实现还不错!!!
1、伪加载效果
2、上传图片后支持对图片的拖拽、缩放编辑
3、编辑后裁剪
4、裁剪后可添加装饰图标
5、装饰图标支持缩放、拖拽、旋转、取消等功能
6、摆放好图标后生成最终头像
7、对UI设计的还原

部分代码

代码量太大,只贴部分了,如果你需要整个源码,可

添加wx:New_SevenHan
<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width,initial-scale=1.0">
  <title>头像制作</title>
  <style>
    html, body {
      margin: 0;
      padding: 0;
    }

    .dome {
      display: flex;
      justify-content: flex-start;
      flex-direction: column;
      align-items: center;
      background-color: #feb9cb;
      width: 100%;
      height: 100%;
    }

    .row {
      width: 100%;
      display: flex;
      flex-direction: row;
      justify-content: center;
      align-items: center;
    }

    .uploadContainer {
      height: 50px;
      /*line-height: 80px;*/
      /* 切记position: relative一点要有 */
      position: relative;
      overflow: hidden;
      text-align: center;
    }

    .uploadImg {
      position: absolute;
      height: 50px;
      left: 0;
      top: 0;
    }

    .makeAva {
      height: 50px;
    }

    .cropper {
      width: 100%;
      /*line-height: 80px;*/
      /* 切记position: relative一点要有 */
      position: relative;
      overflow: hidden;
      text-align: center;
    }

    .img {
      position: absolute;
      width: 100%;
      height: 100%;
      left: 0;
      top: 0;
    }

    .canvas {
      position: absolute;
      width: 100%;
      height: 100%;
      left: 0;
      top: 0;
    }

    .column{
      width: 100%;
      display: flex;
      flex-direction: column;
      justify-content: flex-start;
      align-items: center;
    }
    .ornamentItem{
      width: 65px;
      position: relative;
      /*display: flex;*/
      /*justify-content: center;*/
      /*align-items: center;*/
      /*flex-direction: row;*/
      border-radius: 10px;
      margin: 0 5px;
      list-style:none; display:inline-block;
    }
    .loader{
      width: 100%;
      position: fixed;
      z-index: 9999;
      background-color: #feb9cb;
      overflow:hidden;
    }
    .heartHat{
      animation: heartHat 1.2s ease-in-out 2.8s infinite alternate;
    }

    @keyframes heartHat {
      from {
        transform: translate(0, 0) ;
      }
      to {
        transform: translate(6px, 6px)
      }
    }

    .heartPower{
      animation: heartPower 1.5s ease-in-out 2.3s infinite alternate;
    }

    @keyframes heartPower {
      from {
        transform: translate(0, 0)
      }
      to {
        transform: translate(6px, 0)
      }
    }
    .heartHand{
      animation: heartHand 1s ease-in-out 2.3s infinite alternate;
    }

    @keyframes heartHand {
      from {
        transform: translate(0, 0)
      }
      to {
        transform: translate(0, 10px)
      }
    }
    .heartGlasses{
      animation: heartGlasses 1.3s ease-in-out 2.7s infinite alternate;
    }

    @keyframes heartGlasses {
      from {
        transform: translate(0, 0) rotate(-30deg)
      }
      to {
        transform: translate(0, 6px) rotate(-30deg)
      }
    }
    .previewContaier{
      width: 100%;
      background-color: black;
      /*line-height: 80px;*/
      /* 切记position: relative一点要有 */
      position: fixed;
      overflow: hidden;
      text-align: center;
      z-index: 1000;
    }
    .previewImage{
      position: absolute;
      width: 100%;
      left: 0;
      top: 0;
    }
    .previewTip{
      color: white;
      width: 100%;
      padding-top: 30px;
    }
    .previewCancel{
      width: 100%;
    }

    .wave{
      width: 100%;

    @keyframes wave {
      from {
        transform: translate(0, 0)
      }
      to {
        transform: translate(0, -60px)
      }
    }

    .box { width:100%;white-space: nowrap;overflow-x: auto;-webkit-overflow-scrolling:touch; }		/*注释1*/
    .box::-webkit-scrollbar { width:0; height:0; display: none; }     /*注释2*/
  </style>
</head>

<body>



<div class="loader" style="background-color: #feb9cb;" id="loader">
  <img src="../../images/wave.png" id="wave" style="display:none;height:500px;position: fixed;z-index: 10001;"/>
  <img src="../../images/loadingBg.png" id="loadingBg" style="width:100%;height:100%;position: fixed;z-index: 10001;left: 0;top:0;" />
  <div style="width:100%;text-align:center;color:white;font-size: 28px;font-weight:600;position: fixed;z-index: 10001;display:flex;justify-content: center;align-items: center;flex-direction: row " id="loadingText">
    <img src="../../images/loadingstart.png" id="loadingstart">
    <div id="loadingText1">0%</div>
  </div>
</div>
<audio id="music" autoplay preload src="../../images/music.mp3" loop></audio>



<div class="dome" id="app">
  <div class="previewContaier" id="previewContainer"  style="display: none">
    <div class="previewCancel" id="previewCancel" @click="cancelPreview()"></div>
    <img class="previewImage" id="allImg" />
    <div class="previewTip" id="previewTip" @click="cancelPreview()">长按图片保存到本地</div>
  </div>


  <div class="cropper" id="cropper">
    <canvas  id="puzzleAllCanvas" class="canvas"></canvas>
    <canvas  id="puzzleCanvas" class="canvas"></canvas>
    <canvas  id='cvs'  class="canvas" ></canvas>
<!--    <img src="../../images/flag.png" class="flag" id="flag"/>-->
<!--    <img src="../../images/logo.png" class="logo" id="logo"/>-->
    <img src="../../images/hat1.png" id="heartHat" class="heartHat" style="position:absolute"/>
    <img src="../../images/power1.png" id="heartPower" class="heartPower" style="position:absolute"/>
    <img src="../../images/hand1.png" id="heartHand" class="heartHand" style="position:absolute"/>
    <img src="../../images/glasses1.png" id="heartGlasses" class="heartGlasses" style="position:absolute;transform: rotate(-30deg)"/>

  </div>
  <div style='display: none'>
    <img id='jewelry' src='' alt=''/>
    <img class='glasses' id='glasses' src='../../images/glasses1.png'/>
    <img class='power' id='power' src='../../images/power1.png'/>
    <img class='hat' id='hat' src='../../images/hat1.png'/>
    <img class='hand' id='hand' src='../../images/hand1.png'/>
    <img class='pic01' id='pic01' src='../../images/pic01.png'/>
    <img class='pic02' id='pic02' src='../../images/pic02.png'/>
    <img class='pic03' id='pic03' src='../../images/pic03.png'/>
    <img class='pic04' id='pic04' src='../../images/pic04.png'/>
    <img class='pic05' id='pic05' src='../../images/pic05.png'/>
    <img class='pic06' id='pic06' src='../../images/pic06.png'/>
    <img class='pic07' id='pic07' src='../../images/pic07.png'/>
    <img class='pic08' id='pic08' src='../../images/pic08.png'/>
  </div>
  <div v-if="!isAdded" class="column" id="operationArea1" style="">

    <img :src="slogan" class="slogan" id="slogan"/>
    <div  class="uploadContainer"  id="upload">
      <img :src="uploadImg" class="uploadImg" id="uploadImg"/>
      <!-- option是配置,格式是对象,getbase64Data是组件的一个方法获取裁剪完的头像 -->
      <h5-cropper id="uploadCropper" @getbase64="getbase64Data" @getblob="getBlob"
                  @get-file="getFile"></h5-cropper>
    </div>

  </div>

  <div v-if="isAdded"  class="column" id="operationArea2" style="background-color: #3f282e;">
    <div class="box"  id="operationArea2Row"  style="">
      <div class="ornamentItem" id="ornamentItem1" style="margin-left: 20px" @click="changeGlasses()" >
        <img src="../../images/item1.png" style="width: 100%;position: absolute"/>
        <img src="../../images/glasses1.png" style="width: 100%;position: absolute"/>
      </div>

      <div class="ornamentItem" id="ornamentItem2" @click="changePower()">
        <img src="../../images/item1.png" style="width: 100%;position: absolute"/>
        <img src="../../images/power1.png" style="width: 100%;position: absolute"/>
      </div>

      <div class="ornamentItem" id="ornamentItem3" @click="changeHand()" >
        <img src="../../images/item1.png" style="width: 100%;position: absolute"/>
        <img src="../../images/hand1.png" style="width: 100%;position: absolute"/>
      </div>

      <div class="ornamentItem" id="ornamentItem4" @click="changeHat()" >
        <img src="../../images/item1.png" style="width: 100%;position: absolute"/>
        <img src="../../images/hat1.png" style="width: 100%;position: absolute"/>
      </div>

      <div class="ornamentItem" id="ornamentItem5" @click="changePic01()" >
        <img src="../../images/item1.png" style="width: 100%;position: absolute"/>
        <img src="../../images/pic01.png" style="width: 100%;position: absolute"/>
      </div>

      <div class="ornamentItem" id="ornamentItem6" @click="changePic02()">
        <img src="../../images/item1.png" style="width: 100%;position: absolute"/>
        <img src="../../images/pic02.png" style="width: 100%;position: absolute"/>
      </div>

      <div class="ornamentItem" id="ornamentItem7" @click="changePic03()" >
        <img src="../../images/item1.png" style="width: 100%;position: absolute"/>
        <img src="../../images/pic03.png" style="width: 100%;position: absolute"/>
      </div>

      <div class="ornamentItem" id="ornamentItem8" @click="changePic04()" >
        <img src="../../images/item1.png" style="width: 100%;position: absolute"/>
        <img src="../../images/pic04.png" style="width: 100%;position: absolute"/>
      </div>

      <div class="ornamentItem" id="ornamentItem9" @click="changePic05()" >
        <img src="../../images/item1.png" style="width: 100%;position: absolute"/>
        <img src="../../images/pic05.png" style="width: 100%;position: absolute"/>
      </div>

      <div class="ornamentItem" id="ornamentItem10" @click="changePic06()">
        <img src="../../images/item1.png" style="width: 100%;position: absolute"/>
        <img src="../../images/pic6.png" style="width: 100%;position: absolute"/>
      </div>

      <div class="ornamentItem" id="ornamentItem11" @click="changePic07()" >
        <img src="../../images/item1.png" style="width: 100%;position: absolute"/>
        <img src="../../images/pic7.png" style="width: 100%;position: absolute"/>
      </div>

      <div class="ornamentItem" id="ornamentItem12" @click="changePic08()" style="margin-right: 20px">
        <img src="../../images/item1.png" style="width: 100%;position: absolute"/>
        <img src="../../images/pic8.png" style="width: 100%;position: absolute"/>
      </div>

    </div>
    <div class="row" id="operationArea2Row1" >

      <div class="uploadContainer" id="reUploadContainer">
        <img :src="reUpload" class="uploadImg" id="reUploadImg"/>
        <!-- option是配置,格式是对象,getbase64Data是组件的一个方法获取裁剪完的头像 -->
        <h5-cropper id="reUploadCropper"  @getbase64="getbase64Data" @getblob="getBlob"
                    @get-file="getFile"></h5-cropper>
      </div>
      <img :src="makeAva" class="makeAva" id="makeAva" style="margin-left: 15px" @click="previewImg()"/>
    </div>
  </div>
</div>

<script>

  new Vue({
    el: '#app',
    data: function () {
      return {
        // img: "../../images/oriImg.jpg",
        ratio:0,
        img: "http://geren.yi-school.top/images/logo.png",
        imgModal: "../../images/modal.png",
        slogan: "../../images/slogan.png",
        uploadImg: "../../images/uploadImg.png",
        reUpload: "../../images/reUpload.png",
        makeAva: "../../images/makeAva.png",
        isAdded: false,
        isShowPuzzle:true,
        puzzleCanvas:document.getElementById('puzzleCanvas'),
        cvs:document.getElementById("cvs"),
        cxt:document.getElementById("cvs").getContext("2d"),
        screenWidth: window.screen.width < 500 ? window.screen.width : 300,
        canvasFabric: {},
        isInitCanvasFabric:false,
        glassesInstance: {},
        glasses: "glasses",
        isGlassesAdded:false,
        powerInstance: {},
        power:'power',
        isPowerAdded:false,
        hatInstance: {},
        hat:'hat',
        isHatAdded:false,
        handInstance: {},
        hand:'hand',
        isHandAdded:false,
        pic01Instance: {},
        pic01: "pic01",
        isPic01Added:false,
        pic02Instance: {},
        pic02: "pic02",
        isPic02Added:false,

        pic03Instance: {},
        pic03: "pic03",
        isPic03Added:false,

        pic04Instance: {},
        pic04: "pic04",
        isPic04Added:false,


        pic05Instance: {},
        pic05: "pic05",
        isPic05Added:false,

        pic06Instance: {},
        pic06: "pic06",
        isPic06Added:false,

        pic07Instance: {},
        pic07: "pic07",
        isPic07Added:false,

        pic08Instance: {},
        pic08: "pic08",
        isPic08Added:false,
        loadingNum:0,
        waveLeft:0,
      }
    },

    beforeCreate: function () {
    },
    created: function () {
      this.ratio = document.body.clientWidth/750
      this.autoPlayAudio();
      document.getElementById("loadingstart").style.height = 56*this.ratio+"px"
      document.getElementById("wave").style.height = (1000*this.ratio)+"px"
      document.getElementById("loadingText").style.fontSize = (50*this.ratio)+"px"
      document.getElementById("wave").style.top = window.innerHeight+"px"
      document.getElementById("loadingBg").style.width =  document.body.clientWidth+"px"
      document.getElementById("loadingBg").style.height =  window.innerHeight+"px"
      document.getElementById('loadingBg').onload= function () {
        document.getElementById('wave').style.display = "block"
      }
      document.getElementById("loadingText").style.top = (window.innerHeight*3/5+10)+"px"
      this.timer = setInterval(this.startLoading, 15)
      document.getElementById("cropper").style.height = document.body.clientWidth + "px"
      // document.getElementById("flag").style.height = 56*this.ratio+"px"
      // document.getElementById("flag").style.top = 70*this.ratio+"px"
      // document.getElementById("logo").style.height = 147*this.ratio + "px"
      // document.getElementById("logo").style.top = 40*this.ratio + "px"
      // document.getElementById("logo").style.right = 40*this.ratio+"px"
      document.getElementById('heartHat').style.width = 130*this.ratio+"px"
      document.getElementById("heartHat").style.right = 310*this.ratio+"px"
      document.getElementById("heartHat").style.top = 160*this.ratio+"px"
      document.getElementById('heartGlasses').style.width = 130*this.ratio+"px"
      document.getElementById("heartGlasses").style.left = 120*this.ratio+"px"
      document.getElementById("heartGlasses").style.top = 280*this.ratio+"px"
      document.getElementById('heartHand').style.width = 130*this.ratio+"px"
      document.getElementById("heartHand").style.right = 30*this.ratio+"px"
      document.getElementById("heartHand").style.bottom = 50*this.ratio+"px"
      document.getElementById('heartPower').style.width = 130*this.ratio+"px"
      document.getElementById("heartPower").style.left = 40*this.ratio+"px"
      document.getElementById("heartPower").style.bottom = 40*this.ratio+"px"
      document.getElementById("slogan").style.height = 237*this.ratio+"px"
      document.getElementById("slogan").style.marginTop = 20*this.ratio+"px"
      document.getElementById("upload").style.marginTop = 20*this.ratio+"px"
      document.getElementById('uploadCropper').style.width = 411*this.ratio+"px"
      document.getElementById("uploadCropper").style.height = 100*this.ratio+"px"
      document.getElementById("uploadImg").style.height = 100*this.ratio+"px"
      document.getElementById("operationArea2Row").style.marginTop = 30*this.ratio+"px"
      document.getElementById("ornamentItem1").style.width = document.getElementById("ornamentItem1").style.height =  document.getElementById("ornamentItem2").style.width = document.getElementById("ornamentItem2").style.height =  document.getElementById("ornamentItem3").style.width = document.getElementById("ornamentItem3").style.height = document.getElementById("ornamentItem4").style.width = document.getElementById("ornamentItem4").style.height = document.getElementById("ornamentItem5").style.width = document.getElementById("ornamentItem5").style.height = document.getElementById("ornamentItem6").style.width = document.getElementById("ornamentItem6").style.height = 131*this.ratio+"px"
      document.getElementById("ornamentItem7").style.width = document.getElementById("ornamentItem7").style.height =  document.getElementById("ornamentItem8").style.width = document.getElementById("ornamentItem8").style.height =  document.getElementById("ornamentItem9").style.width = document.getElementById("ornamentItem9").style.height = document.getElementById("ornamentItem10").style.width = document.getElementById("ornamentItem10").style.height = document.getElementById("ornamentItem11").style.width = document.getElementById("ornamentItem11").style.height = document.getElementById("ornamentItem12").style.width = document.getElementById("ornamentItem12").style.height = 131*this.ratio+"px"

      document.getElementById("operationArea2Row1").style.marginTop = 30*this.ratio+"px"
      document.getElementById("reUploadContainer").style.height = 100*this.ratio+"px"
      document.getElementById("reUploadImg").style.height = 100*this.ratio+"px"
      document.getElementById("reUploadCropper").style.width = 292*this.ratio+"px"
      document.getElementById("reUploadCropper").style.height = 100*this.ratio+"px"
      document.getElementById("makeAva").style.height = 100*this.ratio+"px"




      this.cvs.style.width = this.cvs.style.height = document.body.clientWidth + "px"
      this.puzzleCanvas.style.width = this.puzzleCanvas.style.height = document.body.clientWidth + "px"
      document.getElementById("loader").style.width = document.getElementById("loadingBg").style.width = app.style.width = document.body.clientWidth + "px";
      document.getElementById("loader").style.height = document.getElementById("loadingBg").style.height = app.style.height = (window.innerHeight) + "px"
      document.getElementById('operationArea1').style.height = (window.innerHeight-document.body.clientWidth)+'px'
      document.getElementById('operationArea2').style.height = (window.innerHeight-document.body.clientWidth)+'px'
      document.getElementById('previewContainer').style.height = (window.innerHeight)+'px'
      document.getElementById('previewContainer').style.width = document.body.clientWidth+'px'
      document.getElementById('allImg').style.top = (window.innerHeight/2-document.body.clientWidth/2)+'px'
      document.getElementById('previewTip').style.marginTop = document.body.clientWidth +'px'
      document.getElementById('previewCancel').style.height = (window.innerHeight/2-document.body.clientWidth/2)+'px'
      document.getElementById('previewTip').style.height = (window.innerHeight-(window.innerHeight/2-document.body.clientWidth/2)-document.body.clientWidth)+"px"
    },

    beforeMount: function () {
    },
    mounted: function () {
      const that = this
      window.onresize = () => {
        return (() => {
          window.screenWidth = document.body.clientWidth
          that.screenWidth = window.screenWidth
        })()
      }

      var imgsrcArray = [
        '../../images/oriImg.jpeg',
        "../../images/modal.png",
        // '../../images/flag.png',
        // '../../images/logo.png'
      ];
      this.drawCanvas(imgsrcArray)
    },
    beforeUpdate: function () {
    },
    updated: function () {
    },
    beforeDestroy: function () {
    },
    destroyed: function () {
    },
    methods: {
      startLoading(){
        this.loadingNum++
        document.getElementById("loadingText1").textContent = this.loadingNum+"%"
        if(this.loadingNum>10){
          document.getElementById('wave').style.top = (window.innerHeight*3/5-this.loadingNum*2.4)+"px"

        }
        document.getElementById('wave').style.left = -this.loadingNum*5+"px"
        if(this.loadingNum>99){
          clearInterval(this.timer)

          let loader = document.getElementById('loader')
          if(loader!=null){
            document.body.removeChild(loader)
          }
        }
      },
      autoPlayAudio() {
        wx.config({
          debug: false
        });
        wx.ready(function() {
          document.getElementById("music").play();
        });
      },

      changePic08(){
        if(this.isInitCanvasFabric){
          if(this.isPic08Added){
            this.isPic08Added=false
            this.removePic08()
          }else{
            this.isPic08Added = true
            this.addPic08()
          }
        }else{
          var img = new Image();
          img.src = "data:image/jpeg;base64,"+this.downloadUrl;
          var that = this
          img.onload = function () {
            that.img2Cvs(img)
            if(that.isPic08Added){
              that.isPic08Added=false
              that.removePic08()
            }else{
              that.isPic08Added = true
              that.addPic08()
            }
          }
        }
      },
      removePic08(){
        if (this.pic08Instance) {
          this.canvasFabric.remove(this.pic08Instance)
        }
      },
      addPic08(){
        var that = this
        document.getElementById(that.pic08).style.display = "none";
        var pic08Image = document.getElementById(that.pic08);
        pic08Image.style.display = "block";
        if (that.pic08Instance) {
          that.canvasFabric.remove(that.pic08Instance)
        }
        that.pic08Instance = new fabric.Image(pic08Image, {
          top: 40,
          left: that.screenWidth / 3,
          scaleX: 100 / pic08Image.width,
          scaleY: 100 / pic08Image.height,
          cornerColor: "#0b3a42",
          cornerStrokeColor: "#fff",
          cornerStyle: "circle",
          transparentCorners: false,
          rotatingPointOffset: 30
        });
        that.pic08Instance.setControlVisible("bl", false);
        that.pic08Instance.setControlVisible("tr", false);
        that.pic08Instance.setControlVisible("tl", false);
        that.pic08Instance.setControlVisible("mr", false);
        that.pic08Instance.setControlVisible("mt", false);
        that.canvasFabric.add(that.pic08Instance)
      },


      changePic07(){
        if(this.isInitCanvasFabric){
          if(this.isPic07Added){
            this.isPic07Added=false
            this.removePic07()
          }else{
            this.isPic07Added = true
            this.addPic07()
          }
        }else{
          var img = new Image();
          img.src = "data:image/jpeg;base64,"+this.downloadUrl;
          var that = this
          img.onload = function () {
            that.img2Cvs(img)
            if(that.isPic07Added){
              that.isPic07Added=false
              that.removePic07()
            }else{
              that.isPic07Added = true
              that.addPic07()
            }
          }
        }
      },
      removePic07(){
        if (this.pic07Instance) {
          this.canvasFabric.remove(this.pic07Instance)
        }
      },
      addPic07(){
        var that = this
        document.getElementById(that.pic07).style.display = "none";
        var pic07Image = document.getElementById(that.pic07);
        pic07Image.style.display = "block";
        if (that.pic07Instance) {
          that.canvasFabric.remove(that.pic07Instance)
        }
        that.pic07Instance = new fabric.Image(pic07Image, {
          top: 40,
          left: that.screenWidth / 3,
          scaleX: 100 / pic07Image.width,
          scaleY: 100 / pic07Image.height,
          cornerColor: "#0b3a42",
          cornerStrokeColor: "#fff",
          cornerStyle: "circle",
          transparentCorners: false,
          rotatingPointOffset: 30
        });
        that.pic07Instance.setControlVisible("bl", false);
        that.pic07Instance.setControlVisible("tr", false);
        that.pic07Instance.setControlVisible("tl", false);
        that.pic07Instance.setControlVisible("mr", false);
        that.pic07Instance.setControlVisible("mt", false);
        that.canvasFabric.add(that.pic07Instance)
      },


      changePic06(){
        if(this.isInitCanvasFabric){
          if(this.isPic06Added){
            this.isPic06Added=false
            this.removePic06()
          }else{
            this.isPic06Added = true
            this.addPic06()
          }
        }else{
          var img = new Image();
          img.src = "data:image/jpeg;base64,"+this.downloadUrl;
          var that = this
          img.onload = function () {
            that.img2Cvs(img)
            if(that.isPic06Added){
              that.isPic06Added=false
              that.removePic06()
            }else{
              that.isPic06Added = true
              that.addPic06()
            }
          }
        }
      },
      removePic06(){
        if (this.pic06Instance) {
          this.canvasFabric.remove(this.pic06Instance)
        }
      },
      addPic06(){
        var that = this
        document.getElementById(that.pic06).style.display = "none";
        var pic06Image = document.getElementById(that.pic06);
        pic06Image.style.display = "block";
        if (that.pic06Instance) {
          that.canvasFabric.remove(that.pic06Instance)
        }
        that.pic06Instance = new fabric.Image(pic06Image, {
          top: 40,
          left: that.screenWidth / 3,
          scaleX: 100 / pic06Image.width,
          scaleY: 100 / pic06Image.height,
          cornerColor: "#0b3a42",
          cornerStrokeColor: "#fff",
          cornerStyle: "circle",
          transparentCorners: false,
          rotatingPointOffset: 30
        });
        that.pic06Instance.setControlVisible("bl", false);
        that.pic06Instance.setControlVisible("tr", false);
        that.pic06Instance.setControlVisible("tl", false);
        that.pic06Instance.setControlVisible("mr", false);
        that.pic06Instance.setControlVisible("mt", false);
        that.canvasFabric.add(that.pic06Instance)
      },


      changePic05(){
        if(this.isInitCanvasFabric){
          if(this.isPic05Added){
            this.isPic05Added=false
            this.removePic05()
          }else{
            this.isPic05Added = true
            this.addPic05()
          }
        }else{
          var img = new Image();
          img.src = "data:image/jpeg;base64,"+this.downloadUrl;
          var that = this
          img.onload = function () {
            that.img2Cvs(img)
            if(that.isPic05Added){
              that.isPic05Added=false
              that.removePic05()
            }else{
              that.isPic05Added = true
              that.addPic05()
            }
          }
        }
      },
      removePic05(){
        if (this.pic05Instance) {
          this.canvasFabric.remove(this.pic05Instance)
        }
      },
      addPic05(){
        var that = this
        document.getElementById(that.pic05).style.display = "none";
        var pic05Image = document.getElementById(that.pic05);
        pic05Image.style.display = "block";
        if (that.pic05Instance) {
          that.canvasFabric.remove(that.pic05Instance)
        }
        that.pic05Instance = new fabric.Image(pic05Image, {
          top: 40,
          left: that.screenWidth / 3,
          scaleX: 100 / pic05Image.width,
          scaleY: 100 / pic05Image.height,
          cornerColor: "#0b3a42",
          cornerStrokeColor: "#fff",
          cornerStyle: "circle",
          transparentCorners: false,
          rotatingPointOffset: 30
        });
        that.pic05Instance.setControlVisible("bl", false);
        that.pic05Instance.setControlVisible("tr", false);
        that.pic05Instance.setControlVisible("tl", false);
        that.pic05Instance.setControlVisible("mr", false);
        that.pic05Instance.setControlVisible("mt", false);
        that.canvasFabric.add(that.pic05Instance)
      },


      changePic04(){
        if(this.isInitCanvasFabric){
          if(this.isPic04Added){
            this.isPic04Added=false
            this.removePic04()
          }else{
            this.isPic04Added = true
            this.addPic04()
          }
        }else{
          var img = new Image();
          img.src = "data:image/jpeg;base64,"+this.downloadUrl;
          var that = this
          img.onload = function () {
            that.img2Cvs(img)
            if(that.isPic04Added){
              that.isPic04Added=false
              that.removePic04()
            }else{
              that.isPic04Added = true
              that.addPic04()
            }
          }
        }
      },
      removePic04(){
        if (this.pic04Instance) {
          this.canvasFabric.remove(this.pic04Instance)
        }
      },
      addPic04(){
        var that = this
        document.getElementById(that.pic04).style.display = "none";
        var pic04Image = document.getElementById(that.pic04);
        pic04Image.style.display = "block";
        if (that.pic04Instance) {
          that.canvasFabric.remove(that.pic04Instance)
        }
        that.pic04Instance = new fabric.Image(pic04Image, {
          top: 40,
          left: that.screenWidth / 3,
          scaleX: 100 / pic04Image.width,
          scaleY: 100 / pic04Image.height,
          cornerColor: "#0b3a42",
          cornerStrokeColor: "#fff",
          cornerStyle: "circle",
          transparentCorners: false,
          rotatingPointOffset: 30
        });
        that.pic04Instance.setControlVisible("bl", false);
        that.pic04Instance.setControlVisible("tr", false);
        that.pic04Instance.setControlVisible("tl", false);
        that.pic04Instance.setControlVisible("mr", false);
        that.pic04Instance.setControlVisible("mt", false);
        that.canvasFabric.add(that.pic04Instance)
      },


      changePic03(){
        if(this.isInitCanvasFabric){
          if(this.isPic03Added){
            this.isPic03Added=false
            this.removePic03()
          }else{
            this.isPic03Added = true
            this.addPic03()
          }
        }else{
          var img = new Image();
          img.src = "data:image/jpeg;base64,"+this.downloadUrl;
          var that = this
          img.onload = function () {
            that.img2Cvs(img)
            if(that.isPic03Added){
              that.isPic03Added=false
              that.removePic03()
            }else{
              that.isPic03Added = true
              that.addPic03()
            }
          }
        }
      },
      removePic03(){
        if (this.pic03Instance) {
          this.canvasFabric.remove(this.pic03Instance)
        }
      },
      addPic03(){
        var that = this
        document.getElementById(that.pic03).style.display = "none";
        var pic03Image = document.getElementById(that.pic03);
        pic03Image.style.display = "block";
        if (that.pic03Instance) {
          that.canvasFabric.remove(that.pic03Instance)
        }
        that.pic03Instance = new fabric.Image(pic03Image, {
          top: 40,
          left: that.screenWidth / 3,
          scaleX: 100 / pic03Image.width,
          scaleY: 100 / pic03Image.height,
          cornerColor: "#0b3a42",
          cornerStrokeColor: "#fff",
          cornerStyle: "circle",
          transparentCorners: false,
          rotatingPointOffset: 30
        });
        that.pic03Instance.setControlVisible("bl", false);
        that.pic03Instance.setControlVisible("tr", false);
        that.pic03Instance.setControlVisible("tl", false);
        that.pic03Instance.setControlVisible("mr", false);
        that.pic03Instance.setControlVisible("mt", false);
        that.canvasFabric.add(that.pic03Instance)
      },



      changePic02(){
        if(this.isInitCanvasFabric){
          if(this.isPic02Added){
            this.isPic02Added=false
            this.removePic02()
          }else{
            this.isPic02Added = true
            this.addPic02()
          }
        }else{
          var img = new Image();
          img.src = "data:image/jpeg;base64,"+this.downloadUrl;
          var that = this
          img.onload = function () {
            that.img2Cvs(img)
            if(that.isPic02Added){
              that.isPic02Added=false
              that.removePic02()
            }else{
              that.isPic02Added = true
              that.addPic02()
            }
          }
        }
      },
      removePic02(){
        if (this.pic02Instance) {
          this.canvasFabric.remove(this.pic02Instance)
        }
      },
      addPic02(){
        var that = this
        document.getElementById(that.pic02).style.display = "none";
        var pic02Image = document.getElementById(that.pic02);
        pic02Image.style.display = "block";
        if (that.pic02Instance) {
          that.canvasFabric.remove(that.pic02Instance)
        }
        that.pic02Instance = new fabric.Image(pic02Image, {
          top: 40,
          left: that.screenWidth / 3,
          scaleX: 100 / pic02Image.width,
          scaleY: 100 / pic02Image.height,
          cornerColor: "#0b3a42",
          cornerStrokeColor: "#fff",
          cornerStyle: "circle",
          transparentCorners: false,
          rotatingPointOffset: 30
        });
        that.pic02Instance.setControlVisible("bl", false);
        that.pic02Instance.setControlVisible("tr", false);
        that.pic02Instance.setControlVisible("tl", false);
        that.pic02Instance.setControlVisible("mr", false);
        that.pic02Instance.setControlVisible("mt", false);
        that.canvasFabric.add(that.pic02Instance)
      },



      changePic01(){
        if(this.isInitCanvasFabric){
          if(this.isPic01Added){
            this.isPic01Added=false
            this.removePic01()
          }else{
            this.isPic01Added = true
            this.addPic01()
          }
        }else{
          var img = new Image();
          img.src = "data:image/jpeg;base64,"+this.downloadUrl;
          var that = this
          img.onload = function () {
            that.img2Cvs(img)
            if(that.isPic01Added){
              that.isPic01Added=false
              that.removePic01()
            }else{
              that.isPic01Added = true
              that.addPic01()
            }
          }
        }
      },
      removePic01(){
        if (this.pic01Instance) {
          this.canvasFabric.remove(this.pic01Instance)
        }
      },
      addPic01(){
        var that = this
        document.getElementById(that.pic01).style.display = "none";
        var pic01Image = document.getElementById(that.pic01);
        pic01Image.style.display = "block";
        if (that.pic01Instance) {
          that.canvasFabric.remove(that.pic01Instance)
        }
        that.pic01Instance = new fabric.Image(pic01Image, {
          top: 40,
          left: that.screenWidth / 3,
          scaleX: 100 / pic01Image.width,
          scaleY: 100 / pic01Image.height,
          cornerColor: "#0b3a42",
          cornerStrokeColor: "#fff",
          cornerStyle: "circle",
          transparentCorners: false,
          rotatingPointOffset: 30
        });
        that.pic01Instance.setControlVisible("bl", false);
        that.pic01Instance.setControlVisible("tr", false);
        that.pic01Instance.setControlVisible("tl", false);
        that.pic01Instance.setControlVisible("mr", false);
        that.pic01Instance.setControlVisible("mt", false);
        that.canvasFabric.add(that.pic01Instance)
      },
      changeGlasses(){
        if(this.isInitCanvasFabric){
          if(this.isGlassesAdded){
            this.isGlassesAdded=false
            this.removeGlasses()
          }else{
            this.isGlassesAdded = true
            this.addGlasses()
          }
        }else{
          var img = new Image();
          img.src = "data:image/jpeg;base64,"+this.downloadUrl;
          var that = this
          img.onload = function () {
            that.img2Cvs(img)
            if(that.isGlassesAdded){
              that.isGlassesAdded=false
              that.removeGlasses()
            }else{
              that.isGlassesAdded = true
              that.addGlasses()
            }
          }
        }
      },
      removeGlasses(){
        if (this.glassesInstance) {
          this.canvasFabric.remove(this.glassesInstance)
        }
      },
      addGlasses(){
        var that = this
        document.getElementById(that.glasses).style.display = "none";
        var glassesImage = document.getElementById(that.glasses);
        glassesImage.style.display = "block";
        if (that.glassesInstance) {
          that.canvasFabric.remove(that.glassesInstance)
        }
        that.glassesInstance = new fabric.Image(glassesImage, {
          top: 40,
          left: that.screenWidth / 3,
          scaleX: 100 / glassesImage.width,
          scaleY: 100 / glassesImage.height,
          cornerColor: "#0b3a42",
          cornerStrokeColor: "#fff",
          cornerStyle: "circle",
          transparentCorners: false,
          rotatingPointOffset: 30
        });
        that.glassesInstance.setControlVisible("bl", false);
        that.glassesInstance.setControlVisible("tr", false);
        that.glassesInstance.setControlVisible("tl", false);
        that.glassesInstance.setControlVisible("mr", false);
        that.glassesInstance.setControlVisible("mt", false);
        that.canvasFabric.add(that.glassesInstance)
      },
      changePower(){

        if(this.isInitCanvasFabric){
          if(this.isPowerAdded){
            this.isPowerAdded=false
            this.removePower()
          }else{
            this.isPowerAdded = true
            this.addPower()
          }
        }else{
          var img = new Image();
          img.src = "data:image/jpeg;base64,"+this.downloadUrl;
          var that = this
          img.onload = function () {
            that.img2Cvs(img)
            if(that.isPowerAdded){
              that.isPowerAdded=false
              that.removePower()
            }else{
              that.isPowerAdded = true
              that.addPower()
            }
          }
        }
      },
      removePower(){
        if (this.powerInstance) {
          this.canvasFabric.remove(this.powerInstance)
        }
      },
      addPower(){
        var that = this
        document.getElementById(that.power).style.display = "none";
        var powerImage = document.getElementById(that.power);
        powerImage.style.display = "block";
        if (that.powerInstance) {
          that.canvasFabric.remove(that.powerInstance)
        }
        that.powerInstance = new fabric.Image(powerImage, {
          top: 40,
          left: that.screenWidth / 3,
          scaleX: 100 / powerImage.width,
          scaleY: 100 / powerImage.height,
          cornerColor: "#0b3a42",
          cornerStrokeColor: "#fff",
          cornerStyle: "circle",
          transparentCorners: false,
          rotatingPointOffset: 30
        });
        that.powerInstance.setControlVisible("bl", false);
        that.powerInstance.setControlVisible("tr", false);
        that.powerInstance.setControlVisible("tl", false);
        that.powerInstance.setControlVisible("mr", false);
        that.powerInstance.setControlVisible("mt", false);
        that.canvasFabric.add(that.powerInstance)
      },

      changeHat(){


        if(this.isInitCanvasFabric){
          if(this.isHatAdded){
            this.isHatAdded=false
            this.removeHat()
          }else{
            this.isHatAdded = true
            this.addHat()
          }
        }else{
          var img = new Image();
          img.src = "data:image/jpeg;base64,"+this.downloadUrl;
          var that = this
          img.onload = function () {
            that.img2Cvs(img)
            if(that.isHatAdded){
              that.isHatAdded=false
              that.removeHat()
            }else{
              that.isHatAdded = true
              that.addHat()
            }
          }
        }
      },
      removeHat(){
        if (this.hatInstance) {
          this.canvasFabric.remove(this.hatInstance)
        }
      },
      addHat(){
        var that = this
        document.getElementById(that.hat).style.display = "none";
        var hatImage = document.getElementById(that.hat);
        hatImage.style.display = "block";
        if (that.hatInstance) {
          that.canvasFabric.remove(that.hatInstance)
        }
        that.hatInstance = new fabric.Image(hatImage, {
          top: 40,
          left: that.screenWidth / 3,
          scaleX: 100 / hatImage.width,
          scaleY: 100 / hatImage.height,
          cornerColor: "#0b3a42",
          cornerStrokeColor: "#fff",
          cornerStyle: "circle",
          transparentCorners: false,
          rotatingPointOffset: 30
        });
        that.hatInstance.setControlVisible("bl", false);
        that.hatInstance.setControlVisible("tr", false);
        that.hatInstance.setControlVisible("tl", false);
        that.hatInstance.setControlVisible("mr", false);
        that.hatInstance.setControlVisible("mt", false);
        that.canvasFabric.add(that.hatInstance)
      },

      changeHand(){

        if(this.isInitCanvasFabric){
          if(this.isHandAdded){
            this.isHandAdded=false
            this.removeHand()
          }else{
            this.isHandAdded = true
            this.addHand()
          }
        }else{
          var img = new Image();
          img.src = "data:image/jpeg;base64,"+this.downloadUrl;
          var that = this
          img.onload = function () {
            that.img2Cvs(img)
            if(that.isHandAdded){
              that.isHandAdded=false
              that.removeHand()
            }else{
              that.isHandAdded = true
              that.addHand()
            }
          }
        }
      },
      removeHand(){
        if (this.handInstance) {
          this.canvasFabric.remove(this.handInstance)
        }
      },
      addHand(){
        var that = this
        document.getElementById(that.hand).style.display = "none";
        var handImage = document.getElementById(that.hand);
        handImage.style.display = "block";
        if (that.handInstance) {
          that.canvasFabric.remove(that.handInstance)
        }
        that.handInstance = new fabric.Image(handImage, {
          top: 40,
          left: that.screenWidth / 3,
          scaleX: 100 / handImage.width,
          scaleY: 100 / handImage.height,
          cornerColor: "#0b3a42",
          cornerStrokeColor: "#fff",
          cornerStyle: "circle",
          transparentCorners: false,
          rotatingPointOffset: 30
        });
        that.handInstance.setControlVisible("bl", false);
        that.handInstance.setControlVisible("tr", false);
        that.handInstance.setControlVisible("tl", false);
        that.handInstance.setControlVisible("mr", false);
        that.handInstance.setControlVisible("mt", false);
        that.canvasFabric.add(that.handInstance)
      },
      img2Cvs(img) {
        var that = this
        that.cvs.width = img.width;
        that.cvs.height = img.height;
        that.cvs.style.display = "block";
        that.canvasFabric = new fabric.Canvas("cvs", {
          width: that.screenWidth,
          height: that.screenWidth,
          backgroundImage: new fabric.Image(img, {
            scaleX: that.screenWidth / img.width,
            scaleY: that.screenWidth / img.height
          })
        });
        that.isInitCanvasFabric=true;
      },
      getbase64Data(data) {
        this.isAdded = true
        document.getElementById('heartHat').style.display = "none"
        document.getElementById('heartHand').style.display = "none"
        document.getElementById('heartGlasses').style.display = "none"
        document.getElementById('heartPower').style.display = "none"
        var imgsrcArray = [
          data,
          "../../images/modal.png",
        ];

        this.drawCanvas(imgsrcArray)
      },
      getBlob(blob) {
        console.log(blob)
      },
      getFile(file) {
        console.log(file)
      },
      drawCanvas(imgsrcArray) {
        var self = this;
        var canvas = document.getElementById('puzzleCanvas');
        var ctx = canvas.getContext('2d');
        canvas.width = document.body.clientWidth*6
        canvas.height = document.body.clientWidth*6;
        var imglen = imgsrcArray.length;
        var drawimg = (function f(n) {
          if (n < imglen) {
            var img = new Image();
            img.crossOrigin = 'Anonymous'; //解决跨域问题
            img.onload = function () {
              //ctx.save();
              if (n == 0) {
                ctx.drawImage(img, 0, 0, canvas.width, canvas.width);
              } else if(n==1) {
                ctx.drawImage(img, 0, 0, canvas.width, canvas.width);
              }else if(n==2){
                ctx.drawImage(img,0,30,202*self.ratio,56*self.ratio)
              }else{
                ctx.drawImage(img,canvas.width-100,20,canvas.width/5,canvas.width/5)
              }
              f(n + 1);
            }

            img.src = imgsrcArray[n];
          } else {
            self.downloadUrl = canvas.toDataURL("image/jpeg");
            self.downloadUrl = self.downloadUrl.replace("data:image/jpeg;base64,", "");
            console.log("更换背景")
            if(self.isInitCanvasFabric){
              var img = new Image();
              img.src = "data:image/jpeg;base64,"+self.downloadUrl;
              img.onload = function () {
                self.canvasFabric.setBackgroundImage(img.src,self.canvasFabric.renderAll.bind(self.canvasFabric),{
                  scaleX: self.screenWidth / img.width,
                  scaleY: self.screenWidth / img.height
                });
              }
            }
          }
        })(0);

      },

      cancelPreview(){
        console.log("取消")
        document.getElementById('previewContainer').style.display = "none"
      },
      previewImg(){
        var dataBase64 = ""
        if(this.isInitCanvasFabric){
          dataBase64 = this.canvasFabric.toDataURL({width: this.screenWidth, height: this.screenWidth})
        }else{
          dataBase64 = "data:image/jpeg;base64,"+this.downloadUrl
        }
        var imgsrcArray = [
          dataBase64,
          // '../../images/flag.png',
          // '../../images/logo.png'
        ];
        this.puzzleAllCanvas(imgsrcArray)
      },
      puzzleAllCanvas(imgsrcArray){
        var self = this;
        var canvas = document.getElementById('puzzleAllCanvas');
        var ctx = canvas.getContext('2d');
        var multiple = 6
        canvas.width = document.body.clientWidth*multiple
        canvas.height = document.body.clientWidth*multiple;
        var imglen = imgsrcArray.length;
        var drawimg = (function f(n) {
          if (n < imglen) {
            var img = new Image();
            img.crossOrigin = 'Anonymous'; //解决跨域问题
            img.onload = function () {
              //ctx.save();
              if (n == 0) {
                ctx.drawImage(img, 0, 0, canvas.width, canvas.width);
              } else if(n==1) {
                ctx.drawImage(img,0,multiple*70*self.ratio,multiple*202*self.ratio,multiple*56*self.ratio)
              }else{
                ctx.drawImage(img,(document.body.clientWidth-40*self.ratio-165*self.ratio)*multiple,(40*self.ratio)*multiple,165*self.ratio*multiple,147*self.ratio*multiple)
              }
              f(n + 1);
            }

            img.src = imgsrcArray[n];
          } else {
            self.allDataUrl = canvas.toDataURL("image/jpeg");
            console.log("最终图片",self.allDataUrl)
            document.getElementById("previewContainer").style.display = "block"
            document.getElementById("allImg").src = self.allDataUrl
          }
        })(0);
      }
    }
  })
</script>
</body>
</html>

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
### 回答1: uview头像裁剪h5是一款非常实用的网页应用程序,它提供了一个简单而强大的工具,让用户可以轻松地对头像图片进行裁剪和编辑。这个应用程序使用H5技术,可以在任何设备上进行使用,同时也完美适配了手机和平板电脑等各种不同大小的屏幕。 使用uview头像裁剪h5非常简单,用户只需要将需要编辑的图片上传到程序中,然后对图片进行缩放旋转拖拽等操作,最终得到需要的裁剪结果。这个程序提供了多种裁剪比例和尺寸选项,让用户可以按照自己的需求进行精准的裁剪。同时,这个程序还提供了一系列的美化功能,如滤镜、调节亮度、对比度等,可以让头像图片更加美观。 总的来说,uview头像裁剪h5是一款非常实用的工具,不仅功能齐全,而且使用起来也非常简单。对于需要进行头像裁剪和编辑的用户来说,这个程序绝对是一款不可缺少的工具。 ### 回答2: Uview头像裁剪是一款基于H5和Vue.js技术开发的在线头像裁剪工具。它可以实现在网页端方便地裁剪和调整用户上传的图片,适用于各种类型的Web应用程序。Uview头像裁剪提供了一系列的功能,包括头像上传、格式转换、裁剪和预览。它还提供了一个可定制的用户界面,使得用户可以选择自己的喜好来定制裁剪界面的颜色、样式和布局等。 使用Uview头像裁剪工具非常简单,只需要在页面中引入相关的js和css文件即可。用户可以启动裁剪工具并上传待处理的图片,然后通过手动调整裁剪框的大小和位置来确定裁剪区域。裁剪后的头像可以实时预览,并可以通过工具栏中的按钮来进行旋转缩放拖拽等操作。一旦用户确定了头像的最终裁剪结果,就可以获取图片的base64编码或者上传到服务器中进行进一步的处理。 总的来说,Uview头像裁剪是一款非常实用的H5工具,它可以帮助Web应用程序轻松实现头像裁剪功能,提高用户的体验和产品的交互性。 ### 回答3: Uview头像裁剪H5是一种基于网页技术的头像裁剪工具,它的前端技术依托于HTML、CSS和Javascript,广泛应用于移动端和PC端。Uview头像裁剪H5具有快捷、简单、易用和高效的特点,可以满足用户对头像裁剪的各种需求。用户可以选择上传本地图片或拍照,自由调整裁剪框位置和大小,支持缩放旋转和水平翻转等功能,方便用户裁剪出理想的头像,同时还提供了丰富的模板和样式,以及多种输出格式和尺寸,能够满足不同业务场景的需求。 使用Uview头像裁剪H5,用户可以在网页端将需要裁剪头像图片上传到服务器,然后通过Uview头像裁剪的算法来对头像进行裁剪和处理,最终输出需要的头像文件,让用户可以方便地将其作为社交平台头像、微信头像、企业联系方式等用途。同时,Uview头像裁剪H5使用了大量的优化技术,比如说压缩、缓存和CDN加速等,以保证页面的加载速度和用户体验,并且提供了丰富的API和插件,方便开发者进行二次开发和定制。 总的来说,Uview头像裁剪H5结合了HTML、CSS和Javascript的精华,将用户的需求和技术的优势完美地结合在一起,为用户提供了一款优秀的头像裁剪工具,成为了头像处理领域的佼佼者。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

玩烂小程序

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值