canvas 連線動畫

html代碼

<canvas id="canvas">您的瀏覽器不支持canvas</canvas>

css代碼

#canvas {
    background-color: cadetblue;
    display: block;
    overflow: hidden;
}

javascript代碼

var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
//畫布大小
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
//鼠標路徑
var mouseX = canvas.width / 2;
var mouseY = canvas.height / 2;
//瀏覽器窗口改變
window.onresize = function(){
    canvas.width = window.innerWidth
    canvas.height = window.innerHeight
}
//生成隨機數
function randomNum(x, y) {
  return Math.floor(Math.random() * (y - x + 1) + x);
}
    
// 創建小球對象
function Ball() {}
    
Ball.prototype = {
  // 初始化
  init: function(){
    this.r = randomNum(0.1, 3); // 小球半徑
    this.color = "#fff"; // 小球顏色
    // 隨機座標軸
    this.x = randomNum(this.r, canvas.width - this.r); 
    this.y = randomNum(this.r, canvas.height - this.r);
    //隨機速度
    this.speedX = randomNum(1, 3) * (randomNum(0, 1) ? 1 : -1);
    this.speedY = randomNum(1, 3) * (randomNum(0, 1) ? 1 : -1);
  },
    
  move: function(){ // 小球移動
    this.x += this.speedX * 0.2; // x軸移動
    this.y += this.speedY * 0.2; // y軸移動
    
    // 左邊界
    if(this.x <= this.r) {
        this.x = this.r;
        // 反方向
        this.speedX *= -1;
    }
    // 右邊界
    if(this.x >= canvas.width - this.r) {
        this.x = canvas.width - this.r
        this.speedX *= -1;
    }
    //上邊界
    if(this.y <= this.r) {
        this.y = this.r;
        this.speedY *= -1;
    }
    //下邊界 
    if(this.y >= canvas.height - this.r) {
        this.y = canvas.height - this.r;
        this.speedY *= -1;
    }
  },
  draw: function(){ // 繪製小球
    ctx.beginPath();
    ctx.arc(this.x, this.y, this.r, 0, Math.PI * 2, false);
    ctx.fillStyle = this.color;
    ctx.fill();
  }
}
    
var balls = []; // 小球盒子
for(var i = 0; i < 0.0002 * canvas.width * canvas.height; i++) {
  ball = new Ball(); // 實例化對象
  ball.init() // 初始化數據
  balls.push(ball);
}
    
setInterval(function() {
ctx.clearRect(0, 0, canvas.width, canvas.height); // 清除畫布
  for(var i = 0; i < balls.length; i++) {
    balls[i].move(); // 小球移動
    balls[i].draw(); // 更新小球位置
    if(ball_to_mouse(balls[i]) < 130) { // 球體與鼠標距離-->畫線
    ctx.lineWidth = (130 - ball_to_mouse(balls[i])) * 1.5 / 130;
    ctx.beginPath();
    ctx.moveTo(balls[i].x, balls[i].y);
    ctx.lineTo(mouseX, mouseY);
    ctx.strokeStyle = balls[i].color;
    ctx.stroke();
  }
}
    
  for(var i = 0; i < balls.length; i++) {
    for(var j = 0; j < balls.length; j++) {
        if(ball_to_ball(balls[i], balls[j]) < 80) { // 球體之間距離-->畫線
            ctx.lineWidth = (80 - ball_to_ball(balls[i], balls[j])) * 0.6 / 80;
            // 線體透明
            ctx.globalAlpha = (130 - ball_to_ball(balls[i], balls[j])) * 1 / 80;  
            ctx.beginPath();
            ctx.moveTo(balls[i].x, balls[i].y);
            ctx.lineTo(balls[j].x, balls[j].y);
            ctx.strokeStyle = balls[i].color;
            ctx.stroke();
        }
    }
  }
    
  // 當前畫布透明度
  ctx.globalAlpha = 1.0;
    
}, 30);
//鼠標移動
canvas.|| window.event;
  mouseX = e.offsetX;
  mouseY = e.offsetY;
}
//鼠標跟每個點之間的距離
function ball_to_mouse(obj) {
  var disX = Math.abs(mouseX - obj.x);
  var disY = Math.abs(mouseY - obj.y);
  return Math.sqrt(disX * disX + disY * disY);
}
//兩點之間的距離
  function ball_to_ball(obj1, obj2) {
  var disX = Math.abs(obj1.x - obj2.x);
  var disY = Math.abs(obj1.y - obj2.y);
  return Math.sqrt(disX * disX + disY * disY);
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章