本篇文章给大家带来了javascript中星空连线效果应该怎样呈现的相关知识,希望对大家有帮助。
javascript 星空连线效果的简单实现
之前有见过非常炫酷的粒子连线的效果,这篇文章主要是实现一个简单的星空连线的效果。
先贴一下大概的效果图。
这个主要是用到了html5中的canvas绘图,关于canvas的基本使用这里就不展开介绍了,大家可以自行去了解。
然后采用的是requestanimationframe来进行动画的绘制,而没有采用定时器。
一、实现的效果
星星自动生成,且星星的颜色,初始位置,移动方向都是随机的。
当星星之间的距离小于给定值之后,会在星星之间生成连线。
鼠标指针和星星之间的距离小于给定值之后,也会在星星和鼠标指针之间生成连线。
二、实现的方法
通过canvas绘图实现
定义星星类star, 包括位置,半径,颜色,移速等属性与绘制和移动等方法。
绘制星星,实现随机移动的效果。
在绘制星星之后计算每个星星之间的距离,在符合要求的星星之间绘制连线。
计算鼠标指针和星星之间的距离,在符合要求的星星之间绘制连线。
绘制采用requestanimationframe
在主函数中执行4,5的函数继续进行绘制
三、具体的实现
html + css
基本的文档结构非常简单,创建一个canvas容器就可以了。
<!doctype html><html><head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="x-ua-compatible" content="ie=edge"> <title>星空连线</title> <style type="text/css"> * { margin: 0; padding: 0; } body, html { width: 100%; height: 100%; overflow: hidden; } #starry { position: absolute; background-color: #000000; } </style></head><body> <canvas id="starry"></canvas></html>
定义星星类star, 包括位置,半径,颜色,移速等属性与绘制和移动等方法。
class star { constructor() { this.x = randnum(3, canvas.width - 3); this.y = randnum(3, canvas.height - 3); this.r = randnum(1, 3); this.color = randcolor(); this.speedx = randnum(-2, 2) * 0.2; this.speedy = randnum(-3, 3) * 0.2; } // 绘制每个星点 draw() { //新建一条路径 ctx.beginpath(); //调整透明度 ctx.globalalpha = 1; // 填充颜色 ctx.fillstyle = this.color; // 绘制圆弧 ctx.arc(this.x, this.y, this.r, 0, math.pi * 2); // 填充 ctx.fill(); } // 星星移动 move() { this.x += this.speedx; this.y += this.speedy; //设置极限值 if (this.x <= 3 || this.x >= canvas.width - 3) this.speedx *= -1; if (this.y <= 3 || this.y >= canvas.height - 3) this.speedy *= -1; } } // 存储小球 let stars = []; for (let i = 0; i < 150; i++) { let star = new star(); // 存入数组 stars.push(star); }
绘制星星,实现随机移动的效果。
我们可以先实现星星的绘制,先暂时不管连线的效果。
function drawline() { for (var i = 0; i < stars.length; i++) { stars[i].draw(); stars[i].move(); } }
在绘制星星之后计算每个星星之间的距离,在符合要求的星星之间绘制连线。
其实只要在上一步的函数中添加距离判断和绘制连线的代码就可以了。
function drawline() { for (var i = 0; i < stars.length; i++) { stars[i].draw(); stars[i].move(); for (var j = 0; j < stars.length; j++) { if (i != j) { if (math.sqrt(math.pow((stars[i].x - stars[j].x), 2) + math.pow((stars[i].y - stars[j].y), 2)) < 80) { ctx.beginpath(); ctx.moveto(stars[i].x, stars[i].y); ctx.lineto(stars[j].x, stars[j].y); ctx.strokestyle = "white"; ctx.globalalpha = 0.2; ctx.stroke(); } } } } }
计算鼠标指针和星星之间的距离,在符合要求的星星之间绘制连线。
和绘制星星的方法差不多。
function mouseline() { for (var i = 0; i < stars.length; i++) { if (math.sqrt(math.pow((stars[i].x - mousex), 2) + math.pow((stars[i].y - mousey), 2)) < 120) { ctx.beginpath(); ctx.moveto(stars[i].x, stars[i].y); ctx.lineto(mousex, mousey); ctx.strokestyle = "white"; ctx.globalalpha = 0.8; ctx.stroke(); } } }
主函数进行绘制
function main() { // 清除矩形区域 ctx.clearrect(0, 0, canvas.width, canvas.height); //鼠标移动绘制连线 mouseline(); // 小球之间自动连线 drawline(); // 不断重新执行main(绘制和清除) window.requestanimationframe(main); }
一些辅助随机函数
// 随机函数 function randnum(m, n) { return math.floor(math.random() * (n - m + 1) + m); } // 随机颜色 function randcolor() { return 'rgb(' + randnum(0, 255) + ',' + randnum(0, 255) + ',' + randnum(0, 255) + ')'; }
完整的代码
<!doctype html><html><head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="x-ua-compatible" content="ie=edge"> <title>星空连线</title> <style type="text/css"> * { margin: 0; padding: 0; } body, html { width: 100%; height: 100%; overflow: hidden; } #starry { position: absolute; background-color: #000000; } </style></head><body> <canvas id="starry"></canvas> <script type="text/javascript"> // 获取canvas容器 let canvas = document.getelementbyid('starry'); // 获取屏幕的宽高 canvas.width = document.documentelement.clientwidth; canvas.height = document.documentelement.clientheight; // 设置绘制模式为2d let ctx = canvas.getcontext('2d'); class star { constructor() { this.x = randnum(3, canvas.width - 3); this.y = randnum(3, canvas.height - 3); this.r = randnum(1, 3); this.color = 'pink'; this.color = randcolor(); this.speedx = randnum(-2, 2) * 0.2; this.speedy = randnum(-3, 3) * 0.2; } // 绘制每个星点 draw() { //新建一条路径 ctx.beginpath(); //调整透明度 ctx.globalalpha = 1; // 填充颜色 ctx.fillstyle = this.color; // 绘制圆弧 ctx.arc(this.x, this.y, this.r, 0, math.pi * 2); // 填充 ctx.fill(); } // 小球移动 move() { this.x += this.speedx; this.y += this.speedy; //设置极限值 if (this.x <= 3 || this.x >= canvas.width - 3) this.speedx *= -1; if (this.y <= 3 || this.y >= canvas.height - 3) this.speedy *= -1; } } // 存储小球 let stars = []; for (let i = 0; i < 150; i++) { let star = new star(); // 存入数组 stars.push(star); } let mousex; let mousey; canvas.onmousemove = function (e) { var e = event || e; mousex = e.offsetx; mousey = e.offsety; // console.log(mousex+','+mousey); } // 主要事件 main(); function mouseline() { for (var i = 0; i < stars.length; i++) { if (math.sqrt(math.pow((stars[i].x - mousex), 2) + math.pow((stars[i].y - mousey), 2)) < 120) { ctx.beginpath(); ctx.moveto(stars[i].x, stars[i].y); ctx.lineto(mousex, mousey); ctx.strokestyle = "white"; ctx.globalalpha = 0.8; ctx.stroke(); } } } // 在一定范围内划线 function drawline() { for (var i = 0; i < stars.length; i++) { stars[i].draw(); stars[i].move(); // for (var j = 0; j < stars.length; j++) { // if (i != j) { // if (math.sqrt(math.pow((stars[i].x - stars[j].x), 2) + math.pow((stars[i].y - stars[j].y), 2)) < 80) { // ctx.beginpath(); // ctx.moveto(stars[i].x, stars[i].y); // ctx.lineto(stars[j].x, stars[j].y); // ctx.strokestyle = "white"; // ctx.globalalpha = 0.2; // ctx.stroke(); // } // } // } } } function main() { // 清除矩形区域 ctx.clearrect(0, 0, canvas.width, canvas.height); //鼠标移动绘制连线 mouseline(); // 小球之间自动连线 drawline(); // 不断重新执行main(绘制和清除) window.requestanimationframe(main); } // 随机函数 function randnum(m, n) { return math.floor(math.random() * (n - m + 1) + m); } // 随机颜色 function randcolor() { return 'rgb(' + randnum(0, 255) + ',' + randnum(0, 255) + ',' + randnum(0, 255) + ')'; } </script></body></html>
结果如下:
【相关推荐:javascript学习教程】
以上就是怎样利用javascript简单实现星空连线的效果的详细内容。