当前位置:   article > 正文

前端开发——爱心代码&DOM部分学习(一)_前端爱心代码

前端爱心代码

前言

很高兴来到csdn!能够和大家一起分享和学习,在接下来的一段时间我将和大家一起学习计算机相关技术,也希望以后能够和大家分享更多(未接触)知识!在接下来的这个寒假期间,我也将更新一些关于我最近一段时间学的知识,还有最近,我正在学习前端,如果有一些大佬可以指点一下我,我会非常欢迎并感谢的!

知识分享

1.爱心代码

插曲

最近一段时间网上最火的一部剧关于点燃了我,温暖了我这部电视剧,剧中主角李峋用用代码实现了动态爱心代码实现,让很多学计算机同学都在深刻的琢磨着,也让很多不是计算机专业的的人,痴迷计算机。

这个图其实是动态的,只是本人目前还不知道如何将动态图展现出来!大家先凑活一下看看吧,如果可以的话大家复制代码去运行就可以看到动态结果了。

 

下面就有关于李峋代码的python实现版:

  1. import random
  2. from math import sin, cos, pi, log
  3. from tkinter import *
  4. CANVAS_WIDTH = 640 # 画布的宽
  5. CANVAS_HEIGHT = 480 # 画布的高
  6. CANVAS_CENTER_X = CANVAS_WIDTH / 2 # 画布中心的X轴坐标
  7. CANVAS_CENTER_Y = CANVAS_HEIGHT / 2 # 画布中心的Y轴坐标
  8. IMAGE_ENLARGE = 11 # 放大比例
  9. HEART_COLOR = "red" # 心的颜色
  10. def heart_function(t, shrink_ratio: float = IMAGE_ENLARGE):
  11. """
  12. “爱心函数生成器”
  13. :param shrink_ratio: 放大比例
  14. :param t: 参数
  15. :return: 坐标
  16. """
  17. # 基础函数
  18. x = 16 * (sin(t) ** 3)
  19. y = -(13 * cos(t) - 5 * cos(2 * t) - 2 * cos(3 * t) - cos(4 * t))
  20. # 放大
  21. x *= shrink_ratio
  22. y *= shrink_ratio
  23. # 移到画布中央
  24. x += CANVAS_CENTER_X
  25. y += CANVAS_CENTER_Y
  26. return int(x), int(y)
  27. def scatter_inside(x, y, beta=0.15):
  28. """
  29. 随机内部扩散
  30. :param x: 原x
  31. :param y: 原y
  32. :param beta: 强度
  33. :return: 新坐标
  34. """
  35. ratio_x = - beta * log(random.random())
  36. ratio_y = - beta * log(random.random())
  37. dx = ratio_x * (x - CANVAS_CENTER_X)
  38. dy = ratio_y * (y - CANVAS_CENTER_Y)
  39. return x - dx, y - dy
  40. def shrink(x, y, ratio):
  41. """
  42. 抖动
  43. :param x: 原x
  44. :param y: 原y
  45. :param ratio: 比例
  46. :return: 新坐标
  47. """
  48. force = -1 / (((x - CANVAS_CENTER_X) ** 2 + (y - CANVAS_CENTER_Y) ** 2) ** 0.6) # 这个参数...
  49. dx = ratio * force * (x - CANVAS_CENTER_X)
  50. dy = ratio * force * (y - CANVAS_CENTER_Y)
  51. return x - dx, y - dy
  52. def curve(p):
  53. """
  54. 自定义曲线函数,调整跳动周期
  55. :param p: 参数
  56. :return: 正弦
  57. """
  58. return 4 * (2 * sin(4 * p)) / (2 * pi)
  59. class Heart:
  60. """
  61. 爱心类
  62. """
  63. def __init__(self, generate_frame=20):
  64. self._points = set() # 原始爱心坐标集合
  65. self._edge_diffusion_points = set() # 边缘扩散效果点坐标集合
  66. self._center_diffusion_points = set() # 中心扩散效果点坐标集合
  67. self.all_points = {} # 每帧动态点坐标
  68. self.build(2000)
  69. self.random_halo = 1000
  70. self.generate_frame = generate_frame
  71. for frame in range(generate_frame):
  72. self.calc(frame)
  73. def build(self, number):
  74. # 爱心
  75. for _ in range(number):
  76. t = random.uniform(0, 2 * pi) # 随机不到的地方造成爱心有缺口
  77. x, y = heart_function(t)
  78. self._points.add((x, y))
  79. # 爱心内扩散
  80. for _x, _y in list(self._points):
  81. for _ in range(3):
  82. x, y = scatter_inside(_x, _y, 0.05)
  83. self._edge_diffusion_points.add((x, y))
  84. # 爱心内再次扩散
  85. point_list = list(self._points)
  86. for _ in range(4000):
  87. x, y = random.choice(point_list)
  88. x, y = scatter_inside(x, y, 0.17)
  89. self._center_diffusion_points.add((x, y))
  90. @staticmethod
  91. def calc_position(x, y, ratio):
  92. # 调整缩放比例
  93. force = 1 / (((x - CANVAS_CENTER_X) ** 2 + (y - CANVAS_CENTER_Y) ** 2) ** 0.520)
  94. dx = ratio * force * (x - CANVAS_CENTER_X) + random.randint(-1, 1)
  95. dy = ratio * force * (y - CANVAS_CENTER_Y) + random.randint(-1, 1)
  96. return x - dx, y - dy
  97. def calc(self, generate_frame):
  98. ratio = 10 * curve(generate_frame / 10 * pi) # 圆滑的周期的缩放比例
  99. halo_radius = int(4 + 6 * (1 + curve(generate_frame / 10 * pi)))
  100. halo_number = int(3000 + 4000 * abs(curve(generate_frame / 10 * pi) ** 2))
  101. all_points = []
  102. # 光环
  103. heart_halo_point = set() # 光环的点坐标集合
  104. for _ in range(halo_number):
  105. t = random.uniform(0, 2 * pi) # 随机不到的地方造成爱心有缺口
  106. x, y = heart_function(t, shrink_ratio=11)
  107. x, y = shrink(x, y, halo_radius)
  108. if (x, y) not in heart_halo_point:
  109. # 处理新的点
  110. heart_halo_point.add((x, y))
  111. x += random.randint(-11, 11)
  112. y += random.randint(-11, 11)
  113. size = random.choice((1, 2, 2)) # 控制外围粒子的大小
  114. all_points.append((x, y, size))
  115. # 轮廓
  116. for x, y in self._points:
  117. x, y = self.calc_position(x, y, ratio)
  118. size = random.randint(1, 3)
  119. all_points.append((x, y, size))
  120. # 内容
  121. for x, y in self._center_diffusion_points:
  122. x, y = self.calc_position(x, y, ratio)
  123. size = random.randint(1, 2)
  124. all_points.append((x, y, size))
  125. self.all_points[generate_frame] = all_points
  126. def render(self, render_canvas, render_frame):
  127. for x, y, size in self.all_points[render_frame % self.generate_frame]:
  128. render_canvas.create_rectangle(x, y, x + size, y + size, width=0, fill=HEART_COLOR)
  129. def draw(main: Tk, render_canvas: Canvas, render_heart: Heart, render_frame=0):
  130. render_canvas.delete('all')
  131. render_heart.render(render_canvas, render_frame)
  132. main.after(160, draw, main, render_canvas, render_heart, render_frame + 1)
  133. if __name__ == '__main__':
  134. root = Tk() # 一个Tk
  135. root.title('爱心');
  136. canvas = Canvas(root, bg='black', height=CANVAS_HEIGHT, width=CANVAS_WIDTH)
  137. canvas.pack()
  138. heart = Heart() # 心
  139. draw(root, canvas, heart) # 开始画画~
  140. root.mainloop()

