CSS3 realizes the animation of shuttle starry sky

CSS3 realizes the animation of shuttle starry sky

Result:

html

<canvas id="starfield"></canvas>

CSS

* {
  background:black;
  padding:0;
  margin:0;
}

canvas {
  padding:0;
  margin:0;
  width:100%;
  height:100%;
}

js

function $i(t) {
    return document.getElementById(t)
}
function $r(t, r) {
    document.getElementById(t).removeChild(document.getElementById(r))
}
function $t(t) {
    return document.getElementsByTagName(t)
}
function $c(t) {
    return String.fromCharCode(t)
}
function $h(t) {
    return ("0" + Math.max(0, Math.min(255, Math.round(t))).toString(16)).slice(-2)
}
function _i(t, r) {
    $t("div")[t].innerHTML += r
}
function _h(t) {
    return hires ? Math.round(t / 2) : t
}
function get_screen_size() {
    var t = document.documentElement.clientWidth,
        r = document.documentElement.clientHeight;
    return Array(t, r)
}
function init() {
    for (var t = 0; n > t; t++) star[t] = new Array(5), star[t][0] = Math.random() * w * 2 - 2 * x, star[t][1] = Math.random() * h * 2 - 2 * y, star[t][2] = Math.round(Math.random() * z), star[t][3] = 0, star[t][4] = 0;
    var r = $i("starfield");
    r.style.position = "absolute", r.width = w, r.height = h, context = r.getContext("2d"), context.fillStyle = "rgb(0,0,0)", context.strokeStyle = "rgb(255,255,255)"
}
function anim() {
    mouse_x = cursor_x - x, mouse_y = cursor_y - y, context.fillRect(0, 0, w, h);
    for (var t = 0; n > t; t++) test = !0, star_x_save = star[t][3], star_y_save = star[t][4], star[t][0] += mouse_x >> 4, star[t][0] > x << 1 && (star[t][0] -= w << 1, test = !1), star[t][0] < -x << 1 && (star[t][0] += w << 1, test = !1), star[t][1] += mouse_y >> 4, star[t][1] > y << 1 && (star[t][1] -= h << 1, test = !1), star[t][1] < -y << 1 && (star[t][1] += h << 1, test = !1), star[t][2] -= star_speed, star[t][2] > z && (star[t][2] -= z, test = !1), star[t][2] < 0 && (star[t][2] += z, test = !1), star[t][3] = x + star[t][0] / star[t][2] * star_ratio, star[t][4] = y + star[t][1] / star[t][2] * star_ratio, star_x_save > 0 && w > star_x_save && star_y_save > 0 && h > star_y_save && test && (context.lineWidth = 2 * (1 - star_color_ratio * star[t][2]), context.beginPath(), context.moveTo(star_x_save, star_y_save), context.lineTo(star[t][3], star[t][4]), context.stroke(), context.closePath());
    timeout = setTimeout("anim()", fps)
}
function start() {
    resize(), anim()
}
function resize() {
    w = parseInt(-1 != url.indexOf("w=") ? url.substring(url.indexOf("w=") + 2, -1 != url.substring(url.indexOf("w=") + 2, url.length).indexOf("&") ? url.indexOf("w=") + 2 + url.substring(url.indexOf("w=") + 2, url.length).indexOf("&") : url.length) : get_screen_size()[0]), h = parseInt(-1 != url.indexOf("h=") ? url.substring(url.indexOf("h=") + 2, -1 != url.substring(url.indexOf("h=") + 2, url.length).indexOf("&") ? url.indexOf("h=") + 2 + url.substring(url.indexOf("h=") + 2, url.length).indexOf("&") : url.length) : get_screen_size()[1]), x = Math.round(w / 2), y = Math.round(h / 2), z = (w + h) / 2, star_color_ratio = 1 / z, cursor_x = x, cursor_y = y, init()
}
var url = document.location.href,
    flag = !0,
    test = !0,
    n = parseInt(-1 != url.indexOf("n=") ? url.substring(url.indexOf("n=") + 2, -1 != url.substring(url.indexOf("n=") + 2, url.length).indexOf("&") ? url.indexOf("n=") + 2 + url.substring(url.indexOf("n=") + 2, url.length).indexOf("&") : url.length) : 812),
    w = 0,
    h = 0,
    x = 0,
    y = 0,
    z = 0,
    star_color_ratio = 0,
    star_x_save, star_y_save, star_ratio = 115,
    star_speed = 5,
    star_speed_save = 0,
    star = new Array(n),
    color, opacity = .1,
    cursor_x = 0,
    cursor_y = 0,
    mouse_x = 0,
    mouse_y = 0,
    canvas_x = 0,
    canvas_y = 0,
    canvas_w = 0,
    canvas_h = 0,
    context, key, ctrl, timeout, fps = 0;
start();

The above is the details of how to use CSS3 to realize the star-studded animation. For more information about CSS3 star-studded animation, please pay attention to other related articles on 123WORDPRESS.COM!

<<:  MySQL data analysis storage engine example explanation

>>:  Detailed tutorial on jdk installation and environment variable configuration under Win10

Recommend

Sample code for CSS image animation effects (photo frame)

This article introduces the sample code of CSS pi...

How to install and configure Docker nginx

Download Nginx image in Docker docker pull nginx ...

A Brief Analysis of Patroni in Docker Containers

Table of contents Create an image File Structure ...

MySQL character set garbled characters and solutions

Preface A character set is a set of symbols and e...

How to deploy your first application with Docker

In the previous article, you have installed Docke...

Detailed explanation of docker network bidirectional connection

View Docker Network docker network ls [root@maste...

MySQL backup and recovery design ideas

background First, let me explain the background. ...

Basic installation process of mysql5.7.19 under winx64 (details)

1. Download https://dev.mysql.com/downloads/mysql...

MySQL 8.0.15 installation and configuration tutorial under Win10

What I have been learning recently involves knowl...

How to change password in MySQL 5.7.18

How to change the password in MySQL 5.7.18: 1. Fi...

Solution to the problem that Navicat cannot remotely connect to MySql server

The solution to the problem that Navicat cannot r...

Vue implements setting multiple countdowns at the same time

This article example shares the specific code of ...

Vue uses Echarts to implement a three-dimensional bar chart

This article shares the specific code of Vue usin...