• [七夕节]——一款基于canvas绘制五颜六色线条合成一个爱心发光动画特效


    Canvas基本介绍

    Canvas介绍
    1.canvas是html5的一个新标签,属于h5的新特性
    2.canvas标签是一个图形的容器,简单点说就是一块画布,你可以在上画矩形,圆形,三角形,折线等等,也可以用来画logo
    3.它是通过javascript来画的,即脚本绘制图形

    canvas可以用来干啥呢?
    1.制作web网页游戏(但是如果代码写的不咋的游戏可能会非常卡)
    2.数据可视化(这么说你可能不明白,但我告诉你echarts就是基于canvas)
    3.广告banner的动态效果非常适合用canvas制作
    4.canvas还可以用来内嵌一些网页
     

    基本用法

    标签只有两个属性 – width 和 height,所以在低版本的浏览器是不支持的,要在浏览器中写上 :

    "tutorial" width="150" height="150">浏览器版本不支持
    

     在没有设置宽高的时候,默认初始化为 :

    <canvas width='300px' height='150px'>canvas>
    

    浏览器支持

    • Internet Explorer 9、Firefox、Opera、Chrome 以及 Safari 支持 及其属性和方法。

    **注释:**Internet Explorer 8 以及更早的版本不支持元素。

     

    临摹canvas小案例

     实现代码:

    1. html>
    2. <html lang="en" >
    3. <head>
    4. <meta charset="UTF-8">
    5. <title>HTML5线条照射爱心动画特效title>
    6. <link rel="stylesheet" href="css/zzsc.css">
    7. head>
    8. <body>
    9. <canvas width="300" height="300" style="width:100%;height:100vh;" id="c">canvas>
    10. <script src="js/zzsc.js">script>
    11. body>
    12. html>
    1. // zzsc.js
    2. /* DaTouWang URL: www.datouwang.com */
    3. var canvas = document.getElementById("c");
    4. var ctx = canvas.getContext("2d");
    5. var height = void 0,
    6. width = void 0,
    7. innerpoints = [],
    8. outerpoints = [],
    9. particles = [];
    10. var noofpoints = 200,
    11. trashold = 10;
    12. var x = void 0,
    13. y = void 0,
    14. p = void 0,
    15. n = void 0,
    16. point = void 0,
    17. dx = void 0,
    18. dy = void 0,
    19. color = void 0;
    20. (deltaangle = (Math.PI * 2) / noofpoints), (r = Math.min(height, width) * 0.5);
    21. var distance = function distance(x1, y1, x2, y2) {
    22. return Math.sqrt(Math.pow(y2 - y1, 2) + Math.pow(x2 - x1, 2));
    23. };
    24. var mapVal = function mapVal(num, in_min, in_max, out_min, out_max) {
    25. return ((num - in_min) * (out_max - out_min)) / (in_max - in_min) + out_min;
    26. };
    27. var resize = function resize() {
    28. height = ctx.canvas.clientHeight;
    29. width = ctx.canvas.clientWidth;
    30. if (
    31. ctx.canvas.clientWidth !== canvas.width ||
    32. ctx.canvas.clientHeight !== canvas.height
    33. ) {
    34. console.log("resized");
    35. canvas.width = width;
    36. canvas.height = height;
    37. ctx.translate(canvas.width / 2, canvas.height / 2);
    38. ctx.rotate(-Math.PI);
    39. innerpoints = [];
    40. r = 10;
    41. for (var i = deltaangle; i <= Math.PI * 2; i += deltaangle) {
    42. x = r * 16 * Math.pow(Math.sin(i), 3);
    43. y =
    44. r *
    45. (13 * Math.cos(i) -
    46. 5 * Math.cos(2 * i) -
    47. 2 * Math.cos(3 * i) -
    48. Math.cos(4 * i));
    49. innerpoints.push({
    50. x: x,
    51. y: y,
    52. });
    53. x = 10 * r * 16 * Math.pow(Math.sin(i), 3);
    54. y =
    55. 10 *
    56. r *
    57. (13 * Math.cos(i) -
    58. 5 * Math.cos(2 * i) -
    59. 2 * Math.cos(3 * i) -
    60. Math.cos(4 * i));
    61. outerpoints.push({
    62. x: x,
    63. y: y,
    64. });
    65. var step = random(0.001, 0.003, true);
    66. particles.push({
    67. step: step,
    68. x: x,
    69. y: y,
    70. });
    71. }
    72. }
    73. };
    74. var random = function random(min, max, isFloat) {
    75. if (isFloat) {
    76. return Math.random() * (max - min) + min;
    77. }
    78. return ~~(Math.random() * (max - min) + min);
    79. };
    80. resize();
    81. //particles = [...outerpoints];
    82. ctx.globalAlpha = 0.5;
    83. ctx.globalCompositeOperation = "source-over";
    84. var draw = function draw() {
    85. ctx.fillStyle = "rgba(0,0,0,0.03)";
    86. ctx.fillRect(-width, -height, width * 2, height * 2);
    87. ctx.beginPath();
    88. for (var i = 0; i < innerpoints.length; i++) {
    89. s = outerpoints[i];
    90. d = innerpoints[i];
    91. point = particles[i];
    92. if (distance(point.x, point.y, d.x, d.y) > 10) {
    93. dx = d.x - s.x;
    94. dy = d.y - s.y;
    95. point.x += dx * point.step;
    96. point.y += dy * point.step;
    97. color = distance(0, 0, point.x, point.y);
    98. ctx.beginPath();
    99. ctx.fillStyle = "hsl(" + (color % 360) + ",100%,50%)";
    100. ctx.arc(point.x, point.y, 2, 0, Math.PI * 2, false);
    101. ctx.closePath();
    102. ctx.fill();
    103. } else {
    104. point.x = d.x;
    105. point.y = d.y;
    106. ctx.beginPath();
    107. ctx.arc(point.x, point.y, 2, 0, Math.PI * 2, false);
    108. ctx.closePath();
    109. ctx.fill();
    110. particles[i].x = s.x;
    111. particles[i].y = s.y;
    112. particles[i].step = random(0.001, 0.003, true);
    113. }
    114. }
    115. };
    116. var render = function render() {
    117. resize();
    118. draw();
    119. requestAnimationFrame(render);
    120. };
    121. requestAnimationFrame(render);
    1. /* zzsc.css */
    2. /* DaTouWang URL: www.datouwang.com */
    3. html,
    4. body {
    5. border: 0;
    6. padding: 0;
    7. margin: 0;
    8. overflow: hidden;
    9. background: #000;
    10. }
    11. .info {
    12. z-index: 999;
    13. position: absolute;
    14. left: 0;
    15. top: 0;
    16. padding: 10px;
    17. color: #fff;
    18. background: rgba(0, 0, 0, 0.5);
    19. text-transform: capitalize;
    20. }

     

     

     

  • 相关阅读:
    快速上手Django(七) -Django之登录cookie和session
    51.【结构体初始化的两种方法】
    Postgresql systemctl设置开机自启动
    CockroachDB-哈希分片索引
    AUC性能指标计算方法及优缺点
    35、Flink 的 Formats 之CSV 和 JSON Format
    LeetCode 47 全排列 II - Java 实现
    MemoryAnalyzer分析线上OOM异常
    AI实战营第二期 第五节 《目标检测与MMDetection》——笔记6
    微服务开发平台 Spring Cloud Blade 部署实践
  • 原文地址:https://blog.csdn.net/LUZENG_SA/article/details/126124718