<!DOCTYPE html>
<html> <head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">
#canvas {
position: fixed;
z-index: -1;
top: 0;
height: 100%;
width: 100%;
background: radial-gradient(#374566, #010203);
}
</style>
</head> <body> <canvas id="canvas"></canvas> <script type="text/javascript">
// CANVAS
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
var cw = canvas.width = window.innerWidth,
cx = cw / 2;
var ch = canvas.height = window.innerHeight,
cy = ch / 2; var requestId = null; var colors = ["#93DFB8", "#FFC8BA", "#E3AAD6", "#B5D8EB", "#FFBDD8"]; function Particle() {
this.x = Math.random() * cw;
this.y = Math.random() * ch;
this.r = 15 + ~~(Math.random() * 20); //radius of the circumcircle
this.minR = 2 + ~~(Math.random() * 2) ;
this.maxR = 6 + ~~(Math.random() * 2) ;
this.l = 3 + ~~(Math.random() * 2); //polygon sides
this.a = 2 * Math.PI / this.l; // angle between polygon vertices
this.rot = Math.random() * Math.PI; // polygon rotation
this.speed = .05 + Math.random() / 2;
this.rotSpeed = 0.005 + Math.random() * .005;
this.color = colors[~~(Math.random() * colors.length)];
}
Particle.prototype.update = function() {
if(this.y < -this.r) {
this.y = ch + this.r;
this.x = Math.random() * cw;
}
this.y -= this.speed;
}
Particle.prototype.draw = function() {
ctx.save(); //多边形
ctx.translate(this.x, this.y);
ctx.rotate(this.rot);
ctx.beginPath();
for(var i = 0; i < this.l; i++) {
var x = this.r * Math.cos(this.a * i); var y = this.r * Math.sin(this.a * i);
ctx.lineTo(x, y);
}
ctx.closePath();
ctx.lineWidth = 4;
ctx.strokeStyle = this.color;
ctx.stroke();
ctx.restore(); ctx.save(); //星星
ctx.beginPath();
ctx.translate(this.x / 1.1, this.y / 2);
ctx.rotate(this.rot);
ctx.globalAlpha = light;
for(var i = 0; i < 5; i ++){
var x = 5;
var y = 5;
ctx.lineTo( Math.cos( (18 + i*72 )/180 * Math.PI) * this.maxR + x,
-Math.sin( (18 + i*72 )/180 * Math.PI) * this.maxR + y)
ctx.lineTo( Math.cos( (54 + i*72 )/180 * Math.PI) * this.minR + x,
-Math.sin( (54 + i*72 )/180 * Math.PI) * this.minR + y)
}
ctx.closePath();
ctx.lineWidth = 1;
ctx.fillStyle = "#fbd94e";
ctx.strokeStyle = "#fbd94e";
ctx.lineJoin = "round";
ctx.fill();
ctx.stroke();
ctx.restore();
} var particles = [];
for(var i = 0; i < 20; i++) {
var p = new Particle();
particles.push(p)
} function Draw() {
requestId = window.requestAnimationFrame(Draw);
//ctx.globalAlpha=0.65;
ctx.clearRect(0, 0, cw, ch);
particles.map((p) => {
p.rot += p.rotSpeed;
p.update();
p.draw();
}) } function Init() {
if(requestId) {
window.cancelAnimationFrame(requestId);
requestId = null;
} cw = canvas.width = window.innerWidth, cx = cw / 2;
ch = canvas.height = window.innerHeight, cy = ch / 2; //particles.map((p) => p.update());
Draw();
}; setTimeout(function() {
Init();
window.addEventListener('resize', Init, false);
}, 15); var light = 1; //透明度
setInterval(function(){
if(light == 1){
light = .5;
}else{
light = 1;
}
},350)
</script>
</body> </html>

