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

Understanding of web design layout

<br />A contradiction arises. In small works...

Zabbix monitoring docker application configuration

The application of containers is becoming more an...

js to realize login and registration functions

This article example shares the specific code of ...

A complete list of commonly used MySQL functions (classified and summarized)

1. Mathematical Functions ABS(x) returns the abso...

How to implement nginx smooth restart

1. Background During the server development proce...

Solve the problem of using linuxdeployqt to package Qt programs in Ubuntu

I wrote some Qt interface programs, but found it ...

Implementation of multiple instances of tomcat on a single machine

1. Introduction First of all, we need to answer a...

An example of how to implement an adaptive square using CSS

The traditional method is to write a square in a ...

JavaScript to achieve product magnifying glass effect

This article shares the specific code of JavaScri...

Ubuntu compiles kernel modules, and the content is reflected in the system log

Table of contents 1.Linux login interface 2. Writ...

SQL implementation of LeetCode (181. Employees earn more than managers)

[LeetCode] 181.Employees Earning More Than Their ...

How to use IDEA to create a web project and publish it to tomcat

Table of contents Web Development 1. Overview of ...

Two ways to create SSH server aliases in Linux

Preface If you frequently access many different r...