当前位置:网站首页>Special effects - starry cosmic background effects
Special effects - starry cosmic background effects
2022-07-24 06:41:00 【Jie_ one thousand nine hundred and ninety-seven】
Starry universe background effect
One . design sketch

Two . Implementation code
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title> Starry universe background effect </title>
<style> * {
margin: 0; padding: 0; width: 100%; height: 100%; } .canvasContainer {
width: 100%; height: 100%; overflow: hidden; } </style>
</head>
<body>
<div class="canvasContainer">
<canvas id="canvas"></canvas>
</div>
<script> // Cosmic effects "use strict"; var canvas = document.getElementById('canvas'), ctx = canvas.getContext('2d'), w = canvas.width = window.innerWidth, h = canvas.height = window.innerHeight, hue = 217, stars = [], count = 0, maxStars = 2500;// The number of stars var canvas2 = document.createElement('canvas'), ctx2 = canvas2.getContext('2d'); canvas2.width = 100; canvas2.height = 100; var half = canvas2.width / 2, gradient2 = ctx2.createRadialGradient(half, half, 0, half, half, half); gradient2.addColorStop(0.025, '#CCC'); gradient2.addColorStop(0.1, 'hsl(' + hue + ', 61%, 33%)'); gradient2.addColorStop(0.25, 'hsl(' + hue + ', 64%, 6%)'); gradient2.addColorStop(1, 'transparent'); ctx2.fillStyle = gradient2; ctx2.beginPath(); ctx2.arc(half, half, half, 0, Math.PI * 2); ctx2.fill(); // End cache function random(min, max) {
if (arguments.length < 2) {
max = min; min = 0; } if (min > max) {
var hold = max; max = min; min = hold; } return Math.floor(Math.random() * (max - min + 1)) + min; } function maxOrbit(x, y) {
var max = Math.max(x, y), diameter = Math.round(Math.sqrt(max * max + max * max)); return diameter / 2; // The moving range of stars , The larger the value, the smaller the range , } var Star = function () {
this.orbitRadius = random(maxOrbit(w, h)); this.radius = random(60, this.orbitRadius) / 18; // Star size this.orbitX = w / 2; this.orbitY = h / 2; this.timePassed = random(0, maxStars); this.speed = random(this.orbitRadius) / 500000; // The speed at which the stars move this.alpha = random(2, 10) / 10; count++; stars[count] = this; } Star.prototype.draw = function () {
var x = Math.sin(this.timePassed) * this.orbitRadius + this.orbitX, y = Math.cos(this.timePassed) * this.orbitRadius + this.orbitY, twinkle = random(10); if (twinkle === 1 && this.alpha > 0) {
this.alpha -= 0.05; } else if (twinkle === 2 && this.alpha < 1) {
this.alpha += 0.05; } ctx.globalAlpha = this.alpha; ctx.drawImage(canvas2, x - this.radius / 2, y - this.radius / 2, this.radius, this.radius); this.timePassed += this.speed; } for (var i = 0; i < maxStars; i++) {
new Star(); } function animation() {
ctx.globalCompositeOperation = 'source-over'; ctx.globalAlpha = 0.5; // tail ctx.fillStyle = 'hsla(' + hue + ', 64%, 6%, 2)'; ctx.fillRect(0, 0, w, h) ctx.globalCompositeOperation = 'lighter'; for (var i = 1, l = stars.length; i < l; i++) {
stars[i].draw(); }; window.requestAnimationFrame(animation); } animation(); </script>
</body>
</html>
边栏推荐
- Flex layout
- Difference between PX and EM and REM
- JS - calculate the side length and angle of a right triangle
- 联合国农产品数据分析
- 中药材的鉴别
- 【ESP8266点焊机】基于 ESP8266 for Arduino
- Several common problems of SQL server synchronization database without public IP across network segments
- 深入了解MySQL 两把锁啥时候用(表锁,行锁)
- Install agent and proxy using custom ZABBIX package (version 4.0.5)
- DNS域名解析服务
猜你喜欢

Install Apache manually

File system and log analysis

DHCP原理与配置

Customize MVC 3.0

Experiment: disk quota operation

Website B video is embedded in the web page, and relevant controls are hidden
![[esp8266 spot welder] Based on esp8266 for Arduino](/img/9a/81b0d16572116445d46ba4e34a6e4e.png)
[esp8266 spot welder] Based on esp8266 for Arduino

Grid layout

Several common problems of SQL server synchronization database without public IP across network segments

Talk about strong cache and negotiation cache
随机推荐
Solution of forgetting root password in mysql5.7 under Windows
联合国农产品数据分析
It's not too much to fight a landlord in idea!
Rsync (I): basic commands and usage
SSH Remote Access and control
类的加载器 和 双亲委派机制详解
The character that appears the most times in the JS output string
【LVGL(5)】标签的(label)用法
RESTful API介绍
Flink time stream processing
Transition effect
[small object velocimeter] only principle, no code
广度优先搜索(模板使用)
我有 7种 实现web实时消息推送的方案,7种!
General paging 01
Polkadot | interprets how liberty plan, which subverts traditional social media, will be launched in Poka
Sed command
Experiment: disk quota operation
RAID configuration experiment
Customize ZABBIX agent RPM package