台球

<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="Generator" content="EditPlus®">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
<title>Document</title>
</head>
<body>
<canvas id="myCanvas" width="800" height="400" style="background-color:#000000">
Your broswer does not support the canvas element.
</canvas>

<script type="text/javascript">
var c=document.getElementById("myCanvas");
var cxt=c.getContext("2d");
var r=10;
var balls=[];
function initBall(){
var parentBall={
position:{x:200,y:150},
r:r,
color:"#fff"
};
function randomColor(){
var arrHex=["0","1","2","3","4","5","6","7","8","9","A","B","C","D","E"];
var strHex="#";
var index;
for(var i=0;i<6;i++){
index=Math.round(Math.random()*15);
strHex+=arrHex[index];
}
return strHex;
}
var firstBall={
position:{x:400,y:150},
r:r,
color:randomColor()
}
for(var j=1;j<5;j++){
for(var k=0;k<j+1;k++){
var ball={
position:{x:firstBall.position.x+j*Math.sqrt(3)*r,y:firstBall.position.y-j*r+2*k*r},
r:r,
color:randomColor()//产生随机颜色
}
balls.push(ball);
}
}
balls.push(firstBall);
balls.push(parentBall);
}//15个小球的画法
function draw(){
for(i in balls){
cxt.fillStyle=balls[i].color;
cxt.beginPath();
cxt.arc(balls[i].position.x,balls[i].position.y,balls[i].r,0,Math.PI*2,true);
cxt.closePath();
cxt.fill();
}
}
initBall();
draw();

</script>
</body>
</html>

转载于:https://www.cnblogs.com/amy-hu/p/6108227.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值