Home WeChat Applet Mini Program Development Use Canvas to achieve the bubble effect of likes during live broadcast

Use Canvas to achieve the bubble effect of likes during live broadcast

Sep 12, 2017 am 10:05 AM
canvas live streaming

Firstly send Canvas to implement the live broadcast like bubble effect diagram:

Implementation details:

1.JS:

drawImage:function(data){
var that = this 
    var p10= data[0][0];  /* 三阶贝塞尔曲线起点坐标值*/ 
    var p11= data[0][1];  /* 三阶贝塞尔曲线第一个控制点坐标值*/ 
    var p12= data[0][2];  /* 三阶贝塞尔曲线第二个控制点坐标值*/ 
    var p13= data[0][3];  /* 三阶贝塞尔曲线终点坐标值*/ 
    var p20= data[1][0]; 
    var p21= data[1][1]; 
    var p22= data[1][2]; 
    var p23= data[1][3]; 
    var p30= data[2][0]; 
    var p31= data[2][1]; 
    var p32= data[2][2]; 
    var p33= data[2][3]; 
    var t = factor.t; 
    /*计算多项式系数 (下同)*/   
    var cx1 = 3*(p11.x-p10.x); 
    var bx1 = 3*(p12.x-p11.x)-cx1; 
    var ax1 = p13.x-p10.x-cx1-bx1; 
    var cy1 = 3*(p11.y-p10.y); 
    var by1 = 3*(p12.y-p11.y)-cy1; 
    var ay1 = p13.y-p10.y-cy1-by1; 
    var xt1 = ax1*(t*t*t)+bx1*(t*t)+cx1*t+p10.x; 
    var yt1 = ay1*(t*t*t)+by1*(t*t)+cy1*t+p10.y; 
    var cx2 = 3*(p21.x-p20.x); 
    var bx2 = 3*(p22.x-p21.x)-cx2; 
    var ax2 = p23.x-p20.x-cx2-bx2; 
    var cy2 = 3*(p21.y-p20.y); 
    var by2 = 3*(p22.y-p21.y)-cy2; 
    var ay2 = p23.y-p20.y-cy2-by2; 
    var xt2 = ax2*(t*t*t)+bx2*(t*t)+cx2*t+p20.x; 
    var yt2 = ay2*(t*t*t)+by2*(t*t)+cy2*t+p20.y; 
    var cx3 = 3*(p31.x-p30.x); 
    var bx3 = 3*(p32.x-p31.x)-cx3; 
    var ax3 = p33.x-p30.x-cx3-bx3; 
    var cy3 = 3*(p31.y-p30.y); 
    var by3 = 3*(p32.y-p31.y)-cy3; 
    var ay3 = p33.y-p30.y-cy3-by3; 
    /*计算xt yt的值 */ 
    var xt3 = ax3*(t*t*t)+bx3*(t*t)+cx3*t+p30.x; 
    var yt3 = ay3*(t*t*t)+by3*(t*t)+cy3*t+p30.y; 
    factor.t +=factor.speed; 
    ctx.drawImage("../../images/heart1.png",xt1,yt1,30,30); 
    ctx.drawImage("../../images/heart2.png",xt2,yt2,30,30); 
    ctx.drawImage("../../images/heart3.png",xt3,yt3,30,30); 
    ctx.draw(); 
    if(factor.t>1){ 
      factor.t=0; 
      cancelAnimationFrame(timer); 
      that.startTimer(); 
    }else{ 
      timer =requestAnimationFrame(function(){ 
        that.drawImage([[{x:30,y:400},{x:70,y:300},{x:-50,y:150},{x:30,y:0}],[{x:30,y:400},{x:30,y:300},{x:80,y:150},{x:30,y:0}],[{x:30,y:400},{x:0,y:90},{x:80,y:100},{x:30,y:0}]]) 
     }) 
    }}
Copy after login

2. Principle:

