Small program lightweight like animation, based on CSS background map

In the stream 2021-06-18 01:33:43
small program lightweight animation based


Origin of the problem

Click like animation effect in punch duck app

advantage

Fireworks animation , Its animation effect is produced randomly . According to the UI In terms of designers , It can promote users to like more .

The problem is

The phenomenon : In the punch list of the applet , give the thumbs-up 50 After more than one time , Maybe you can't continue to like it .

I guess the reason is to create 50 The canvas above Canvas, Every canvas loads 10 A picture , This leads to insufficient performance of applets , Stuck .

Complete source code

Complete source code , Please blog this link in the stream ( Click to see the original ) View in SoureMap To obtain a .

Small program fireworks like animation description : Based on small programs Canvas Of ctx.draw() To achieve , Thanks to the Taro H5 There are corresponding draw It's the code that shows .

Part of the source code is as follows

const ctx = Taro.createCanvasContext('like-animation')

let _rw = canvasWidth / 300
let _rh = canvasHeight / 150

likeTimer = setInterval(() => {

  likeList = likeList.filter(_l => _l.d >= -30).map(_l => {
    _l = nextXY(_l)
    if (_l.d <= 0) {
      ctx.save()
      ctx.scale(_rw, _rh)
      ctx.beginPath()
      if (-_l.d > 20) {
        let _ga = (_l.d + 30) / 10
        ctx.globalAlpha = _ga > 0.2 ? _ga : 0
      }
      ctx.drawImage(_l.img, _l.x, _l.y, 1818)
      ctx.fill()
      ctx.restore()
    }
    return _l
  })

  if (likeList.length === 0) {
    clear(options)
  }

  ctx.draw()
}, 30)

//  Calculate the position of the picture
function nextXY({ a, x, y, h, k, s, d, img }{
  if (d <= 0) {
    x = x + s
    y = (a * Math.pow(x - h, 2)) + k
  }
  d = d - 3
  return { a, x, y, h, k, s, d, img }
}

Excellent articles in the industry

Industry solutions

Live studio animation

Like animation found on Nuggets , Most of them are live studio animations , The realization principle is Canvas Draw pictures on , Of course, it can also be used CSS The animation . For details, please refer to 《H5 How to realize the live crazy like animation ?( Complete source code attached )》.

  • Use CSS3 Animation, You need to create more animation elements , Add different animation delays .
  • Use Canvas, You need to create a canvas , And load multiple graphics resources , Suitable for a single scene ( Such as live room )

Twitter animation

stay Medium I found 《How Did They Do That? The Twitter “Like” Animation.》 It's based on sprite maps .

 Sprite
  • The advantage is , It's done with a single element .
  • The difficulties are : Sprite has many animation details , More for designers , Developers can't configure the details themselves , Such as animation delay time and execution time

And in the CodePen Most of the animations found are SVG Realized , The effect is very good . But it can't be used directly in small programs SVG Animators , It can only be used as a background image to show .

Like the idea of animation

First of all, thank you "XboxYan" 《CSS It's a button that works with particles 》. I learned from its many circular gradients , Change the image of background-size and background-position Principle .

The effect I achieved

Round effect

  • The like figure is a circle
  • Like animation is by changing background-size and background-position To complete
.like-icon:before {
  background-size10px 10px20px 20px15px 15px20px 20px18px 18px10px 10px15px 15px10px 10px18px 18px15px 15px20px 20px18px 18px20px 20px15px 15px10px 10px20px 20px;
  background-position50% 50%50% 50%50% 50%50% 50%50% 50%50% 50%50% 50%50% 50%50% 50%50% 50%50% 50%50% 50%50% 50%50% 50%50% 50%50% 50%;
}
#checkbox:checked + label .like-icon:before {
  opacity1;
  background-size0px 0px;
  background-position5% 65%1% 44%8% 25%20% 12%40% 2%52% 0%65% 5%78% 15%95% 30%100% 55%8% 85%23% 100%42% 105%60% 99%75% 90%92% 84%;
  transition: opacity 0.25s, background-position .35s ease-in-out, background-size .5s ease-in-out;
}

The heart effect

  • Like graphics changed from circle to heart
  • Animation icon added the effect of switching zoom , With a little bit of elasticity
/*  heart-shaped  */
#checkbox:checked + label .like-icon:before {
  background-position5% 65%1% 44%8% 25%20% 12%40% 22%52% 40%65% 25%78% 15%95% 30%100% 55%8% 85%23% 100%42% 105%60% 99%75% 90%92% 84%;
}

/*  Like icon elastic effect  */
.like-icon {
  backgroundurl('/images/posts/like-css3-animation/like-blue.png'), url('/images/posts/like-css3-animation/like-gray.png');
  background-size0px18px;
}
#checkbox:checked + label .like-icon {
  background-size18px0;
  transformscale(1.05);
  transition: background-size .4s ease-in-out, transform 0.5s cubic-bezier(.97,.01,.76,4);
}

Heart animation tuning

In the actual digital change of like animation , There will be “ give the thumbs-up →1”、“9→10” The situation of .

  • Overall left , The animation effect is ideal
  • The whole is in the middle or on the right , When there is a change in the text , The position of the heart changes , At this point, you need to add some animation delay
/*  heart-shaped  */
#checkbox:checked + label .like-icon:before {
  opacity1;
  background-size0px 0px;
  background-position5% 65%1% 44%8% 25%20% 12%40% 22%52% 40%65% 25%78% 15%95% 30%100% 55%8% 85%23% 100%42% 105%60% 99%75% 90%92% 84%;
  transition: opacity 0.25s, background-position .35s ease-in-out, background-size .5s ease-in-out;
  transition-delay0.2s;
}