爱心代码( 前端实现):

运行效果如下:

这段代码其实也是动态的,大家可以复制代码去看看效果

 代码实现:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Document</title>
  8. <style>
  9. html,
  10. body {
  11. border: 0;
  12. padding: 0;
  13. margin: 0;
  14. overflow: hidden;
  15. background: #000;
  16. }
  17. .info {
  18. z-index: 999;
  19. position: absolute;
  20. left: 0;
  21. top: 0;
  22. padding: 10px;
  23. color: #fff;
  24. background: rgba(0, 0, 0, 0.5);
  25. text-transform: capitalize;
  26. }
  27. </style>
  28. </head>
  29. <body>
  30. <canvas width="300" height="300" style="width:100%;height:100vh;" id="c"></canvas>
  31. <script>
  32. var canvas = document.getElementById("c");
  33. var ctx = canvas.getContext("2d");
  34. var height = void 0,
  35. width = void 0,
  36. innerpoints = [],
  37. outerpoints = [],
  38. particles = [];
  39. var noofpoints = 200,
  40. trashold = 10;
  41. var x = void 0,
  42. y = void 0,
  43. p = void 0,
  44. n = void 0,
  45. point = void 0,
  46. dx = void 0,
  47. dy = void 0,
  48. color = void 0;
  49. (deltaangle = (Math.PI * 2) / noofpoints), (r = Math.min(height, width) * 0.5);
  50. var distance = function distance(x1, y1, x2, y2) {
  51. return Math.sqrt(Math.pow(y2 - y1, 2) + Math.pow(x2 - x1, 2));
  52. };
  53. var mapVal = function mapVal(num, in_min, in_max, out_min, out_max) {
  54. return ((num - in_min) * (out_max - out_min)) / (in_max - in_min) + out_min;
  55. };
  56. var resize = function resize() {
  57. height = ctx.canvas.clientHeight;
  58. width = ctx.canvas.clientWidth;
  59. if (
  60. ctx.canvas.clientWidth !== canvas.width ||
  61. ctx.canvas.clientHeight !== canvas.height
  62. ) {
  63. console.log("resized");
  64. canvas.width = width;
  65. canvas.height = height;
  66. ctx.translate(canvas.width / 2, canvas.height / 2);
  67. ctx.rotate(-Math.PI);
  68. innerpoints = [];
  69. r = 10;
  70. for (var i = deltaangle; i <= Math.PI * 2; i += deltaangle) {
  71. x = r * 16 * Math.pow(Math.sin(i), 3);
  72. y =
  73. r *
  74. (13 * Math.cos(i) -
  75. 5 * Math.cos(2 * i) -
  76. 2 * Math.cos(3 * i) -
  77. Math.cos(4 * i));
  78. innerpoints.push({
  79. x: x,
  80. y: y,
  81. });
  82. x = 10 * r * 16 * Math.pow(Math.sin(i), 3);
  83. y =
  84. 10 *
  85. r *
  86. (13 * Math.cos(i) -
  87. 5 * Math.cos(2 * i) -
  88. 2 * Math.cos(3 * i) -
  89. Math.cos(4 * i));
  90. outerpoints.push({
  91. x: x,
  92. y: y,
  93. });
  94. var step = random(0.001, 0.003, true);
  95. particles.push({
  96. step: step,
  97. x: x,
  98. y: y,
  99. });
  100. }
  101. }
  102. };
  103. var random = function random(min, max, isFloat) {
  104. if (isFloat) {
  105. return Math.random() * (max - min) + min;
  106. }
  107. return ~~(Math.random() * (max - min) + min);
  108. };
  109. resize();
  110. //particles = [...outerpoints];
  111. ctx.globalAlpha = 0.5;
  112. ctx.globalCompositeOperation = "source-over";
  113. var draw = function draw() {
  114. ctx.fillStyle = "rgba(0,0,0,0.03)";
  115. ctx.fillRect(-width, -height, width * 2, height * 2);
  116. ctx.beginPath();
  117. for (var i = 0; i < innerpoints.length; i++) {
  118. s = outerpoints[i];
  119. d = innerpoints[i];
  120. point = particles[i];
  121. if (distance(point.x, point.y, d.x, d.y) > 10) {
  122. dx = d.x - s.x;
  123. dy = d.y - s.y;
  124. point.x += dx * point.step;
  125. point.y += dy * point.step;
  126. color = distance(0, 0, point.x, point.y);
  127. ctx.beginPath();
  128. ctx.fillStyle = "hsl(" + (color % 360) + ",100%,50%)";
  129. ctx.arc(point.x, point.y, 2, 0, Math.PI * 2, false);
  130. ctx.closePath();
  131. ctx.fill();
  132. } else {
  133. point.x = d.x;
  134. point.y = d.y;
  135. ctx.beginPath();
  136. ctx.arc(point.x, point.y, 2, 0, Math.PI * 2, false);
  137. ctx.closePath();
  138. ctx.fill();
  139. particles[i].x = s.x;
  140. particles[i].y = s.y;
  141. particles[i].step = random(0.001, 0.003, true);
  142. }
  143. }
  144. };
  145. var render = function render() {
  146. resize();
  147. draw();
  148. requestAnimationFrame(render);
  149. };
  150. requestAnimationFrame(render);
  151. </script>
  152. </body>
  153. </html>

