CSS3 实现穿梭星空动画
程序员文章站
2022-07-08 12:32:28
实现效果:htmlcss* { background:black; padding:0;...
实现效果:
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();
以上就是css3 实现穿梭星空动画的详细内容,更多关于css3 星空动画的资料请关注其它相关文章!