


How to implement element picture mirror flip animation effect on canvas
This article mainly introduces the relevant information on the method of realizing the mirror flip animation effect of element pictures on canvas. The content is quite good. I will share it with you now and give it as a reference.
1. Canvas picture horizontal mirror flip effect preview
You can click here: Canvas picture horizontal mirror flip animation demo
The animation effect of clicking the picture on the demo page is visible.
2. Implementation of image flipping on Canvas
It is relatively simple to achieve the flipping effect of elements in CSS. For example, we want To flip a picture horizontally, you only need one line of CSS:
img { transform: scaleX(-1); }
or:
img { transform: scale(-1, 1); }
But in canvas, it is more troublesome. The trouble is not that it cannot be flipped, but that it cannot be flipped. is the positioning of the coordinate system.
In Canvas, the following code can achieve horizontal mirror flipping of resources (assuming context is the 2d context of Canvas):
context.scale(-1, 1);
Or use the setTransform API for direct matrix transformation:
context.setTransform(-1, 0, 0, 1, 0, 0);
However, although flipping is implemented, there is a big problem with the positioning of elements in Canvas. This is because the coordinate transformation system of Canvas is different from that of CSS. Therefore, if we want to achieve a centered flip effect, we need to move the center point of the target element to the transformation axis before flipping.
Take the horizontal flip distance, translate the horizontal offset after the displacement transformation before scale, and then you can see the effect of center flipping.
The language is pale, so let’s take a picture to illustrate.
The default change coordinate system of canvas is the upper left corner.
Therefore, if the horizontal scale is 1, 0.5, 0, -0.5, -1, the final position is as shown below:
So we can get the corresponding The horizontal distance formula for offset:
distance = (canvas.width – image.width * scale) / 2;
So, the key code for final mirror drawing of the picture It becomes like this (assuming the horizontal zoom size is scale):
// 坐标参考调整 context.translate((canvas.width - image.width * scale) / 2, 0); context.scale(scale, 1); context.drawImage(image, 0, 0); // 坐标参考还原 context.setTransform(1, 0, 0, 1, 0, 0);
How to increase the animation effect?
We can use Tween.js, https://github.com/zhangxinxu/tween
There are various easing algorithms, and with the help of the convenient Math.animation() method, We can easily achieve the effect we want!
Math.animation(form, to, duration, easing, callback);
The animation JS is as follows:
var canvas = document.querySelector('canvas'); var context = canvas.getContext('2d'); // 动画进行 Math.animation(1, -1, 600, 'Quad.easeInOut', function (value, isEnding) { // 清除画布内容 context.clearRect(0, 0, canvas.width, canvas.height); // 调整坐标 context.translate((canvas.width - canvas.width * value) / 2, 0); // 调整缩放 context.scale(value, 1); // 绘制此时图片 context.drawImage(eleImg, 0, 0); // 坐标参考还原 context.setTransform(1, 0, 0, 1, 0, 0); });
3. Conclusion
Another cold article, canvas. There are not many front-end players, the audience is limited, and it is no more popular than popular technologies. However, as the old saying goes, no good deed is too small to be neglected. I hope that some friends can provide help when they search for related issues in the future.
The above is the entire content of this article. I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website!
Related recommendations:
html5 uses canvas to draw secondary tree structure diagrams
canvas draws various basic graphics
Use Canvas to imitate the method of loading small balls on Baidu Tieba client
##
The above is the detailed content of How to implement element picture mirror flip animation effect on canvas. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Schools using canvas include Stanford University, MIT, Columbia University, University of California, Berkeley, etc. Detailed introduction: 1. Stanford University uses Canvas as its main online learning platform. Teachers and students at Stanford University use Canvas to manage and communicate course content, and learn through functions such as online discussions, assignment submissions, and exams; 2. Ma Provincial Polytechnic Institute and MIT also use Canvas as their online learning management system and conduct course management through the Canvas platform; 3. Columbia University, etc.

The canvas arrow plug-ins include: 1. Fabric.js, which has a simple and easy-to-use API and can create custom arrow effects; 2. Konva.js, which provides the function of drawing arrows and can create various arrow styles; 3. Pixi.js , which provides rich graphics processing functions and can achieve various arrow effects; 4. Two.js, which can easily create and control arrow styles and animations; 5. Arrow.js, which can create various arrow effects; 6. Rough .js, you can create hand-drawn arrows, etc.

The details of the canvas clock include clock appearance, tick marks, digital clock, hour, minute and second hands, center point, animation effects, other styles, etc. Detailed introduction: 1. Clock appearance, you can use Canvas to draw a circular dial as the appearance of the clock, and you can set the size, color, border and other styles of the dial; 2. Scale lines, draw scale lines on the dial to represent hours or minutes. Position; 3. Digital clock, you can draw a digital clock on the dial to indicate the current hour and minute; 4. Hour hand, minute hand, second hand, etc.

The versions of html2canvas include html2canvas v0.x, html2canvas v1.x, etc. Detailed introduction: 1. html2canvas v0.x, which is an early version of html2canvas. The latest stable version is v0.5.0-alpha1. It is a mature version that has been widely used and verified in many projects; 2. html2canvas v1.x, this is a new version of html2canvas.

How to use canvas to draw charts and animation effects in uniapp requires specific code examples 1. Introduction With the popularity of mobile devices, more and more applications need to display various charts and animation effects on the mobile terminal. As a cross-platform development framework based on Vue.js, uniapp provides the ability to use canvas to draw charts and animation effects. This article will introduce how uniapp uses canvas to achieve chart and animation effects, and give specific code examples. 2. canvas

Invalid styles include CSS3 animations and transitions, CSS filter effects, CSS3 complex graphics and paths, some CSS3 features, pseudo elements and some CSS features, Z-index, background images and gradients, etc. Detailed introduction: 1. CSS3 animation and transition: html2canvas may not fully capture CSS3 animation and transition effects. Although attempts will be made to capture the final style, these animations and transitions may be lost during the conversion process; 2. CSS filter effects: filters such as blur and shadow may not be retained during the conversion process, etc.

With the rapid development of science and technology and the widespread application of information technology in the field of education, Canvas, as a world-leading online learning management system, is gradually emerging in the Chinese education industry. The emergence of Canvas provides new possibilities for the reform of education and teaching methods in China. This article will explore the development trends and prospects of Canvas in China’s education sector. First of all, one of the development trends of Canvas in China’s education sector is in-depth integration. With the rapid development of cloud computing, big data and artificial intelligence, Canvas will increasingly

The tkinter canvas attributes include bg, bd, relief, width, height, cursor, highlightbackground, highlightcolor, highlightthickness, insertbackground, insertwidth, selectbackground, selectforeground, xscrollcommand attributes, etc. Detailed introduction