2.DOM学习部分

通过className改变元素属性

代码实现:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>通过className来修改元素属性</title>
  8. </head>
  9. <style>
  10. div{
  11. height: 50px;
  12. width: 50px;
  13. background-color: pink;
  14. color: #999;
  15. }
  16. .change{
  17. background-color:aqua;
  18. margin-top: 100px;
  19. color: green;
  20. }
  21. </style>
  22. <body>
  23. <div>文字</div>
  24. <script>
  25. var div=document.querySelector('div');
  26. div.onclick=function(){
  27. div.className="change";
  28. }
  29. </script>
  30. </body>
  31. </html>

密码框验证信息

验证输入密码是否输入字符是否长度满足6~16位,如果满足,则返回

否则,返回 

 代码实现:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>密码框验证信息</title>
  8. <style>
  9. div{
  10. width: 600px;
  11. margin: 100px auto;
  12. }
  13. .message{
  14. display: inline-block;
  15. size: 12px;
  16. color: #999;
  17. background:url(./images/mess.png) no-repeat left center;
  18. padding-left: 20px;
  19. }
  20. .worng{
  21. color: red;
  22. background-image: url(./images/wrong.png);
  23. }
  24. .right{
  25. color: green;
  26. background-image: url(./images/right.png);
  27. }
  28. </style>
  29. </head>
  30. <body>
  31. <div class="register">
  32. <input type="password" class="ipt">
  33. <p class="message">请输入6~16位密码</p>
  34. <script>
  35. var ipt=document.querySelector('.ipt');
  36. var mag=document.querySelector('.message');
  37. ipt.onblur=function(){
  38. if(this.value.length<6||this.value.length>16){
  39. mag.className='message worng';
  40. mag.innerHTML="您输入的位数不对要求6~16位"
  41. }else{
  42. mag.className='message right';
  43. mag.innerHTML='您输入的正确';
  44. }
  45. }
  46. </script>
  47. </div>
  48. </body>
  49. </html>

