当前位置:   article > 正文

python爱心表白代码简单,python爱心的编程代码_python爱心编码10084

python爱心编码10084

这篇文章主要介绍了python爱心表白代码简单,具有一定借鉴价值,需要的朋友可以参考下。希望大家阅读完这篇文章后大有收获,下面让小编带着大家一起了解一下。

上一篇的python爱心代码有很多读者反馈不会用。
 

本篇来一个小白版的,无需会代码,有浏览器就可python编程代码大全

操作步骤

01 复制以下内容,保存为.html文件

        (不会保存?先保存为txt文本,然后将后缀改为.html)

02  双击该文件,打开即可出现动态爱心

代码如下:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title></title>
  5. < src="js/jquery.min.js"></>
  6. </head>
  7. <style>
  8. * {
  9. padding: 0;
  10. margin: 0;
  11. }
  12. html,
  13. body {
  14. height: 100%;
  15. padding: 0;
  16. margin: 0;
  17. background: #000;
  18. }
  19. .aa {
  20. position: fixed;
  21. left: 50%;
  22. bottom: 10px;
  23. color: #ccc;
  24. }
  25. .container {
  26. width: 100%;
  27. height: 100%;
  28. }
  29. canvas {
  30. z-index: 99;
  31. position: absolute;
  32. width: 100%;
  33. height: 100%;
  34. }
  35. </style>
  36. <body>
  37. <!-- 樱花 -->
  38. <div id="jsi-cherry-container" class="container">
  39. <audio autoplay="autopaly">
  40. <source src="renxi.mp3" type="audio/mp3" />
  41. </audio>
  42. <img class="img" src="./123.png" alt="" />
  43. <!-- 爱心 -->
  44. <canvas id="pinkboard" class="container"> </canvas>
  45. </div>
  46. </body>
  47. </html>
  48. <>
  49. /*
  50. * Settings
  51. */
  52. var settings = {
  53. particles: {
  54. length: 500, // maximum amount of particles
  55. duration: 2, // particle duration in sec
  56. velocity: 100, // particle velocity in pixels/sec
  57. effect: -0.75, // play with this for a nice effect
  58. size: 30, // particle size in pixels
  59. },
  60. };
  61. (function () {
  62. var b = 0;
  63. var c = ["ms", "moz", "webkit", "o"];
  64. for (var a = 0; a < c.length && !window.requestAnimationFrame; ++a) {
  65. window.requestAnimationFrame = window[c[a] + "RequestAnimationFrame"];
  66. window.cancelAnimationFrame =
  67. window[c[a] + "CancelAnimationFrame"] ||
  68. window[c[a] + "CancelRequestAnimationFrame"];
  69. }
  70. if (!window.requestAnimationFrame) {
  71. window.requestAnimationFrame = function (h, e) {
  72. var d = new Date().getTime();
  73. var f = Math.max(0, 16 - (d - b));
  74. var g = window.setTimeout(function () {
  75. h(d + f);
  76. }, f);
  77. b = d + f;
  78. return g;
  79. };
  80. }
  81. if (!window.cancelAnimationFrame) {
  82. window.cancelAnimationFrame = function (d) {
  83. clearTimeout(d);
  84. };
  85. }
  86. })();
  87. /*
  88. * Point class
  89. */
  90. var Point = (function () {
  91. function Point(x, y) {
  92. this.x = typeof x !== "undefined" ? x : 0;
  93. this.y = typeof y !== "undefined" ? y : 0;
  94. }
  95. Point.prototype.clone = function () {
  96. return new Point(this.x, this.y);
  97. };
  98. Point.prototype.length = function (length) {
  99. if (typeof length == "undefined")
  100. return Math.sqrt(this.x * this.x + this.y * this.y);
  101. this.normalize();
  102. this.x *= length;
  103. this.y *= length;
  104. return this;
  105. };
  106. Point.prototype.normalize = function () {
  107. var length = this.length();
  108. this.x /= length;
  109. this.y /= length;
  110. return this;
  111. };
  112. return Point;
  113. })();
  114. /*
  115. * Particle class
  116. */
  117. var Particle = (function () {
  118. function Particle() {
  119. this.position = new Point();
  120. this.velocity = new Point();
  121. this.acceleration = new Point();
  122. this.age = 0;
  123. }
  124. Particle.prototype.initialize = function (x, y, dx, dy) {
  125. this.position.x = x;
  126. this.position.y = y;
  127. this.velocity.x = dx;
  128. this.velocity.y = dy;
  129. this.acceleration.x = dx * settings.particles.effect;
  130. this.acceleration.y = dy * settings.particles.effect;
  131. this.age = 0;
  132. };
  133. Particle.prototype.update = function (deltaTime) {
  134. this.position.x += this.velocity.x * deltaTime;
  135. this.position.y += this.velocity.y * deltaTime;
  136. this.velocity.x += this.acceleration.x * deltaTime;
  137. this.velocity.y += this.acceleration.y * deltaTime;
  138. this.age += deltaTime;
  139. };
  140. Particle.prototype.draw = function (context, image) {
  141. function ease(t) {
  142. return --t * t * t + 1;
  143. }
  144. var size = image.width * ease(this.age / settings.particles.duration);
  145. context.globalAlpha = 1 - this.age / settings.particles.duration;
  146. context.drawImage(
  147. image,
  148. this.position.x - size / 2,
  149. this.position.y - size / 2,
  150. size,
  151. size
  152. );
  153. };
  154. return Particle;
  155. })();
  156. /*
  157. * ParticlePool class
  158. */
  159. var ParticlePool = (function () {
  160. var particles,
  161. firstActive = 0,
  162. firstFree = 0,
  163. duration = settings.particles.duration;
  164. function ParticlePool(length) {
  165. // create and populate particle pool
  166. particles = new Array(length);
  167. for (var i = 0; i < particles.length; i++)
  168. particles[i] = new Particle();
  169. }
  170. ParticlePool.prototype.add = function (x, y, dx, dy) {
  171. particles[firstFree].initialize(x, y, dx, dy);
  172. // handle circular queue
  173. firstFree++;
  174. if (firstFree == particles.length) firstFree = 0;
  175. if (firstActive == firstFree) firstActive++;
  176. if (firstActive == particles.length) firstActive = 0;
  177. };
  178. ParticlePool.prototype.update = function (deltaTime) {
  179. var i;
  180. // update active particles
  181. if (firstActive < firstFree) {
  182. for (i = firstActive; i < firstFree; i++)
  183. particles[i].update(deltaTime);
  184. }
  185. if (firstFree < firstActive) {
  186. for (i = firstActive; i < particles.length; i++)
  187. particles[i].update(deltaTime);
  188. for (i = 0; i < firstFree; i++) particles[i].update(deltaTime);
  189. }
  190. // remove inactive particles
  191. while (
  192. particles[firstActive].age >= duration &&
  193. firstActive != firstFree
  194. ) {
  195. firstActive++;
  196. if (firstActive == particles.length) firstActive = 0;
  197. }
  198. };
  199. ParticlePool.prototype.draw = function (context, image) {
  200. // draw active particles
  201. if (firstActive < firstFree) {
  202. for (i = firstActive; i < firstFree; i++)
  203. particles[i].draw(context, image);
  204. }
  205. if (firstFree < firstActive) {
  206. for (i = firstActive; i < particles.length; i++)
  207. particles[i].draw(context, image);
  208. for (i = 0; i < firstFree; i++) particles[i].draw(context, image);
  209. }
  210. };
  211. return ParticlePool;
  212. })();
  213. /*
  214. * Putting it all together
  215. */
  216. (function (canvas) {
  217. var context = canvas.getContext("2d"),
  218. particles = new ParticlePool(settings.particles.length),
  219. particleRate =
  220. settings.particles.length / settings.particles.duration, // particles/sec
  221. time;
  222. // get point on heart with -PI <= t <= PI
  223. function pointOnHeart(t) {
  224. return new Point(
  225. 160 * Math.pow(Math.sin(t), 3),
  226. 130 * Math.cos(t) -
  227. 50 * Math.cos(2 * t) -
  228. 20 * Math.cos(3 * t) -
  229. 10 * Math.cos(4 * t) +
  230. 25
  231. );
  232. }
  233. // creating the particle image using a dummy canvas
  234. var image = (function () {
  235. var canvas = document.createElement("canvas"),
  236. context = canvas.getContext("2d");
  237. canvas.width = settings.particles.size;
  238. canvas.height = settings.particles.size;
  239. // helper function to create the path
  240. function to(t) {
  241. var point = pointOnHeart(t);
  242. point.x =
  243. settings.particles.size / 2 +
  244. (point.x * settings.particles.size) / 350;
  245. point.y =
  246. settings.particles.size / 2 -
  247. (point.y * settings.particles.size) / 350;
  248. return point;
  249. }
  250. // create the path
  251. context.beginPath();
  252. var t = -Math.PI;
  253. var point = to(t);
  254. context.moveTo(point.x, point.y);
  255. while (t < Math.PI) {
  256. t += 0.01; // baby steps!
  257. point = to(t);
  258. context.lineTo(point.x, point.y);
  259. }
  260. context.closePath();
  261. // create the fill
  262. context.fillStyle = "#ea80b0";
  263. context.fill();
  264. // create the image
  265. var image = new Image();
  266. image.src = canvas.toDataURL();
  267. return image;
  268. })();
  269. // render that thing!
  270. function render() {
  271. // next animation frame
  272. requestAnimationFrame(render);
  273. // update time
  274. var newTime = new Date().getTime() / 1000,
  275. deltaTime = newTime - (time || newTime);
  276. time = newTime;
  277. // clear canvas
  278. context.clearRect(0, 0, canvas.width, canvas.height);
  279. // create new particles
  280. var amount = particleRate * deltaTime;
  281. for (var i = 0; i < amount; i++) {
  282. var pos = pointOnHeart(Math.PI - 2 * Math.PI * Math.random());
  283. var dir = pos.clone().length(settings.particles.velocity);
  284. particles.add(
  285. canvas.width / 2 + pos.x,
  286. canvas.height / 2 - pos.y,
  287. dir.x,
  288. -dir.y
  289. );
  290. }
  291. // update and draw particles
  292. particles.update(deltaTime);
  293. particles.draw(context, image);
  294. }
  295. // handle (re-)sizing of the canvas
  296. function onResize() {
  297. canvas.width = canvas.clientWidth;
  298. canvas.height = canvas.clientHeight;
  299. }
  300. window.onresize = onResize;
  301. // delay rendering bootstrap
  302. setTimeout(function () {
  303. onResize();
  304. render();
  305. }, 10);
  306. })(document.getElementById("pinkboard"));
  307. </>
  308. <>
  309. var RENDERER = {
  310. INIT_CHERRY_BLOSSOM_COUNT: 30,
  311. MAX_ADDING_INTERVAL: 10,
  312. init: function () {
  313. this.setParameters();
  314. this.reconstructMethods();
  315. this.createCherries();
  316. this.render();
  317. if (
  318. navigator.userAgent.match(
  319. /(phone|pod|iPhone|iPod|ios|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i
  320. )
  321. ) {
  322. // var box = document.querySelectorAll(".box")[0];
  323. // console.log(box, "移动端");
  324. // box.style.marginTop = "65%";
  325. }
  326. },
  327. setParameters: function () {
  328. this.$container = $("#jsi-cherry-container");
  329. this.width = this.$container.width();
  330. this.height = this.$container.height();
  331. this.context = $("<canvas />")
  332. .attr({ width: this.width, height: this.height })
  333. .appendTo(this.$container)
  334. .get(0)
  335. var rate = this.FOCUS_POSITION / (this.z + this.FOCUS_POSITION),
  336. x = this.renderer.width / 2 + this.x * rate,
  337. y = this.renderer.height / 2 - this.y * rate;
  338. return { rate: rate, x: x, y: y };
  339. },
  340. re
  341. }
  342. } else {
  343. this.phi += Math.PI / (axis.y == this.thresholdY ? 200 : 500);
  344. this.phi %= Math.PI;
  345. }
  346. if (this.y <= -this.renderer.height * this.SURFACE_RATE) {
  347. this.x += 2;
  348. this.y = -this.renderer.height * this.SURFACE_RATE;
  349. } else {
  350. this.x += this.vx;
  351. this.y += this.vy;
  352. }
  353. return (
  354. this.z > -this.FOCUS_POSITION &&
  355. this.z < this.FAR_LIMIT &&
  356. this.x < this.renderer.width * 1.5
  357. );
  358. },
  359. };
  360. $(function () {
  361. RENDERER.init();
  362. });
  363. </>

太单调?想要加文案?

定制升级版(可以自定义姓名及文案

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/586800
推荐阅读
相关标签