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);
}創新互聯www.cdcxhl.cn,專業提供香港、美國云服務器,動態BGP最優骨干路由自動選擇,持續穩定高效的網絡助力業務部署。公司持有工信部辦法的idc、isp許可證, 機房獨有T級流量清洗系統配攻擊溯源,準確進行流量調度,確保服務器高可用性。佳節活動現已開啟,新人活動云服務器買多久送多久。
網頁標題:canvas連線動畫-創新互聯
標題路徑:http://www.yijiale78.com/article42/icoec.html
成都網站建設公司_創新互聯,為您提供網站排名、移動網站建設、App設計、軟件開發、搜索引擎優化、網站維護
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