当前位置:   article > 正文

HTML星空特效_我不是哆啦a梦 csdn

我不是哆啦a梦 csdn

目录

写在前面

完整代码

代码分析

运行效果

系列文章

写在后面


写在前面

100行代码实现HTML星空特效。

完整代码

全部代码如下。

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  5. <title>星空特效</title>
  6. </head>
  7. <body>
  8. </body>
  9. <!-- Html代码 -->
  10. <div>
  11. <canvas id="canvas"></canvas>
  12. <canvas id="snow"></canvas>
  13. <div class="am-g" style="position: fixed; bottom: 0px;">
  14. <div class="am-u-sm-12">
  15. <div style="z-index: 9999" id="player" class="aplayer">
  16. </div>
  17. </div>
  18. </div>
  19. </div>
  20. <!-- CSS代码 -->
  21. <style type="text/css">
  22. canvas {
  23. position: fixed;
  24. width: 100%;
  25. height: 100%;
  26. z-index: -1;
  27. }
  28. </style>
  29. <script type="text/javascript">
  30. var canvas = document.getElementById('canvas'),
  31. ctx = canvas.getContext('2d'),
  32. w = canvas.width = window.innerWidth,
  33. h = canvas.height = window.innerHeight,
  34. hue = 217,
  35. stars = [],
  36. count = 0,
  37. maxStars = 1300; //星星数量
  38. var canvas2 = document.createElement('canvas'),
  39. ctx2 = canvas2.getContext('2d');
  40. canvas2.width = 100;
  41. canvas2.height = 100;
  42. var half = canvas2.width / 2,
  43. gradient2 = ctx2.createRadialGradient(half, half, 0, half, half, half);
  44. gradient2.addColorStop(0.025, '#CCC');
  45. gradient2.addColorStop(0.1, 'hsl(' + hue + ', 61%, 33%)');
  46. gradient2.addColorStop(0.25, 'hsl(' + hue + ', 64%, 6%)');
  47. gradient2.addColorStop(1, 'transparent');
  48. ctx2.fillStyle = gradient2;
  49. ctx2.beginPath();
  50. ctx2.arc(half, half, half, 0, Math.PI * 2);
  51. ctx2.fill();
  52. function random(min, max) {
  53. if (arguments.length < 2) {
  54. max = min;
  55. min = 0;
  56. }
  57. if (min > max) {
  58. var hold = max;
  59. max = min;
  60. min = hold;
  61. }
  62. return Math.floor(Math.random() * (max - min + 1)) + min;
  63. }
  64. function maxOrbit(x, y) {
  65. var max = Math.max(x, y),
  66. diameter = Math.round(Math.sqrt(max * max + max * max));
  67. return diameter / 2;
  68. //星星移动范围,值越大范围越小,
  69. }
  70. var Star = function () {
  71. this.orbitRadius = random(maxOrbit(w, h));
  72. this.radius = random(60, this.orbitRadius) / 8;
  73. //星星大小
  74. this.orbitX = w / 2;
  75. this.orbitY = h / 2;
  76. this.timePassed = random(0, maxStars);
  77. this.speed = random(this.orbitRadius) / 50000;
  78. //星星移动速度
  79. this.alpha = random(2, 10) / 10;
  80. count++;
  81. stars[count] = this;
  82. }
  83. Star.prototype.draw = function () {
  84. var x = Math.sin(this.timePassed) * this.orbitRadius + this.orbitX,
  85. y = Math.cos(this.timePassed) * this.orbitRadius + this.orbitY,
  86. twinkle = random(10);
  87. if (twinkle === 1 && this.alpha > 0) {
  88. this.alpha -= 0.05;
  89. } else if (twinkle === 2 && this.alpha < 1) {
  90. this.alpha += 0.05;
  91. }
  92. ctx.globalAlpha = this.alpha;
  93. ctx.drawImage(canvas2, x - this.radius / 2, y - this.radius / 2, this.radius, this.radius);
  94. this.timePassed += this.speed;
  95. }
  96. for (var i = 0; i < maxStars; i++) {
  97. new Star();
  98. }
  99. function animation() {
  100. ctx.globalCompositeOperation = 'source-over';
  101. ctx.globalAlpha = 0.5; //尾巴
  102. ctx.fillStyle = 'hsla(' + hue + ', 64%, 6%, 2)';
  103. ctx.fillRect(0, 0, w, h)
  104. ctx.globalCompositeOperation = 'lighter';
  105. for (var i = 1, l = stars.length; i < l; i++) {
  106. stars[i].draw();
  107. canvas2.style.cssText = "display:none";
  108. };
  109. window.requestAnimationFrame(animation);
  110. }
  111. animation();
  112. </script>
  113. </html>