a. By drawing three different third-order Bezier curves, select three pictures and let them move along their respective Bezier curves. The movement trajectories are as follows:

b. Calculate the mathematical expression of third-order Bezier curve x(t), y(t).

The third-order Bezier curve forms a curve through four points, two control points, a starting point and an end point.

The mathematical expressions of x(t), y(t) can be obtained by using polynomial coefficients:
cx = 3 * (x1 - x0)bx = 3 * (x2 - x1) - cxax = x3 - x0 - cx - bxcy = 3 * ( y1 - y0 ) by = 3 * ( y2 - y1 ) - cyay = y3 - y0 - cy - byx(t) = ax * t ^ 3 + bx * t ^ 2 + cx * t + x0y(t) = ay * t ^ 3 + by * t ^ 2 + cy * t + y0
Three Bezier curves are drawn here. Just apply the formula three times. No loop is used here. If When there are many Bezier curves, you can call ctx.drawImage in a loop, where factor.t is the parameter of the third-order Bezier curve, with a value range of [0,1], and finally call ctx.draw(), and set The timer can realize the movement of the picture along the Bezier curve.
3.Tip:

The timer used here is implemented through the requestAnimationFrame() function. The reason for deprecating setInterval is that there is a frame stuck phenomenon in the actual test and the animation display is slightly discontinuous.

The above is the detailed content of Use Canvas to achieve the bubble effect of likes during live broadcast. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to start a live broadcast on Douyin How to start a live broadcast on Douyin How to start a live broadcast on Douyin How to start a live broadcast on Douyin Feb 22, 2024 pm 04:20 PM

Click the Creator Service Center, click Start Live Broadcast, and select the content topic to start the live broadcast. Tutorial Applicable Model: iPhone 13 System: iOS 15.8 Version: Douyin 20.9.0 Analysis 1 Click the three horizontal lines in the upper right corner of Douyin’s personal homepage. 2Click Creator Service Center on the menu bar. 3Click all categories in the Creator Service Center. 4Enter the function list and click to start live broadcast. 5Select the content topic and click to start the live video broadcast. Supplement: How to delete works on Douyin 1. First enter the My page of Douyin and click on the video under the work. 2 Enter the video page and click the three-dot icon on the right. 3. In the menu bar that pops up at the bottom, click the delete icon. 4Finally, in the pop-up window at the bottom, click to confirm the deletion. Summary/notes

What causes the live broadcast to freeze? What causes the live broadcast to freeze? Nov 03, 2023 pm 04:26 PM

Live broadcast freezes are caused by network problems and video source problems. The solutions are as follows: 1. Insufficient network bandwidth, upgrade the network bandwidth, or try to avoid using other devices that occupy network bandwidth at the same time; 2. Network delay, optimize the network connection, and reduce network delay; 3. The network is unstable, check the network equipment, Make sure the network connection is stable; 4. The video source server load is too high, choose a video source with a lower load, or wait for the peak period to pass; 5. The video source quality is poor, choose a better quality video source or upgrade your network Bandwidth; 5. The video source is compressed, etc.

How to start a live broadcast on Xiaohongshu How to start a live broadcast on Xiaohongshu How to start a live broadcast on Xiaohongshu How to start a live broadcast on Xiaohongshu Mar 28, 2024 pm 01:50 PM

Xiaohongshu is a life community platform application that you are very familiar with. It has many functions and allows everyone to see a variety of information content at any time. There are many notes with pictures and texts. All of them can make everyone very satisfied, and sometimes you can see some live broadcast rooms, so everyone also wants to start a live broadcast and chat with everyone, but they don’t know how to start a live broadcast. The editor below I can also give you specific operation methods, I hope it can help you. How to start live streaming in Xiaohongshu: 1. First open Xiaohongshu and click + at the bottom of the homepage. 2. Then switch to live broadcast and click the start live broadcast entrance.

