93破解-安卓破解-原创软件-技术资源分享-www.93pojie.com

 找回密码
立即注册
查看: 1220|回复: 89

[编程学习] HTML实现爱心旋转代码

  [复制链接]
发表于 2022-11-14 15:58:42 | 显示全部楼层 |阅读模式
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
  2. <HTML>
  3.   
  4. <HEAD>
  5.   <TITLE> New Document </TITLE>
  6.   <META NAME="Generator" CONTENT="EditPlus">
  7.   <META NAME="Author" CONTENT="">
  8.   <META NAME="Keywords" CONTENT="">
  9.   <META NAME="Description" CONTENT="">
  10.   <style>
  11.     html,
  12.     body {
  13.       height: 100%;
  14.       padding: 0;
  15.       margin: 0;
  16.       background: #000;
  17.     }
  18.   
  19.     canvas {
  20.       position: absolute;
  21.       width: 100%;
  22.       height: 100%;
  23.     }
  24.   </style>
  25. </HEAD>
  26.   
  27. <BODY>
  28.   <canvas id="pinkboard"></canvas>
  29.   <script>
  30.     /*
  31.    * Settings
  32.    */
  33.     var settings = {
  34.       particles: {
  35.         length: 500, // maximum amount of particles
  36.         duration: 2, // particle duration in sec
  37.         velocity: 100, // particle velocity in pixels/sec
  38.         effect: -0.75, // play with this for a nice effect
  39.         size: 30, // particle size in pixels
  40.       },
  41.     };
  42.   
  43.     /*
  44.      * RequestAnimationFrame polyfill by Erik Möller
  45.      */
  46.     (function () { var b = 0; var c = ["ms", "moz", "webkit", "o"]; for (var a = 0; a < c.length && !window.requestAnimationFrame; ++a) { window.requestAnimationFrame = window[c[a] + "RequestAnimationFrame"]; window.cancelAnimationFrame = window[c[a] + "CancelAnimationFrame"] || window[c[a] + "CancelRequestAnimationFrame"] } if (!window.requestAnimationFrame) { window.requestAnimationFrame = function (h, e) { var d = new Date().getTime(); var f = Math.max(0, 16 - (d - b)); var g = window.setTimeout(function () { h(d + f) }, f); b = d + f; return g } } if (!window.cancelAnimationFrame) { window.cancelAnimationFrame = function (d) { clearTimeout(d) } } }());
  47.   
  48.     /*
  49.      * Point class
  50.      */
  51.     var Point = (function () {
  52.       function Point(x, y) {
  53.         this.x = (typeof x !== 'undefined') ? x : 0;
  54.         this.y = (typeof y !== 'undefined') ? y : 0;
  55.       }
  56.       Point.prototype.clone = function () {
  57.         return new Point(this.x, this.y);
  58.       };
  59.       Point.prototype.length = function (length) {
  60.         if (typeof length == 'undefined')
  61.           return Math.sqrt(this.x * this.x + this.y * this.y);
  62.         this.normalize();
  63.         this.x *= length;
  64.         this.y *= length;
  65.         return this;
  66.       };
  67.       Point.prototype.normalize = function () {
  68.         var length = this.length();
  69.         this.x /= length;
  70.         this.y /= length;
  71.         return this;
  72.       };
  73.       return Point;
  74.     })();
  75.   
  76.     /*
  77.      * Particle class
  78.      */
  79.     var Particle = (function () {
  80.       function Particle() {
  81.         this.position = new Point();
  82.         this.velocity = new Point();
  83.         this.acceleration = new Point();
  84.         this.age = 0;
  85.       }
  86.       Particle.prototype.initialize = function (x, y, dx, dy) {
  87.         this.position.x = x;
  88.         this.position.y = y;
  89.         this.velocity.x = dx;
  90.         this.velocity.y = dy;
  91.         this.acceleration.x = dx * settings.particles.effect;
  92.         this.acceleration.y = dy * settings.particles.effect;
  93.         this.age = 0;
  94.       };
  95.       Particle.prototype.update = function (deltaTime) {
  96.         this.position.x += this.velocity.x * deltaTime;
  97.         this.position.y += this.velocity.y * deltaTime;
  98.         this.velocity.x += this.acceleration.x * deltaTime;
  99.         this.velocity.y += this.acceleration.y * deltaTime;
  100.         this.age += deltaTime;
  101.       };
  102.       Particle.prototype.draw = function (context, image) {
  103.         function ease(t) {
  104.           return (--t) * t * t + 1;
  105.         }
  106.         var size = image.width * ease(this.age / settings.particles.duration);
  107.         context.globalAlpha = 1 - this.age / settings.particles.duration;
  108.         context.drawImage(image, this.position.x - size / 2, this.position.y - size / 2, size, size);
  109.       };
  110.       return Particle;
  111.     })();
  112.   
  113.     /*
  114.      * ParticlePool class
  115.      */
  116.     var ParticlePool = (function () {
  117.       var particles,
  118.         firstActive = 0,
  119.         firstFree = 0,
  120.         duration = settings.particles.duration;
  121.   
  122.       function ParticlePool(length) {
  123.         // create and populate particle pool
  124.         particles = new Array(length);
  125.         for (var i = 0; i < particles.length; i++)
  126.           particles[i] = new Particle();
  127.       }
  128.       ParticlePool.prototype.add = function (x, y, dx, dy) {
  129.         particles[firstFree].initialize(x, y, dx, dy);
  130.   
  131.         // handle circular queue
  132.         firstFree++;
  133.         if (firstFree == particles.length) firstFree = 0;
  134.         if (firstActive == firstFree) firstActive++;
  135.         if (firstActive == particles.length) firstActive = 0;
  136.       };
  137.       ParticlePool.prototype.update = function (deltaTime) {
  138.         var i;
  139.   
  140.         // update active particles
  141.         if (firstActive < firstFree) {
  142.           for (i = firstActive; i < firstFree; i++)
  143.             particles[i].update(deltaTime);
  144.         }
  145.         if (firstFree < firstActive) {
  146.           for (i = firstActive; i < particles.length; i++)
  147.             particles[i].update(deltaTime);
  148.           for (i = 0; i < firstFree; i++)
  149.             particles[i].update(deltaTime);
  150.         }
  151.   
  152.         // remove inactive particles
  153.         while (particles[firstActive].age >= duration && firstActive != firstFree) {
  154.           firstActive++;
  155.           if (firstActive == particles.length) firstActive = 0;
  156.         }
  157.   
  158.   
  159.       };
  160.       ParticlePool.prototype.draw = function (context, image) {
  161.         // draw active particles
  162.         if (firstActive < firstFree) {
  163.           for (i = firstActive; i < firstFree; i++)
  164.             particles[i].draw(context, image);
  165.         }
  166.         if (firstFree < firstActive) {
  167.           for (i = firstActive; i < particles.length; i++)
  168.             particles[i].draw(context, image);
  169.           for (i = 0; i < firstFree; i++)
  170.             particles[i].draw(context, image);
  171.         }
  172.       };
  173.       return ParticlePool;
  174.     })();
  175.   
  176.     /*
  177.      * Putting it all together
  178.      */
  179.     (function (canvas) {
  180.       var context = canvas.getContext('2d'),
  181.         particles = new ParticlePool(settings.particles.length),
  182.         particleRate = settings.particles.length / settings.particles.duration, // particles/sec
  183.         time;
  184.   
  185.       // get point on heart with -PI <= t <= PI
  186.       function pointOnHeart(t) {
  187.         return new Point(
  188.           160 * Math.pow(Math.sin(t), 3),
  189.           130 * Math.cos(t) - 50 * Math.cos(2 * t) - 20 * Math.cos(3 * t) - 10 * Math.cos(4 * t) + 25
  190.         );
  191.       }
  192.   
  193.       // creating the particle image using a dummy canvas
  194.       var image = (function () {
  195.         var canvas = document.createElement('canvas'),
  196.           context = canvas.getContext('2d');
  197.         canvas.width = settings.particles.size;
  198.         canvas.height = settings.particles.size;
  199.         // helper function to create the path
  200.         function to(t) {
  201.           var point = pointOnHeart(t);
  202.           point.x = settings.particles.size / 2 + point.x * settings.particles.size / 350;
  203.           point.y = settings.particles.size / 2 - point.y * settings.particles.size / 350;
  204.           return point;
  205.         }
  206.         // create the path
  207.         context.beginPath();
  208.         var t = -Math.PI;
  209.         var point = to(t);
  210.         context.moveTo(point.x, point.y);
  211.         while (t < Math.PI) {
  212.           t += 0.01; // baby steps!
  213.           point = to(t);
  214.           context.lineTo(point.x, point.y);
  215.         }
  216.         context.closePath();
  217.         // create the fill
  218.         context.fillStyle = '#ea80b0';
  219.         context.fill();
  220.         // create the image
  221.         var image = new Image();
  222.         image.src = canvas.toDataURL();
  223.         return image;
  224.       })();
  225.   
  226.       // render that thing!
  227.       function render() {
  228.         // next animation frame
  229.         requestAnimationFrame(render);
  230.   
  231.         // update time
  232.         var newTime = new Date().getTime() / 1000,
  233.           deltaTime = newTime - (time || newTime);
  234.         time = newTime;
  235.   
  236.         // clear canvas
  237.         context.clearRect(0, 0, canvas.width, canvas.height);
  238.   
  239.         // create new particles
  240.         var amount = particleRate * deltaTime;
  241.         for (var i = 0; i < amount; i++) {
  242.           var pos = pointOnHeart(Math.PI - 2 * Math.PI * Math.random());
  243.           var dir = pos.clone().length(settings.particles.velocity);
  244.           particles.add(canvas.width / 2 + pos.x, canvas.height / 2 - pos.y, dir.x, -dir.y);
  245.         }
  246.   
  247.         // update and draw particles
  248.         particles.update(deltaTime);
  249.         particles.draw(context, image);
  250.       }
  251.   
  252.       // handle (re-)sizing of the canvas
  253.       function onResize() {
  254.         canvas.width = canvas.clientWidth;
  255.         canvas.height = canvas.clientHeight;
  256.       }
  257.       window.onresize = onResize;
  258.   
  259.       // delay rendering bootstrap
  260.       setTimeout(function () {
  261.         onResize();
  262.         render();
  263.       }, 10);
  264.     })(document.getElementById('pinkboard'));
  265.   </script>
  266. </BODY>
  267.   
  268. </HTML>
