<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">
#kk{
box-shadow: 0 0 10px 0 sandybrown;
}
</style>
</head>
<body>
<canvas id="kk" width="500" height="500"></canvas>
</body>
<script type="text/javascript">
var myCanvas = document.getElementById("kk");
var context = myCanvas.getContext("2d");
// context.beginPath();
// context.fillStyle = "red";
// context.arc(100,100,30,0,Math.PI*2,false);
// context.fill();
// var ball = {
// r:30,
// x:100,
// y:100,
// color:"red",
// speedX:3,
// speedY:5
// }
function rand (min,max) {
return parseInt(Math.random()*(max-min+1)+min)
}
function Ball () {
this.r = rand(10,30);
this.x = rand(this.r,myCanvas.width-this.r);
this.y = rand(this.r,myCanvas.height-this.r);
this.color = `rgb(${rand(0,255)},${rand(0,255)},${rand(0,255)})`;
var rX = rand(-4,4);
var rY = rand(-4,4);
this.speedX = rX == 0?2:rX;
this.speedY = rY == 0?1:rY;
}
Ball.prototype.draw = function () {
context.beginPath();
context.fillStyle = this.color;
context.arc(this.x,this.y,this.r,0,Math.PI*2,false);
context.fill();
}
Ball.prototype.move = function () {
this.x+=this.speedX;
this.y+=this.speedY;
if(this.x+this.r>500 || this.x-this.r<0){
this.speedX*=-1;
}
if(this.y+this.r>500 || this.y-this.r<0){
this.speedY*=-1;
}
}
var balls = [];//用來存儲所有產(chǎn)生的對象
for(var i=0;i<20;i++){
var ball = new Ball();
balls.push(ball);
}
function animate () {
context.clearRect(0,0,myCanvas.width,myCanvas.height);
for(var j=0;j<balls.length;j++){
balls[j].draw();
balls[j].move();
}
window.requestAnimationFrame(animate);
}
animate();
</script>
</html>
直線運(yùn)動
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
相關(guān)閱讀更多精彩內(nèi)容
- 能夠滿足廣泛的自動化&直線運(yùn)動應(yīng)用 -獨(dú)特的刮擦動作,可以掃除碎屑,確保設(shè)備的正常和可靠運(yùn)行 -線性運(yùn)動系統(tǒng),可加...
- 走線的活動,除了培養(yǎng)孩子步行機(jī)能的平衡感覺、大肌肉協(xié)調(diào)之外,還培養(yǎng)孩子的意志力、獨(dú)立性,更可以使孩子充分的集中注意...
- (1)定義:物體沿著直線快慢不變的運(yùn)動. 汽車在平直的馬路上勻速行駛可以近似的認(rèn)為是勻速直線運(yùn)動。 為什么說是近似...
- 這張導(dǎo)圖講的是物理的勻變速直線運(yùn)動,中心圖是幾個(gè)在奔跑的小人~枝干介紹了速度、位移,速度、時(shí)間,位移、時(shí)間的一些公...
- 其實(shí)我們都太善于說服自己了 如果有諾貝爾謊言獎的話,我想有競爭力的人真的太多了。昨天,我還曾信誓旦旦的說:"回去好...