Vuejs multiple image upload with preview component

Vuejs multiple image upload with preview component

In this Post We Will Explain About is Vuejs multiple image upload with preview component With Example and Demo.Welcome on infinityknow.com – Examples, The best For Learn web development Tutorials,Demo with Example! Hi Dear Friends here u can know to Vuejs multiple image upload with previewExample

In this post we will show you Best way to implement VueJS + HTML5 Canvas: Image upload component, hear for Image Upload in Your Vue.js Apps With vue-picture-inputwith Download .we will give you demo,Source Code and examples for implement Step By Step Good Luck!.

HTML PART

<main class="liveApp-container">
  
  <h1>Basic - VueJS + HTML5 Canvas</h1>
  <span class="sub">Simple Image upload file, as well as resize images, crop, as well as base64 converter</span>
  
  <div id="liveApp">
    <image-uploader 
      width="700px" 
      height="500px">
    </image-uploader>
    <section class="text-area-section">
      <textarea 
        v-model="shareliveData.liveImgDt">
      </textarea>
    </section>
  </div>
</main>

<!-- -->
<script type="text/x-template" id="image-uploader-template">
<div class="image-input__container image-uploader" 
	v-bind:style="containerStyles" 
  v-bind:class="{ 'image-loaded': simpleImgupload }">


    <form class="image-input__form" ref="form">
      <input @change="simplelivethumb" class="image-input__input" ref="fileInput" type="file" />
    </form>
    <div v-on:click="triggerInput" class="image-input__overlay"></div>
    <div class="image-input__control-buttons">
    	<button v-on:click="con64base" type="button" class="to-base64">64</button>
      <button v-on:click="upmethods" type="button" class="scale-up">+</button>
      <button v-on:click="downup" type="button" class="scale-down">-</button>
      <button v-on:click="clearmethods" type="button" class="reset-input">x</button>
    </div>

    <canvas class="image-input__canvas"
      ref="canvas"
      v-bind:height="height"
      v-bind:width="width"
      v-bind:class="{ 'is-draggable': hoverIsDraggable }">
    </canvas>

  </div>
</script>

JavaScript Part

var shareliveData = {
  liveImgDt: '',
  updateImageData: function(newData){
    this.liveImgDt = newData;
  }
};