How to live broadcast on Douyin - Douyin live broadcast tutorial How to live broadcast on Douyin - Douyin live broadcast tutorial Mar 06, 2024 pm 09:00 PM

Many novice friends still don’t know how to live broadcast on Douyin, so the editor below has brought a detailed tutorial on Douyin live broadcast. Friends who need it, please take a look. Step 1: First open the Douyin software and enter the Douyin page, then click the [plus sign] icon directly below the page, as shown in the figure below; Step 2: After entering the page, click [Start Live Broadcast] in the lower right corner, as shown below As shown in the picture; Step 3: After entering this page, you can choose what kind of live broadcast to watch. After selecting, click [Start Live Broadcast], as shown in the picture below. The above is the entire content of how to live broadcast on Douyin brought to you by the editor. I hope it can be helpful to everyone.

How to watch live broadcast on PotPlayer? -PotPlayer to watch live tutorials How to watch live broadcast on PotPlayer? -PotPlayer to watch live tutorials Mar 19, 2024 pm 10:04 PM

Friends, do you know how to watch live broadcasts with PotPlayer? Today I will explain the tutorial for watching live broadcasts with PotPlayer. If you are interested, come and take a look with me. I hope it can help everyone. First we open PotPlayer, then select the multi-functional sidebar in the lower right corner of the software, and then we click; refer to the picture below and a playlist will pop up. We can select the "Add" option, where we can adjust the live broadcast settings and Add to. At this time, in the drop-down box that pops up, we choose to add a link. Of course, if we have a live broadcast source file, we can directly choose to add the file and then import the file. Then in the address box that pops up, we enter what we want to watch

How to implement short video and live broadcast functions in uniapp How to implement short video and live broadcast functions in uniapp Oct 20, 2023 am 08:24 AM

How to implement short video and live broadcast functions in uniapp With the development of mobile Internet, short videos and live broadcasts have become a hot trend in the field of social entertainment. Implementing short video and live broadcast functions in uniapp allows developers to create more engaging and interactive applications. This article will introduce how to implement short video and live broadcast functions in uniapp, and provide specific code examples. Implementation of short video functions Short video functions mainly include video recording, editing and sharing. The following are the steps to implement the short video function in uniapp

How to enable Douyin live broadcast How to enable Douyin live broadcast Mar 26, 2024 pm 04:01 PM

Douyin live broadcast is becoming more and more popular. I believe many users want to start live broadcast and give it a try. So how do we start Douyin live broadcast? In fact, starting Douyin live broadcast requires a series of live broadcast procedures. Below, the editor will bring you a detailed overview of the entire process of starting Douyin live broadcast. Come and take a look. Douyin usage tutorial: How to start Douyin live broadcast 1. First, we open Douyin, click the + button as shown in the bottom of the main interface to shoot videos or start live broadcast. 2. Then we enter the live broadcast page. We need to select our own live broadcast type. We can choose according to our own equipment and actual situation. Click to start the video live broadcast. 3. Then the most important step before we start the live broadcast is real-name authentication. Enter your relevant information and click the same button.

How to watch the replay of Xiaohongshu live broadcast How to watch the replay of Xiaohongshu live broadcast Mar 21, 2024 pm 08:52 PM

How to watch the replay of Xiaohongshu live broadcast? You can watch the live broadcast replay in the Xiaohongshu APP. Most users don’t know how to watch the live broadcast replay. Next, the editor brings to users the pictures and texts of how to watch the replay of Xiaohongshu live broadcast. Tutorial, interested users come and take a look! Xiaohongshu usage tutorial How to watch the replay of Xiaohongshu live broadcast 1. First open the Xiaohongshu APP, enter the main page and click [My] in the lower right corner; 2. Then in the My function area, select the [three horizontal lines] in the upper left corner Line]; 3. Then expand the function bar on the left and click [Creation Center]; 4. Then enter the creation service page and select [Anchor Center]; 5. Finally jump to the page below and click [Live Broadcast] Playback].

See all articles