下拉选择框 

效果如下:

 代码实现:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>下拉选择框</title>
  8. </head>
  9. <style>
  10. * {
  11. margin: 0;
  12. padding: 0;
  13. }
  14. li {
  15. list-style-type: none;
  16. }
  17. a {
  18. text-decoration: none;
  19. font-size: 14px;
  20. }
  21. .nav {
  22. margin: 100px;
  23. }
  24. .nav > li {
  25. position: relative;
  26. float: left;
  27. width: 80px;
  28. height: 41px;
  29. text-align: center;
  30. border: 1px solid chartreuse;
  31. }
  32. .nav li a {
  33. display: block;
  34. width: 100%;
  35. height: 100%;
  36. line-height: 41px;
  37. color: #333;
  38. }
  39. .nav > li > a:hover {
  40. background-color: #eee;
  41. }
  42. .nav ul {
  43. display: none;
  44. position: absolute;
  45. top: 41px;
  46. left: 0;
  47. width: 100%;
  48. border-left: 1px solid #FECC5B;
  49. border-right: 1px solid #FECC5B;
  50. }
  51. .nav ul li {
  52. border-bottom: 1px solid #FECC5B;
  53. }
  54. .nav ul li a:hover {
  55. background-color: #FFF5DA;
  56. }
  57. </style>
  58. <body>
  59. <ul class="nav">
  60. <li>
  61. <a href="#">菜单</a>
  62. <ul>
  63. <li>
  64. <a href="#">私信</a>
  65. </li>
  66. <li>
  67. <a href="#">评论</a>
  68. </li>
  69. <li>
  70. <a href="#">@我</a>
  71. </li>
  72. </ul>
  73. </li>
  74. </ul>
  75. <script>
  76. var nav=document.querySelector('.nav');
  77. var lis=nav.children;
  78. lis[0].onmouseover=function(){
  79. this.children[1].style.display='block';
  80. }
  81. lis[0].onmouseout=function(){
  82. this.children[1].style.display='none';
  83. }
  84. </script>
  85. </body>
  86. </html>

这篇博客到这已经到末尾了,如果觉得写的好,大家可以关注一下,以后大家可以一起学习!相互进步,后面我也可能为大家更新前端知识或者其它计算相关技术,最后谢谢各位博客的支持,我们下期再见 

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

闽ICP备14008679号