Vue.component('image-uploader', {
  template: '#image-uploader-template',
  props: ['id','width','height'],
  data: function () {
    return {
      simpleImgupload: false,
      hoverIsDraggable: false,
      currtx: null, canvas: null,
      img: null, 
      scaleSteps: 0,
      liveimgX: 0, liveimgY: 0,
      scaledImageWidth: 0, simpleImgHeight: 0,
      imageWidth: 0, imageHeight: 0, imageRight: 0, bottomimg: 0,
      draggingImage: false,
      startX: 0, startY: 0,
      mouseX: 0, mouseY: 0,
      shareliveData: window.shareliveData
    }
  },
  computed: {
    containerStyles: function () {
      return {
        width: this.width,
        height: this.height
      };
    }
  },
  mounted: function(){
    this.$on('get-image-base64', function(){
      console.log("ON'ING");
      this.updateTemplateImage();
    });
    this.canvas = this.$refs.canvas;
    this.currtx = this.canvas.getContext("2d");
    this.img = document.createElement("img");
  },
  methods: {
    con64base: function()
    {
      var liveImgDt = this.canvas.toDataURL("image/png");
      liveImgDt.replace('data:image/png;base64,', '');
      this.shareliveData.updateImageData(liveImgDt);
    },
    draw: function(withBorders) 
    {
      this.currtx.clearRect(0, 0, this.canvas.width, this.canvas.height);
      var scaleFactor = 1 - (this.scaleSteps * .1) ;
      this.scaledImageWidth = this.img.width * scaleFactor;
      this.simpleImgHeight = this.scaledImageWidth * (this.img.height/this.img.width);
      this.currtx.drawImage(this.img, 0, 0, this.img.width, this.img.height, this.liveimgX, this.liveimgY, this.scaledImageWidth, this.simpleImgHeight);

      this.imageRight = this.liveimgX + this.scaledImageWidth;
      this.bottomimg = this.liveimgY + this.simpleImgHeight;
      if (withBorders) {
          this.currtx.beginPath();
          this.currtx.moveTo(this.liveimgX, this.liveimgY);
          this.currtx.lineTo(this.imageRight, this.liveimgY);
          this.currtx.lineTo(this.imageRight, this.bottomimg);
          this.currtx.lineTo(this.liveimgX, this.bottomimg);
          this.currtx.closePath();
          this.currtx.stroke();
      }
    },
    clearmethods: function(){
      // clear the canvas
      this.currtx.clearRect(0, 0, this.canvas.width, this.canvas.height);
      // Reset state
      this.$refs.form.reset();
      this.simpleImgupload = false;
    },
    upmethods: function(){
      this.scaleSteps--;
      this.draw(false);
    },
    downup: function(){
      this.scaleSteps++;
      this.draw(false);
    },
    getMousePos: function(evt){
        var rect = this.canvas.getBoundingClientRect(), // abs. size of element
            scaleX = this.canvas.width / rect.width,    // relationship bitmap vs. element for X
            scaleY = this.canvas.height / rect.height;  // relationship bitmap vs. element for Y

        return {
          x: (evt.clientX - rect.left) * scaleX,   // scale mouse coordinates after they have
          y: (evt.clientY - rect.top) * scaleY     // been adjusted to be relative to element
        }
    },
    isImageHit: function(x, y){
      return (x > this.liveimgX && x < this.liveimgX + this.imageWidth && y > this.liveimgY && y < this.liveimgY + this.imageHeight);
    },
    triggerInput: function(event)
    {
      event.preventDefault();
      this.$refs.fileInput.click();
    },
    simplelivethumb: function (event) 
    {
      var vueminit = this;
      var input = event.target;

      if (input.files && input.files[0]) 
      {
        var imgread = new FileReader();

        imgread.onload = function (e) {
          vueminit.img.src = e.target.result;
        }

        imgread.readAsDataURL(input.files[0]);

        vueminit.img.onload = function() 
        {

          vueminit.currtx.drawImage(vueminit.img, 0, 0);


          vueminit.imageWidth = vueminit.img.width;
          vueminit.imageHeight = vueminit.img.height;
          vueminit.imageRight = vueminit.liveimgX + vueminit.imageWidth;
          vueminit.bottomimg = vueminit.liveimgY + vueminit.imageHeight
          

          vueminit.draw(false);


          vueminit.simpleImgupload = true;
        }
      }

      function handleMouseDown(e) {
        var pos = vueminit.getMousePos(e);
        vueminit.startX = pos.x;
        vueminit.startY = pos.y;
        vueminit.draggingImage = vueminit.hoverIsDraggable;
      }

      function handleMouseUp(e) {
        vueminit.draggingImage = false;
        vueminit.draw(false);
      }

      function handleMouseOut(e) {
        handleMouseUp(e);
      }

      function handleMouseMove(e) 
      {

        var pos = vueminit.getMousePos(e);
        vueminit.hoverIsDraggable = vueminit.isImageHit(pos.x, pos.y);

        if (vueminit.draggingImage) 
        {

          vueminit.mouseX = pos.x;
          vueminit.mouseY = pos.y;

          var dx = vueminit.mouseX - vueminit.startX;
          var dy = vueminit.mouseY - vueminit.startY;

          var collidedOnLeft = vueminit.liveimgX > 0 && dx > 0;
          var collidedOnRight = vueminit.imageRight < vueminit.canvas.width && dx < 0;
          var collidedOnTop = vueminit.liveimgY > 0 && dy > 0;
          var collidedOnBottom = vueminit.bottomimg < vueminit.canvas.height && dy < 0;

          if(collidedOnLeft) {
            vueminit.liveimgX = 0;
            vueminit.imageRight = vueminit.scaledImageWidth;
          }
          else if(collidedOnRight) {
            vueminit.liveimgX = vueminit.canvas.width - vueminit.scaledImageWidth;
            vueminit.imageRight = vueminit.canvas.width;
          } else {
            vueminit.liveimgX += dx;
            vueminit.imageRight += dx;
          }
          
          if(collidedOnTop) {
            vueminit.liveimgY = 0;
            vueminit.bottomimg = vueminit.simpleImgHeight;
          }
          else if(collidedOnBottom) {
            vueminit.liveimgY = vueminit.canvas.height - vueminit.simpleImgHeight;
            vueminit.bottomimg = vueminit.canvas.height;
          } else {
            vueminit.liveimgY += dy;
            vueminit.bottomimg += dy;
          }

          vueminit.startX = vueminit.mouseX;
          vueminit.startY = vueminit.mouseY;

          vueminit.draw(true);
        }
      } 
      vueminit.canvas.addEventListener('mousedown', handleMouseDown, false);
      vueminit.canvas.addEventListener('mousemove', handleMouseMove, false);
      vueminit.canvas.addEventListener('mouseup', handleMouseUp, false);
      vueminit.canvas.addEventListener('mouseout', handleMouseOut, false);
    }
  }
});

var liveApp = new Vue({
  el: '#liveApp',
  data: {
    shareliveData: window.shareliveData
  },
  methods: {}
});

Example

READ :  How to send WhatsApp Messages from PHP

I hope you have Got What is Vuejs multiple image upload with preview component And how it works.I would Like to have FeadBack From My Blog(infinityknow.com) readers.Your Valuable FeadBack,Any Question,or any Comments abaout This Article(infinityknow.com) Are Most Always Welcome.

Add a Comment

Your email address will not be published. Required fields are marked *