复制代码


回复

使用道具 举报

发表于 2022-11-15 00:23:12 | 显示全部楼层
谢谢大神分享
回复

使用道具 举报

发表于 2022-11-18 03:42:39 | 显示全部楼层
谢谢大神
回复

使用道具 举报

发表于 2022-11-18 13:24:23 | 显示全部楼层
感谢楼主分享!93有你更精彩!
回复

使用道具 举报

发表于 2022-11-22 15:54:24 | 显示全部楼层
支持楼主,谢谢分享。
回复

使用道具 举报

发表于 2022-11-24 06:24:00 | 显示全部楼层
正需要,支持楼主,在93我只看好你!
回复

使用道具 举报

发表于 2022-12-2 00:58:46 | 显示全部楼层
这个好,谢谢分享
回复

使用道具 举报

发表于 2022-12-3 13:27:10 | 显示全部楼层
不错不错
回复

使用道具 举报

发表于 2022-12-7 01:45:25 | 显示全部楼层
感谢楼主分享!93有你更精彩!
回复

使用道具 举报

发表于 2022-12-14 23:24:29 | 显示全部楼层
感谢楼主,好人一生平安
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则


免责声明:
93破解论坛所发布的一切破解补丁、注册机和注册信息及软件的解密分析文章仅限用于学习和研究目的;不得将上述内容用于商业或者非法用途,否则,一切后果请用户自负。本站信息来自网络,版权争议与本站无关。您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容。如果您喜欢该程序,请支持正版软件,购买注册,得到更好的正版服务。如有侵权请邮件与我们联系处理。

E-Mail

RRS订阅|小黑屋|93破解论坛

GMT+8, 2024-5-6 08:41

Powered by Discuz!

Copyright © 2021.

快速回复 返回顶部 返回列表