/*  Like icon elastic effect  */
.like-icon {
  backgroundurl('/images/posts/like-css3-animation/like-blue.png'), url('/images/posts/like-css3-animation/like-gray.png');
  background-size0px18px;
}
#checkbox:checked + label .like-icon {
  background-size18px0;
  transformscale(1.05);
  transition: background-size .4s ease-in-out, transform 0.5s cubic-bezier(.97,.01,.76,4);
  transition-delay0.2s;
}

summary

The animation in the studio , Use CSS3 Animation or Canvas It's going to be great , It just needs more animation elements . And I want to use One or two DOM Element coordination CSS Achieve more lightweight animation .

The next possible challenge

Josh God's praise animation is great , Next time I want to challenge to do a similar .

Preview URL :Building a Modern-Day Hit Counter

thank

  • Thank you, Li Bo 、 Xiaogu students before the realization of the complete point like animation
  • Thanks Shuai 、 The animation modification suggestions provided by Lao Wu
  • Thanks for my family's support
版权声明
本文为[In the stream]所创,转载请带上原文链接,感谢
https://qdmana.com/2021/05/20210530150659188w.html

  1. HTML + CSS + JavaScript to achieve cool Fireworks (cloud like particle text 3D opening)
  2. HTML + CSS + JavaScript realizes 520 advertising love tree (including music), which is necessary for programmers to express themselves
  3. Solve the problem of Web front-end deployment server (it can be deployed online without a server)
  4. HTML + CSS + JS make wedding countdown web page template (520 / Tanabata Valentine's Day / programmer advertisement)
  5. What else can driverless minibus do besides "Park connection"?
  6. Cloud native leads the era of all cloud development
  7. NRM mirror source management tool
  8. Bring it to you, flex Jiugong
  9. Lolstyle UI component development practice (II) -- button group component
  10. Deconstruction assignment in ES6
  11. Luo 2 peerless Tang clan was officially launched. The official gave a key point, and the broadcast time was implied
  12. 20初识前端HTML(1)
  13. 当新零售遇上 Serverless
  14. 20 initial knowledge of front-end HTML (1)
  15. When new retail meets serverless
  16. [golang] - go into go language lesson 5 type conversion
  17. [golang] - go into go language lesson 6 conditional expression
  18. HTML5(八)——SVG 之 path 详解
  19. HTML5 (8) -- detailed explanation of SVG path
  20. 需要开通VIP以后页面内容才能复制怎么办?控制台禁用javascript即可
  21. Web前端|CSS入门教程(超详细的CSS使用讲解,适合前端初学者)
  22. 实践积累 —— 用Vue3简单写一个单行横向滚动组件
  23. Serverless 全能选手,再下一城
  24. What if you need to open a VIP to copy the page content? Just disable JavaScript on the console
  25. Web front end | CSS introductory tutorial (super detailed CSS explanation, suitable for front-end beginners)
  26. Practice accumulation - write a single line horizontal scroll component simply with vue3
  27. Dili Reba is thin again. She looks elegant and high in a strapless hollow skirt, and her "palm waist" is beautiful to a new height
  28. Serverless all-round player, next city
  29. The difference between MySQL semi synchronous replication and lossless semi synchronous replication
  30. Vue表单设计器的终极解决方案
  31. The ultimate solution for Vue form designer
  32. Nginx从理论到实践超详细笔记
  33. Yu Shuxin's red backless swimsuit is split to the waist and tail, with a concave convex figure and excessive color matching, and his face is white to dazzling
  34. Nginx ultra detailed notes from theory to practice
  35. 【动画消消乐|CSS】086.炫酷水波浪Loading过渡动画
  36. typecho全站启用https
  37. CCTV has another popular employee. The off-site interpretation is very professional, and the appearance ability is no less than that of Wang Bingbing
  38. [animation Xiaole | CSS] 086. Cool water wave loading transition animation
  39. Enable HTTPS in Typecho
  40. 50天用JavaScript完成50个web项目,我学到了什么?
  41. 根据JavaScript中原生的XMLHttpRequest实现jQuery的Ajax
  42. What have I learned from completing 50 web projects with JavaScript in 50 days?
  43. "My neighbor doesn't grow up" has hit the whole network. There are countless horse music circles, and actor Zhou Xiaochuan has successfully made a circle
  44. 根据JavaScript中原生的XMLHttpRequest实现jQuery的Ajax
  45. Implement the Ajax of jQuery according to the native XMLHttpRequest in JavaScript
  46. Implement the Ajax of jQuery according to the native XMLHttpRequest in JavaScript
  47. 30 + women still wear less T-shirts and jeans. If they wear them like stars, they will lose weight
  48. 数栈技术分享前端篇:TS,看你哪里逃~
  49. Several stack technology sharing front end: TS, see where you escape~
  50. 舍弃Kong和Nginx,Apache APISIX 在趣链科技 BaaS 平台的落地实践
  51. Abandon the landing practice of Kong and nginx, Apache apisik on the baas platform of fun chain technology
  52. 浪迹天涯king教你用elementui做复杂的表格,去处理报表数据(合并表头,合并表体行和列)
  53. 前端HTML两万字图文大总结,快来看看你会多少!【️熬夜整理&建议收藏️】
  54. Wandering around the world king teaches you to use elementui to make complex tables and process report data (merge header, merge table body rows and columns)
  55. 路由刷新数据丢失 - vuex数据读取的问题
  56. Front end HTML 20000 word graphic summary, come and see how much you can【 Stay up late to sort out & suggestions]
  57. Route refresh data loss - vuex data reading problem
  58. Systemctl系统启动Nginx服务脚本
  59. Systemctl system startup nginx service script
  60. sleepless