当前位置:Gxlcms > html代码 > Canvas制作旋转太极的动画

Canvas制作旋转太极的动画

时间:2021-07-01 10:21:17 帮助过:38人阅读

这次给大家带来Canvas制作旋转太极的动画,Canvas制作旋转太极动画的注意事项有哪些,下面就是实战案例,一起来看一下。

前言

好久没动canvas了,今下午突然想回顾一下,就写了个旋转的太极,哈哈,蛮好玩的,在这里就将自己写的过程展示出来,旋转使用的css实现的,没有用canvas自己的,希望大佬们不要吐槽。

css

  1. body{
  2. background: #ddd;
  3. }
  4. #canvas{
  5. position: absolute;
  6. left: 40%;
  7. top: 30%;
  8. -webkit-transform: translate(-50%,-50%);
  9. -moz-transform: translate(-50%,-50%);
  10. -ms-transform: translate(-50%,-50%);
  11. -o-transform: translate(-50%,-50%);
  12. transform: translate(-50%,-50%);
  13. -webkit-animation: testAnimate 3s linear infinite;
  14. -o-animation: testAnimate 3s linear infinite;
  15. animation: testAnimate 3s linear infinite;
  16. }
  17. @keyframes testAnimate {
  18. from {
  19. -webkit-transform: rotate(0);
  20. -moz-transform: rotate(0);
  21. -ms-transform: rotate(0);
  22. -o-transform: rotate(0);
  23. transform: rotate(0);
  24. }
  25. to {
  26. -webkit-transform: rotate(360deg);
  27. -moz-transform: rotate(360deg);
  28. -ms-transform: rotate(360deg);
  29. -o-transform: rotate(360deg);
  30. transform: rotate(360deg);
  31. }
  32. }

html

  1. <body>
  2. <canvas id="canvas" width="500" height="500"></canvas>
  3. </body>

js

  1. let ctx = document
  2. .getElementById("canvas")
  3. .getContext("2d");
  4. // left-black-big
  5. ctx.beginPath();
  6. ctx.fillStyle = "#000";
  7. ctx.arc(250,250,200,Math.PI/2,Math.PI*1.5,false);
  8. ctx.closePath();
  9. ctx.fill();
  10. // right-white-big
  11. ctx.beginPath();
  12. ctx.fillStyle = "#fff";
  13. ctx.arc(250,250,200,Math.PI/2,Math.PI*1.5,true);
  14. ctx.closePath();
  15. ctx.fill();
  16. // top-black-middle
  17. ctx.beginPath();
  18. ctx.fillStyle = "#000";
  19. ctx.arc(250,150,100,Math.PI/2,Math.PI*1.5,true);
  20. ctx.closePath();
  21. ctx.fill();
  22. // bottom-white-middle
  23. ctx.beginPath();
  24. ctx.fillStyle = "#fff";
  25. ctx.arc(250,350,100,Math.PI/2,Math.PI*1.5,false);
  26. ctx.closePath();
  27. ctx.fill();
  28. // top-white-small
  29. ctx.beginPath();
  30. ctx.fillStyle = "#fff";
  31. ctx.arc(250,150,25,0,Math.PI*2);
  32. ctx.closePath();
  33. ctx.fill();
  34. // bottom-black-small
  35. ctx.beginPath();
  36. ctx.fillStyle = "#000";
  37. ctx.arc(250,350,25,0,Math.PI*2);
  38. ctx.closePath();
  39. ctx.fill();

效果

相信看了本文案例你已经掌握了方法,更多精彩请关注Gxl网其它相关文章!

推荐阅读:

H5中APP监听返回事件处理

h5实现多图片预览上传及点击可拖拽控件

以上就是Canvas制作旋转太极的动画的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行