HTML5 canvas鼠标经过星星连线

简介: HTML5 canvas鼠标经过星星连线

HTML5 canvas鼠标经过星星连线,鼠标经过星空可将星星一颗颗连成直线,页面背景有变色效果。

完整项目文件(关注后下载免费不需要积分):https://download.csdn.net/download/qq_44273429/13781365


HTML代码

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>互动星空</title>
<style>
html,body {
  margin:0;
  overflow:hidden;
  width:100%;
  height:100%;
  cursor:none;
  background:black;
  background:linear-gradient(to bottom,#000000 0%,#5788fe 100%);
}
.filter {
  width:100%;
  height:100%;
  position:absolute;
  top:0;
  left:0;
  background:#fe5757;
  animation:colorChange 30s ease-in-out infinite;
  animation-fill-mode:both;
  mix-blend-mode:overlay;
}
@keyframes colorChange {
  0%,100% {
  opacity:0;
}
50% {
  opacity:.9;
}
}.landscape {
  position:absolute;
  bottom:0px;
  left:0;
  width:100%;
  height:100%;
  /*background-image:url(img/Trees-Landscape-Silhouette.png);
  */
background-image:url('img/xkbg.png');
  background-size:1000px 250px;
  background-repeat:repeat-x;
  background-position:center bottom;
}
</style>
</head>
<body>
<div class="landscape"></div>
<div class="filter"></div>
<canvas id="canvas"></canvas>
<script>
function Star(id, x, y){
  this.id = id;
  this.x = x;
  this.y = y;
  this.r = Math.floor(Math.random()*2)+1;
  var alpha = (Math.floor(Math.random()*10)+1)/10/2;
  this.color = "rgba(255,255,255,"+alpha+")";
}
Star.prototype.draw = function() {
  ctx.fillStyle = this.color;
  ctx.shadowBlur = this.r * 2;
  ctx.beginPath();
  ctx.arc(this.x, this.y, this.r, 0, 2 * Math.PI, false);
  ctx.closePath();
  ctx.fill();
}
Star.prototype.move = function() {
  this.y -= .15;
  if (this.y <= -10) this.y = HEIGHT + 10;
  this.draw();
}
Star.prototype.die = function() {
    stars[this.id] = null;
    delete stars[this.id];
}
function Dot(id, x, y, r) {
  this.id = id;
  this.x = x;
  this.y = y;
  this.r = Math.floor(Math.random()*5)+1;
  this.maxLinks = 2;
  this.speed = .5;
  this.a = .5;
  this.aReduction = .005;
  this.color = "rgba(255,255,255,"+this.a+")";
  this.linkColor = "rgba(255,255,255,"+this.a/4+")";
  this.dir = Math.floor(Math.random()*140)+200;
}
Dot.prototype.draw = function() {
  ctx.fillStyle = this.color;
  ctx.shadowBlur = this.r * 2;
  ctx.beginPath();
  ctx.arc(this.x, this.y, this.r, 0, 2 * Math.PI, false);
  ctx.closePath();
  ctx.fill();
}
Dot.prototype.link = function() {
  if (this.id == 0) return;
  var previousDot1 = getPreviousDot(this.id, 1);
  var previousDot2 = getPreviousDot(this.id, 2);
  var previousDot3 = getPreviousDot(this.id, 3);
  if (!previousDot1) return;
  ctx.strokeStyle = this.linkColor;
  ctx.moveTo(previousDot1.x, previousDot1.y);
  ctx.beginPath();
  ctx.lineTo(this.x, this.y);
  if (previousDot2 != false) ctx.lineTo(previousDot2.x, previousDot2.y);
  if (previousDot3 != false) ctx.lineTo(previousDot3.x, previousDot3.y);
  ctx.stroke();
  ctx.closePath();
}
function getPreviousDot(id, stepback) {
  if (id == 0 || id - stepback < 0) return false;
  if (typeof dots[id - stepback] != "undefined") return dots[id - stepback];
  else return false;//getPreviousDot(id - stepback);
}
Dot.prototype.move = function() {
  this.a -= this.aReduction;
  if (this.a <= 0) {
    this.die();
    return
  }
  this.color = "rgba(255,255,255,"+this.a+")";
  this.linkColor = "rgba(255,255,255,"+this.a/4+")";
  this.x = this.x + Math.cos(degToRad(this.dir))*this.speed,
  this.y = this.y + Math.sin(degToRad(this.dir))*this.speed;
  this.draw();
  this.link();
}
Dot.prototype.die = function() {
    dots[this.id] = null;
    delete dots[this.id];
}
var canvas  = document.getElementById('canvas'),
  ctx = canvas.getContext('2d'),
  WIDTH,
  HEIGHT,
  mouseMoving = false,
  mouseMoveChecker,
  mouseX,
  mouseY,
  stars = [],
  initStarsPopulation = 80,
  dots = [],
  dotsMinDist = 2,
  maxDistFromCursor = 50;
setCanvasSize();
init();
function setCanvasSize() {
  WIDTH = document.documentElement.clientWidth,
    HEIGHT = document.documentElement.clientHeight;                      
  canvas.setAttribute("width", WIDTH);
  canvas.setAttribute("height", HEIGHT);
}
function init() {
  ctx.strokeStyle = "white";
  ctx.shadowColor = "white";
  for (var i = 0; i < initStarsPopulation; i++) {
    stars[i] = new Star(i, Math.floor(Math.random()*WIDTH), Math.floor(Math.random()*HEIGHT));
    //stars[i].draw();
  }
  ctx.shadowBlur = 0;
  animate();
}
function animate() {
    ctx.clearRect(0, 0, WIDTH, HEIGHT);
    for (var i in stars) {
      stars[i].move();
    }
    for (var i in dots) {
      dots[i].move();
    }
    drawIfMouseMoving();
    requestAnimationFrame(animate);
}
window.onmousemove = function(e){
  mouseMoving = true;
  mouseX = e.clientX;
  mouseY = e.clientY;
  clearInterval(mouseMoveChecker);
  mouseMoveChecker = setTimeout(function() {
    mouseMoving = false;
  }, 100);
}
function drawIfMouseMoving(){
  if (!mouseMoving) return;
  if (dots.length == 0) {
    dots[0] = new Dot(0, mouseX, mouseY);
    dots[0].draw();
    return;
  }
  var previousDot = getPreviousDot(dots.length, 1);
  var prevX = previousDot.x; 
  var prevY = previousDot.y; 
  var diffX = Math.abs(prevX - mouseX);
  var diffY = Math.abs(prevY - mouseY);
  if (diffX < dotsMinDist || diffY < dotsMinDist) return;
  var xVariation = Math.random() > .5 ? -1 : 1;
  xVariation = xVariation*Math.floor(Math.random()*maxDistFromCursor)+1;
  var yVariation = Math.random() > .5 ? -1 : 1;
  yVariation = yVariation*Math.floor(Math.random()*maxDistFromCursor)+1;
  dots[dots.length] = new Dot(dots.length, mouseX+xVariation, mouseY+yVariation);
  dots[dots.length-1].draw();
  dots[dots.length-1].link();
}
//setInterval(drawIfMouseMoving, 17);
function degToRad(deg) {
  return deg * (Math.PI / 180);
}
</script>
<div style="text-align:center;">
<p><a href="https://blog.csdn.net/qq_44273429" target="_blank">海拥</a></p>
</div>
</body>
</html>
目录
相关文章
|
3月前
|
移动开发 前端开发 JavaScript
Twaver-HTML5基础学习(12)连线(Link)
本文介绍了Twaver HTML5中的连线(Link)元素,包括设置起始和结束节点的方法,以及如何管理和操作多个连线。通过示例代码展示了如何在React组件中创建和管理Link,包括设置连线颜色和标签偏移量。
96 2
Twaver-HTML5基础学习(12)连线(Link)
|
3月前
|
移动开发 前端开发 JavaScript
Twaver-HTML5基础学习(15)形状连线(ShapeLink)四种类型(直线、正交、二次贝塞尔、三次贝塞尔)
本文介绍了Twaver HTML5中的形状连线(ShapeLink),包括如何使用它以及如何添加控制点。文章详细解释了ShapeLink支持的四种连线类型:直线(lineto)、正交(orthogonalto)、二次贝塞尔曲线(quadto)和三次贝塞尔曲线(cubicto),并通过示例代码展示了如何在React组件中创建和配置ShapeLink。
59 3
|
17天前
|
Web App开发 移动开发 前端开发
html5 canvas五彩碎纸屑飘落动画特效
h5 canvas飘落纸片动画是一款实现五彩纸屑飘落的背景动画特效,基于canvas绘制的空中飘落的纸屑片动画特效,适用于网页动态背景效果代码。简单使用,欢迎下载!代码适用浏览器:搜狗、360、FireFox(建议)、Chrome、Safari、Opera、傲游、世界之窗,是一款不错的的特效插件,希望大家喜欢!
34 5
|
28天前
|
前端开发
基于canvas实现的彩色纸屑组成文字3d动画HTML源码
基于canvas实现的彩色纸屑组成文字3d动画HTML源码
23 0
基于canvas实现的彩色纸屑组成文字3d动画HTML源码
|
28天前
|
移动开发 前端开发 HTML5
HTML5 Canvas制作的粒子十秒倒计时源码
一段基于HTML5 Canvas制作的粒子爆炸,十秒数字倒计时,全屏倒计时动画效果,给人一种非常大气的视觉感
33 0
HTML5 Canvas制作的粒子十秒倒计时源码
|
1月前
|
JavaScript
JS鼠标框选并删除HTML源码
这是一个js鼠标框选效果,可实现鼠标右击出现框选效果的功能。右击鼠标可拖拽框选元素,向下拖拽可实现删除效果,简单实用,欢迎下载
42 4
|
5月前
|
移动开发 前端开发 JavaScript
基于 HTML5 和 Canvas 开发的在线图片编辑器
基于 HTML5 和 Canvas 开发的在线图片编辑器
113 0
|
1月前
|
前端开发 JavaScript
Canvas三维变化背景动画HTML源码
Canvas三维变化背景动画HTML源码
32 5
|
3月前
|
移动开发 前端开发 JavaScript
Twaver-HTML5基础学习(13)连线(Link)连线的绑定与展开
本文介绍了Twaver HTML5中连线(Link)的绑定与展开功能,包括分组绑定、自环绑定、绑定与展开以及展开间隙等属性的设置。通过示例代码展示了如何在React组件中创建Link并设置其绑定属性,实现连线的分组管理。
34 4
Twaver-HTML5基础学习(13)连线(Link)连线的绑定与展开
|
3月前
|
移动开发 前端开发 JavaScript
Twaver-HTML5基础学习(14)连线(Link)连线类型(直线、延伸直线、正交直线)
本文介绍了Twaver HTML5中连线(Link)的不同类型,包括直线、延伸直线和正交直线,并通过示例代码展示了如何在React组件中设置Link的类型和样式。
34 1
Twaver-HTML5基础学习(14)连线(Link)连线类型(直线、延伸直线、正交直线)