canvas实现宇宙星尘尘埃行星旋转动画效果代码
代码语言:html
所属分类:动画
代码描述:canvas实现宇宙星尘尘埃行星旋转动画效果代码
代码标签: canvas 宇宙 星尘 尘埃 行星 旋转 动画
下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开
<!doctype html>
<html lang="zh-CN">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width,initial-scale=1" />
<title>全屏 宇宙 星尘(Canvas)</title>
<style>
html,body{height:100%;margin:0;background:#02020b;overflow:hidden}
canvas{display:block; width:100vw; height:100vh; background: radial-gradient(ellipse at 50% 40%, #05040a 0%, #02020b 40%, #000 100%);}
/* small credit text (optional) */
.credit{
position:fixed;right:12px;bottom:8px;color:rgba(255,255,255,0.18);font-family:Inter, system-ui, -apple-system; font-size:12px;pointer-events:none;
text-shadow:0 1px 6px rgba(0,0,0,0.6)
}
</style>
</head>
<body>
<canvas id="c"></canvas>
<div class="credit">宇宙星尘 · 缓慢旋转</div>
<script>
(function(){
const canvas = document.getElementById('c');
const ctx = canvas.getContext('2d', { alpha: true });
let W = window.innerWidth, H = window.innerHeight;
let DPR = Math.max(1, window.devicePixelRatio || 1);
let cx = W/2, cy = H/2;
let maxRadius = Math.min(W, H) * 0.48;
let particles = [];
let farStars = [];
let ROT_SPEED = 0.000008; // rad per ms, very slow
let arms = 4;
let particleCount = Math.max(900, Math.min(5000, Math.floor((W*H)/800)));
const reduceMotion = window.matchMedia && window.matchMedia('(prefers-reduced-motion: reduce)').matches;
// Utility: hex <-> rgb, lerp
function hexToRgb(h){
h = h.replace('#','');
return { r: parseInt(h.substring(0,2),16), g: parseInt(h..........完整代码请登录后点击上方下载按钮下载查看
















网友评论0