代码分析

这段HTML和JavaScript代码创建了一个网页,该网页展示了一个动态的星空效果,其中星星在画布上随机闪烁并移动。

首先,在HTML结构中,有两个<canvas>元素被定义,分别用于绘制星空背景和可能的其他效果(如雪花),以及一个用于放置音频播放器<div><canvas>元素通过CSS设置为全屏覆盖,并且位置固定,这样它们可以作为背景层显示。

接下来是CSS样式,它主要设置了<canvas>元素的样式,包括位置、尺寸和层级,确保画布覆盖整个屏幕并且位于其他元素下方。

在JavaScript部分,首先获取了<canvas>元素及其绘图上下文,接着初始化了画布的宽度和高度以匹配浏览器窗口。之后,创建了一个辅助的<canvas>canvas2)来预渲染星星的图像,使用径向渐变填充,模拟出星星从明亮到暗淡的渐变效果。

random函数用于生成随机数,而maxOrbit函数则计算出星星的移动范围,基于画布的尺寸。Star构造函数定义了星星的属性和方法,包括其轨道半径、大小、位置、速度和透明度。星星的运动遵循正弦和余弦函数,从而产生环绕中心点的圆周运动。

animation函数实现了动画循环,它首先设置全局混合模式为source-over,然后设置全局透明度为0.5,以绘制星星留下的“尾巴”效果。接下来,使用hsla颜色填充整个画布,然后切换全局混合模式为lighter,以正确地叠加星星图像。通过遍历所有星星对象并调用draw方法,实现星星的绘制和更新。最后,利用requestAnimationFrame来确保动画的平滑执行,每帧都调用animation函数自身,形成连续的动画效果。

整个星空特效通过精心设计的星星闪烁算法和动态渲染技术,呈现出一个既真实又具有艺术感的星空场景。

运行效果

图片

系列文章

序号目录直达链接
1HTML实现3D相册HTML实现3D相册-CSDN博客
2HTML元素周期表HTML元素周期表-CSDN博客
3HTML黑客帝国字母雨HTML黑客帝国字母雨_字母雨html-CSDN博客
4HTML五彩缤纷的爱心HTML五彩缤纷的爱心-CSDN博客
5HTML飘落的花瓣HTML飘落的花瓣-CSDN博客
6HTML哆啦A梦HTML哆啦A梦_html哆啦a梦代码-CSDN博客
7HTML爱情树HTML爱情树-CSDN博客
8HTML新春烟花盛宴HTML新春烟花盛宴-CSDN博客
9HTML想见你HTML想见你-CSDN博客
10HTML蓝色爱心HTML蓝色爱心-CSDN博客
11HTML跳动的爱心HTML跳动的爱心-CSDN博客
12HTML橙色爱心HTML橙色爱心-CSDN博客
13HTML大雪纷飞HTML大雪纷飞-CSDN博客
14HTML跨年烟花HTML跨年烟花-CSDN博客
15HTML跳动的爱心HTML跳动的爱心-CSDN博客
16HTML动态爱心HTML动态爱心-CSDN博客
17HTML浪漫星空https://want595.blog.csdn.net/article/details/139799620
18
19
20
21
22
23
24
25
26
27

写在后面

我是一只有趣的兔子,感谢你的喜欢!

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

闽ICP备14008679号