JavaScript canvas實(shí)現(xiàn)跟隨鼠標(biāo)移動(dòng)小球
本文實(shí)例為大家分享了js跟隨鼠標(biāo)移動(dòng)小球的具體代碼,供大家參考,具體內(nèi)容如下
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style>
canvas{
border: 1px solid #000;
}
</style>
</head>
<body>
<canvas id="mycanvas" width="1500" height="800"></canvas>
<script>
// 創(chuàng)建畫布
var canvas = document.getElementById('mycanvas');
var ctx = canvas.getContext('2d');
// 球類
function Ball(x, y) {
this.x = x;
this.y = y;
// 初始半徑
this.r = parseInt(Math.random() * 50) + 10;
this.step = parseInt(Math.random() * 5) + 0.1;
// 設(shè)置隨機(jī)顏色
this.color = getRandom();
// 設(shè)置隨機(jī)方向
this.dx = parseInt(Math.random() * 10) - 5;
this.dy = parseInt(Math.random() * 10) - 5;
// 將自身對(duì)象裝入數(shù)組中
ballArr.push(this);
}
// 在數(shù)組中刪除對(duì)象
Ball.prototype.remove = function() {
for (var i = 0; i < ballArr.length; i++) {
if (ballArr[i] == this) {
ballArr.splice(i, 1);
}
}
}
// 更新數(shù)據(jù)
Ball.prototype.update = function() {
// 更新數(shù)據(jù)
this.x += this.dx;
this.y += this.dy;
this.r -= this.step;
// 清除數(shù)組中的小球
if (this.r <= 0) {
this.remove();
}
// 如果超出邊界,小球繼續(xù)運(yùn)動(dòng)
if (this.x < 0) {
this.x = 1500;
this.color = getRandom();
}
else if (this.x > 1500) {
this.x = 0;
this.color = getRandom();
}
else if (this.y < 0) {
this.y = 800;
this.color = getRandom();
}
else if (this.y > 800) {
this.y = 0;
this.color = getRandom();
}
}
// 渲染小球
Ball.prototype.render = function() {
ctx.beginPath();
ctx.arc(this.x, this.y, this.r, 0, Math.PI * 2, false);
ctx.fillStyle = this.color;
ctx.fill();
}
// canvas 畫布DOM2事件
canvas.addEventListener("mousemove", function(event) {
new Ball(event.offsetX, event.offsetY);
});
var ballArr = [];
// 定時(shí)器進(jìn)行動(dòng)畫渲染和更新
setInterval(function() {
// 動(dòng)畫邏輯
// 清屏-更新-渲染
ctx.clearRect(0, 0, canvas.width, canvas.height);
// 小球的更新和渲染
for (var i = 0; i < ballArr.length; i++) {
ballArr[i].update();
if (ballArr[i]) {
ballArr[i].render();
}
}
}, 30);
// 隨機(jī)顏色
function getRandom() {
var allType = "0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f";
var allTypeArr = allType.split(",");
var color = "#";
// 拼接顏色字符串
for (var i = 0; i < 6; i++) {
var random = parseInt(Math.random() * allTypeArr.length);
color += allTypeArr[random];
}
return color;
}
</script>
</body>
</html>
效果

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
JS中2種定時(shí)器的使用及清除的實(shí)現(xiàn)
本文主要介紹了JS中2種定時(shí)器的使用及清除的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08
微信小程序與后臺(tái)PHP交互的方法實(shí)例分析
這篇文章主要介紹了微信小程序與后臺(tái)PHP交互的方法,結(jié)合實(shí)例形式分析了微信小程序基于wx.request(OBJECT)方法與后臺(tái)php程序交互相關(guān)操作技巧與注意事項(xiàng),需要的朋友可以參考下2018-12-12
javascript實(shí)現(xiàn)的HashMap類代碼
這篇文章主要介紹了javascript實(shí)現(xiàn)的HashMap類代碼,實(shí)現(xiàn)了添加、獲取、刪除、查詢key和value功能,需要的朋友可以參考下2014-06-06
webpack與SPA實(shí)踐之管理CSS等資源的方法
本篇文章主要介紹了webpack與SPA實(shí)踐之管理CSS等資源的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-12-12
JavaScript高級(jí)程序設(shè)計(jì) 閱讀筆記(十七) js事件
IE中是冒泡型事件,即從最特定的事件目標(biāo)到最不特定的事件目標(biāo)2012-08-08
跨瀏覽器的 mouseenter mouseleave 以及 compareDocumentPosition的使用說明
昨天去 大牛 司徒正美 的blog 看博文 突然看到 關(guān)于 onmouseenter 和onmouseleave 兩個(gè)ie專有事件..2010-05-05
element-ui 時(shí)間選擇器限制范圍的實(shí)現(xiàn)(隨動(dòng))
這篇文章主要介紹了element-ui 時(shí)間選擇器限制范圍(隨動(dòng)),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-01-01
js apply/call/caller/callee/bind使用方法與區(qū)別分析
js apply/call/caller/callee/bind使用方法與區(qū)別分析,需要的朋友可以參考下。2009-10-10

