JavaScript to achieve click image flip effect

JavaScript to achieve click image flip effect

I was recently working on a project about face collection, and when I was writing the front-end module, I encountered a problem. When customers upload photos or take photos and upload them directly, some pictures may be flipped 90 degrees. So, we need to give customers a button so that they can rotate the pictures themselves. The effect is roughly as follows

Figure 1. Normal image upload

Figure 2. The image is rotated left

Figure 3. Image rotated right

The above is a picture rotation function

Now let’s start with the code part.

Here I have adopted a method, and my current image format is base64. It doesn’t matter if you encounter other formats, because our final effect still needs to be converted to an image object to achieve it.

/**
     * Image rotation* @param direction The direction of rotation*/
    rotate (direction) {
      const img = new Image()
      // The idea here is to map the image to a drawing board and then redraw the image, so we need to create a canvas object to act as our drawing board const canvas = document.createElement('canvas')
      // base64 convert image object img.src = this.uploadImage
      // Remember to convert base64 into an image object before performing other operations. The author has made a mistake here. The onload method loads the image before performing other operations. If the image is a file path, the cross-domain call effect is more obvious.img.onload = () => {
        // The height and width of img cannot be obtained after the img element is hidden, otherwise an error will occur const height = img.height
        const width = img.width
        // The rotation angle is in radians as a parameter const ctx = canvas.getContext('2d')
        if (direction === 'right') {
          canvas.width = height
          canvas.height = width
          ctx.rotate(90 * Math.PI / 180)
          ctx.drawImage(img, 0, 0, width, -height)
        } else {
          canvas.width = height
          canvas.height = width
          ctx.rotate(-90 * Math.PI / 180)
          ctx.drawImage(img, 0, 0, -width, height)
        }
        // Convert the rotated image back to base64
        this.uploadImage = this.getBase64Image(img, canvas)
      }
    },
    /**
    * Convert the image file to base64
    */
    getBase64Image (img, canvas) {
      const ctx = canvas.getContext('2d')
      ctx.drawImage(img, 0, 0, img.width, img.height)
      const ext = img.src.substring(img.src.lastIndexOf('.') + 1).toLowerCase()
      return canvas.toDataURL('image/' + ext)
    }

Note: If the image is obtained from a cross-domain, there may be problems converting canvas back to base64. In this case, you may need to enable a proxy to obtain the image.

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • JS implements the method of following the mouse to flip the picture in three dimensions
  • How to implement simple image flipping with JavaScript
  • js method to achieve button control 360 degree flip effect of picture
  • JS control image flip sample code (compatible with firefox, ie, chrome)

<<:  Mysql example of querying all lower-level multi-level sub-departments based on a certain department ID

>>:  Start nginxssl configuration based on docker

Recommend

Detailed explanation of the use of Arguments object in JavaScript

Table of contents Preface Basic Concepts of Argum...

Stop using absolute equality operators everywhere in JS

Table of contents Overview 1. Test for null value...

JavaScript canvas realizes dynamic point and line effect

This article shares the specific code for JavaScr...

Two ways to implement HTML to randomly drag content positions

Test: Chrome v80.0.3987.122 is normal There are t...

WeChat applet realizes taking photos and selecting pictures from albums

This article shares the specific code for WeChat ...

Details on how to write react in a vue project

We can create jsx/tsx files directly The project ...

JavaScript canvas Tetris game

Tetris is a very classic little game, and I also ...

Reduce memory and CPU usage by optimizing web pages

Some web pages may not look large but may be very ...

Implementation of automatic completion of Docker commands

Preface I don't know how long this friend has...

Implementation of scheduled backup in Mysql5.7

1. Find mysqldump.exe in the MySQL installation p...

Vue implements partial refresh of the page (router-view page refresh)

Using provide+inject combination in Vue First you...

JavaScript exquisite snake implementation process

Table of contents 1. Create HTML structure 2. Cre...

Solution to MySQL remote connection failure

I have encountered the problem that MySQL can con...

Vue implements form data validation example code

Add rules to the el-form form: Define rules in da...

WeChat Mini Programs Achieve Seamless Scrolling

This article example shares the specific code for...