Vue implements irregular screenshots

Vue implements irregular screenshots

Most of what you have seen are regular screenshots, which can cope with most application scenarios. However, for image processing, if you want to hand over the rules to users, ordinary screenshots can no longer satisfy users. Then can we implement arbitrary regular capture of images on the front end? Let me discuss it together!

Image capture through svg

Use the clipPath image tag in svg to capture images through id mapping and the coordinates of the dynamic position polygon

    <div>
        <div class="content" @mousemove="mousemove" @mouseup="(e) => {mouseup(e);}">
          <!-- Canvas display -->
          <svg
            ref="blackSvg"
            class="blackSvg"
            xmlns="http://www.w3.org/2000/svg"
            width="300"
            height="300"
          >
            <defs>
              <clipPath id="clippath">
                <polygon :points="points"></polygon>
              </clipPath>
            </defs>
            <image
              xmlns:link="http://www.w3.org/1999/xlink"
              href="https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=3228549874,2173006364&fm=26&gp=0.jpg" rel="external nofollow" 
              width="300"
              height="300"
              preserveAspectRatio="none"
              style="clip-path: url(#clippath)"
            ></image>
          </svg>
          <!-- Drag point -->
          <ul class="interception">
            <li
              v-for="item in 4"
              :ref="`li${item}`"
              :key="item"
              @mousedown="(e) => {mousedown(e, item);}"
            ></li>
          </ul>
          <!-- Base map display -->
          <img
            class="blackImge"
            src="https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=3228549874,2173006364&fm=26&gp=0.jpg"
            alt=""
          />
          <!-- Mask layer-->
          <div class="blackDiv"></div>
    </div>
  </div>

CSS part

<style lang="sass">
.blackDiv
    width: 100%
    height: 100%
    position: absolute
    top: 0
    z-index: 2
    background: rgba(0,0,0, 1)
.content
    width:300px
    height:300px
    text-align: left
    position: relative
    .blackSvg
        position: absolute
        top: 0
        z-index: 3
    .blackImage
        position: absolute
        top: 0
        left: 0
        width: 300px
        height: 300px
    .interception
        list-style: none
        position: absolute
        top: 0
        margin: 0
        padding: 0
        z-index: 3
        >li
            position: absolute
            width: 10px
            height: 10px
            background: blue
            border-radius: 50%
            cursor: pointer
            &:hover
                transform: scale(1.2)
                transition-duration: .2
        >li:nth-child(1)
            top: 10px
            left: 10px
        >li:nth-child(2)
            top: 10px
            left: 100px
        >li:nth-child(3)
            top: 100px
            left: 100px
        >li:nth-child(4)
            top: 100px
            left: 10px
</style>
<script>

export default {
  name: 'Canvas',
  data() {
    return {
      points: '0 0,300 0,300 300,0 300', // Image display initialization status: false,
      index: 0,
      disX: 0,
      disY: 0,
      coordinates: { // Initialize drag point 1: [0, 0],
        2: [300, 0],
        3: [300, 300],
        4: [0, 300],
      },
    };
  },
  mounted() {
    this.$nextTick(() => {
      for (let key in this.coordinates) {
        const left = this.coordinates[key][0];
        const top = this.coordinates[key][1];
        this.$refs[`li${key}`].style.left = `${left}px`;
        this.$refs[`li${key}`].style.top = `${top}px`;
        if (key == 2 || key == 3) {
          this.$refs[`li${key}`].style.left = `${left - 10}px`;
        }
        if (key == 3 || key == 4) {
          this.$refs[`li${key}`].style.top = `${top - 10}px`;
        }
      }
      document.onmouseup = () => {
        this.status = false;
      };
    });
  },
  methods: {
    //Mouse down mousedown(e, index) {
      this.status = true;
      this.index = index;
      this.disX = e.clientX - this.$refs[`li${index}`].offsetLeft;
      this.disY = e.clientY - this.$refs[`li${index}`].offsetTop;
    },
    // Mouse up mouseup(e) {
      this.status = false;
    },
    //Mouse movement mousemove(e) {
      if (this.status) {
        let left = e.clientX - this.disX;
        let top = e.clientY - this.disY;
        this.$refs[`li${this.index}`].style.left = `${left}px`;
        this.$refs[`li${this.index}`].style.top = `${top}px`;
        this.coordinates[this.index] = [left, top];
        const pointsArr = [];
        for (let item in this.coordinates) {
          pointsArr.push(
            Array.from(this.coordinates[item], (e) => {
              return e + 5;
            })
          );
        }
        this.points = pointsArr.join(' ');
      }
    },
  },
};

Effect picture display

Source code address

GitHub address --> github.com/lgxin/captu…

The above is the detailed content of Vue's implementation of irregular screenshots. For more information about Vue's irregular screenshots, please pay attention to other related articles on 123WORDPRESS.COM!

You may also be interested in:
  • Realizing drag effect based on Vue
  • Implementing drag and drop function based on Vue
  • Example of implementing drag and drop effect with Vue.js
  • Vue.js implements random dragging of pictures
  • Vue implements div drag and drop
  • Vue custom directive drag function example
  • A simple way to implement Vue's drag screenshot function

<<:  Detailed explanation of the role of static variables in MySQL

>>:  Linux editing start, stop and restart springboot jar package script example

Recommend

Detailed explanation of group by and having in MySQL

The GROUP BY syntax can group and count the query...

Best Practices for Implementing Simple Jira Projects with React+TS

A set of projects for training react+ts Although ...

Detailed explanation of built-in methods of javascript array

Table of contents 1. Array.at() 2. Array.copyWith...

MySQL series 15 MySQL common configuration and performance stress test

1. Common MySQL configuration All the following c...

How to use nginx as a load balancer for mysql

Note: The nginx version must be 1.9 or above. Whe...

Detailed explanation of the minimum width value of inline-block in CSS

Preface Recently, I have been taking some time in...

Summary of React's way of creating components

Table of contents 1. Create components using func...

How to implement encryption and decryption of sensitive data in MySQL database

Table of contents 1. Preparation 2. MySQL encrypt...

A brief analysis of the game kimono memo problem

Today, after the game was restarted, I found that...

5 JavaScript Ways to Flatten Arrays

Table of contents 1. Concept of array flattening ...

How to permanently change the host name in Linux

If you want to change your host name, you can fol...

Solve the problem of no my.cnf file in /etc when installing mysql on Linux

Today I wanted to change the mysql port, but I fo...

How to achieve 3D dynamic text effect with three.js

Preface Hello everyone, this is the CSS wizard - ...

Mysql8.0 uses window functions to solve sorting problems

Introduction to MySQL Window Functions MySQL has ...

A brief introduction to MySQL InnoDB ReplicaSet

Table of contents 01 Introduction to InnoDB Repli...