星星闪烁+多边形移动 canvas的更多相关文章

  1. 怎么实现类似星星闪烁的效果(box-shadow)

    有时候设计希望我们能够在页面实现类似星星闪烁的效果,如图: 我的解决办法是用box-shadow: html <div class="star04 active-blink" ...

  2. 原生js实现星星闪烁的效果

    星星闪烁的原理其实很简单: html代码: <body style="background:#000"> <div id="stars_box" ...

  3. JS框架_(JQuery.js)夜晚天空满天星星闪烁动画

    百度云盘 传送门 密码:xftr 满天星星闪烁动画效果: (可用星空动画来作为页面背景,白色文字改为文章或者其他的O(∩_∩)O) <!doctype html> <html> ...

  4. canvas 星星闪烁的效果

    代码实例: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...

  5. Silverlight之我见——制作星星闪烁动画

    圣诞节来了,无聊,做点东西纪念一下. 原理很简单,生成1000个圆,从随机数来布置它们的位置,通过动画来处理它们的透明度,动画时长也是随机生成. 1.创建图形数组并设置背景透明,渐变笔触,大小等,而后 ...

  6. html5实例-闪烁的星星

    一.绘制五角星 1.1页面结构 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"&g ...

  7. 如何用PowerPoint制作闪烁的星星

    在PPT中,PPT动画说是幻灯片PPT中的精华是当之无愧的!ppt文件有了动画,犹如插上翅膀的鸟,让PPT的色彩衍生出了更多的特色.只要你的ppt动画效果制作的对,你的幻灯片将明显与众不同,观众也更容 ...

  8. 我用canvas带你看一场流星雨

    前言 最近总是梦见一些小时候的故事,印象最深刻的就是夏天坐在屋顶上,看着满天的繁星,一颗,两颗,三颗...不由自主地开始了数星星的过程.不经意间,一颗流星划过夜间,虽然只是转瞬即逝,但它似乎比夜空中的 ...

  9. 星星dom

    <!DOCTYPE html><html><head><meta charset="UTF-8"><title>星星&l ...

随机推荐

  1. cv2.getRotationMatrix2D函数

  2. 【BZOJ5194】Snow Boots

    [原题题面]传送门 [简化题意] 给定一个长度为n的序列. 有m次询问,每次询问给定两个数si,di.你一开始站在0,每次你可以走不超过di,但你到达的位置的数不能超过si.问能否走到n+1. n,m ...

  3. 记python使用grpc

    using grpc in Python gRPC是基于http/2的RPC框架,使用ProtoBuf作为底层数据序列化.Nginx服务器2018年3月17日引入gRPC支持. gRPC 是用来实现跨 ...

  4. 【HNOI 2018】转盘

    Problem Description 一次小 \(G\) 和小 \(H\) 原本准备去聚餐,但由于太麻烦了于是题面简化如下: 一个转盘上有摆成一圈的 \(n\) 个物品(编号 \(1\) 至 \(n ...

  5. React点击操作自动定位到另外一个元素

    使用Ref 方式一 使用ScrollIntoView方法 import React from 'react' export default class ScrollToElement extends ...

  6. Python绘制温度变化曲线

    导入必要的第三方库 from requests import get import matplotlib.pyplot as plt /usr/lib/python3/dist-packages/ma ...

  7. GT sport赛道详解 - 富士国际赛车场

    练了3-4个小时,最好的成绩只有2'09多,这5秒真的很难跨越,很是绝望,感觉碰到瓶颈了. 看了几个视频,发现大家的走线有些差异,但是切apx的极速都是一样的,所以在复合弯道,走线其实不止一种. 分析 ...

  8. 20190321xlVBA_汇总表按模板生成明细表

    Public Sub 汇总表转信息表() '日期 '作者 Next 'QQ 84857038 Dim Wb, Sht, msht, NewSht, rng Set Wb = Application.T ...

  9. Petrozavodsk Winter Camp, Day 8, 2014, Ship

    $dp(i,j)$表示i-j这段还没运走时的状态,包括 运输了多少次,还剩多少空间 每次枚举运输左边还是右边转移 #include <bits/stdc++.h> #define rep( ...

  10. redist命令操作(一)--键key,字符串String

    1.Redis 字符串(String) 参考菜鸟教程:http://www.runoob.com/redis/redis-strings.html 设置指定key的值,如果原来有,覆盖 127.0.0 ...