Math.sin()—-Math.cos()
曲线运动
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="Author" content=" ">
<title>Document</title>
<style type="text/css">
*{margin: 0;padding: 0;}
a{text-decoration: none;}
ul,li{list-style: none;}
body{font-size: 14px;font-family: "微软雅黑";}
#wrap{width: 400px;margin: 30px auto;}
#box{width: 400px;height: 400px;background: #333;position: relative;}
#ball{width: 10px;height: 10px;position: absolute;background: red;top: 0;left: 0}
#ball1{width: 10px;height: 10px;position: absolute;background: pink;top: 0;left: 0}
#x{width: 100%;border: 1px dashed red;position: absolute;top: 50%;}
#y{height: 100%;border: 1px dashed green;position: absolute;top: 0;}
</style>
</head>
<body>
<div id="wrap">
<input type="button" value="start" onclick="cstart()">
<input type="button" value="stop" onclick="cstop()">
<div id="box">
<div id="ball"></div>
<div id="ball1"></div>
<span id="x"></span>
<span id="y"></span>
</div>
</div>
<script type="text/javascript">
/*
三角函数 参数---弧度
Math.sin()
Math.cos()
Math.PI --- π 圆周率
角度 弧度
90° --- π/2
180°--- π
1° --- π/180° ===>
180°/π--- 1
deg ---- deg*π/180°
x -- 0.09 180*0.09/π
*/
//alert(Math.PI);
//alert(Math.sin(Math.PI/2));
//alert(Math.cos(Math.PI/2));
//画 sin曲线
var ball = document.getElementById("ball");
var ball1 = document.getElementById("ball1");
var x = 0;
var y = 0;
var xspeed = 2;
//弧度
var yspeed = 0.09; //5°
var y1 = 0;
var xspeed1 = 2;
//弧度
var yspeed1 = 0.09;
var timer = null;
function draw(){
//每次让x值加2
x += xspeed;
y += yspeed;
var cleft = x;
var ctop = 200 + Math.sin(y)*-50;
ball.style.left = cleft+"px";
ball.style.top = ctop+"px";
y1 += yspeed1;
var cleft1 = x;
var ctop1 = 200 + Math.cos(y1)*-50;
ball1.style.left = cleft1+"px";
ball1.style.top = ctop1+"px";
if(x>=400){
x=0;
}
}
timer = setInterval(draw,1000/30);
function cstart(){
if(timer)clearInterval(timer);//不能让计时器累加
timer = setInterval(draw,1000/30);
};
function cstop(){
clearInterval(timer);
}
</script>
</body>
</html>
曲线运动 ===例子
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
<title>运用-曲线运动</title>
<style type="text/css">
* { margin: 0; padding: 0;}
a {text-decoration: none;}
ul,li { list-style: none;}
body { font-family: "Microsoft yahei"; position: relative;}
#box {position: relative; width: 100%; height: 900px; background: #fafafa;}
#box div {position: absolute; top: 0; left: 0;opacity: 0; transition: opacity 1s;}
</style>
</head>
<body>
<div id="box"></div>
<script type="text/javascript">
var box = document.getElementById("box");
setInterval(draw,200);
function draw(){
var ball = document.createElement("div");
var size = randomNum(10,50);
var color = randomColor();
ball.style.width = size+"px";
ball.style.height = size+"px";
ball.style.borderRadius = size/2+"px";
ball.style.background = color;
box.appendChild(ball);
var x=0,y=0;
var xspeed = randomNum(7,10);
var yspeed = changeDeg(randomNum(4,6));
var range = randomNum(50,400);
var timer = setInterval(function(){
x +=xspeed;
y +=yspeed;
var cleft = x; //x轴的起点
var ctop = (400-size/2)+Math.sin(y)*-range; //y轴的起点+曲线的跳动的大小Math.sin(y)*-range
ball.style.left = cleft+'px';
ball.style.top = ctop+'px';
ball.style.opacity = 1;
if(cleft+size>=box.offsetWidth){
clearInterval(timer);
box.removeChild(ball);
}
},30)
};
function randomNum(start,end){
return Math.floor(Math.random()*(end-start+1)+start);
};
function randomColor(){
var r = Math.floor(Math.random()*256);
var g = Math.floor(Math.random()*256);
var b = Math.floor(Math.random()*256);
var a = Math.random().toFixed(1);
return "rgba("+r+","+g+","+b+","+a+")";
};
function changeDeg(deg){
return deg*Math.PI/180;
};
</script>
</body>
</html>
图片展示:
圆周运动
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="Author" content=" ">
<title>Document</title>
<style type="text/css">
*{margin: 0;padding: 0;}
a{text-decoration: none;}
ul,li{list-style: none;}
body{font-size: 14px;font-family: "微软雅黑";}
#wrap{width: 400px;margin: 30px auto;}
#box{width: 400px;height: 400px;background: #333;position: relative;}
#ball{width: 10px;height: 10px;position: absolute;background: red;top: 0;left: 0}
#ball1{width: 10px;height: 10px;position: absolute;background: pink;top: 0;left: 0}
</style>
</head>
<body>
<div id="wrap">
<input type="button" value="start" onclick="cstart()">
<input type="button" value="stop" onclick="cstop()">
<div id="box">
<div id="ball"></div>
<div id="ball1"></div>
</div>
</div>
<script type="text/javascript">
var ball = document.getElementById("ball");
var ball1 = document.getElementById("ball1");
var x = 0,y = Math.PI; //===控制起点 结合图
var xangle = changeDeg(1);//弧度
var yangle = changeDeg(1);
var timer = null;
function draw(){
x += xangle;
y += yangle; //[-1,1]==>循环
var cleft = 200-5 + Math.sin(x)*50; // 圆心 + 半径
var ctop = 200-5 + Math.cos(y)*50;
ball.style.left = cleft+"px";
ball.style.top = ctop+"px";
var cleft1 = 200-5 + Math.sin(x)*200;
var ctop1 = 200-5 + Math.cos(y)*200;
ball1.style.left = cleft1+"px";
ball1.style.top = ctop1+"px";
}
timer = setInterval(draw,1000/30);
function cstart(){
if(timer)clearInterval(timer);
timer = setInterval(draw,1000/30);
};
function cstop(){
clearInterval(timer);
}
function changeDeg(deg){ //角度转换为弧度
return deg/180*Math.PI;
}
</script>
</body>
</html>
圆周运动===>例子
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
<title>运用-圆周运动</title>
<style type="text/css">
* { margin: 0; padding: 0;}
a {text-decoration: none;}
ul,li { list-style: none;}
body { font-family: "Microsoft yahei"; position: relative;}
#box {position: relative; width: 100%; height: 900px; background: #fafafa;}
#box div {position: absolute; top: 0; left: 0;opacity: 0; transition: opacity 1s;}
</style>
</head>
<body>
<div id="box"></div>
<script type="text/javascript">
var box = document.getElementById("box");
setInterval(draw,200);
// draw()
var range = 50;//
function draw(){
var ball = document.createElement("div");
var size = randomNum(10,20);//球球的大小
var color = randomColor();
var Width = box.offsetWidth;// 在页面占据的宽度
var Height = box.offsetHeight;// 在页面占据的高度
ball.style.width = size+"px";
ball.style.height = size+"px";
ball.style.borderRadius = size/2+"px";
ball.style.background = color;
box.appendChild(ball);
var x=0,y=0,r=0;
var xspeed = changeDeg(4); //转动的速度 xspeed==yspeed时==>圆周运动
var yspeed = changeDeg(4);
/*
xspeed,yspeed转动的速度相等时==>圆周运动
xspeed!=yspeeds时==> 螺旋运动
xspeed>yspeeds时==> 垂直方向
xspeed<yspeeds时==> 水平方向
*/
range += 10;
r = range; // 半径自增
var timer = setInterval(function(){
x +=xspeed;
y +=yspeed;
var cleft = (Width/2-size/2)+Math.cos(x)*-r; // 圆心 + 半径
var ctop = (box.offsetHeight/2-size/2)+Math.sin(y)*-r;
ball.style.left = cleft+'px';
ball.style.top = ctop+'px';
ball.style.opacity = 1;
if(cleft+size>=Width||ctop+size>=Height){
clearInterval(timer);
box.removeChild(ball);
}
},30)
// range %= Width/2;
// console.log(range);
};
function randomNum(start,end){
return Math.floor(Math.random()*(end-start+1)+start);
};
function randomColor(){
var r = Math.floor(Math.random()*256);
var g = Math.floor(Math.random()*256);
var b = Math.floor(Math.random()*256);
var a = Math.random().toFixed(1);
return "rgba("+r+","+g+","+b+","+a+")";
};
function changeDeg(deg){//角度转换为弧度
return deg*Math.PI/180;
};
</script>
</body>
</html>
图片展示:
补充:
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
<title>圆圈</title>
<style type="text/css">
* { margin: 0; padding: 0;}
a {text-decoration: none;}
ul,li { list-style: none;}
body { font-family: "Microsoft yahei"; position: relative;}
#box {position: relative; width: 100%; height: 900px; background: #fafafa;}
#box div {position: absolute; top: 0; left: 0;opacity: 0; transition: opacity 1s;}
</style>
</head>
<body>
<div id="box"></div>
<script type="text/javascript">
var box = document.getElementById("box");
//setInterval(draw,200);
var num = 10;
for(var i=0;i<num;i++){
draw();
}
//console.log(1);
// draw();
var range = 50;//
function draw(){
var ball = document.createElement("div");
var size = randomNum(10,20);//球球的大小
var color = randomColor();
var Width = box.offsetWidth;// 在页面占据的宽度
var Height = box.offsetHeight;// 在页面占据的高度
ball.style.width = size+"px";
ball.style.height = size+"px";
ball.style.borderRadius = size/2+"px";
ball.style.background = color;
box.appendChild(ball);
var x=0,y=0,r=0;
var turnSpeed = randomNum(1,6) //转动的速度
var xspeed = changeDeg(turnSpeed); //转动的速度相等时 ==>圆周运动
var yspeed = changeDeg(turnSpeed); //
/*
xspeed,yspeed转动的速度相等时==>圆周运动
xspeed!=yspeeds时==> 螺旋运动
xspeed>yspeeds时==> 垂直方向
xspeed<yspeeds时==> 水平方向
*/
range += 10;
r = 100; // 控制半径
var timer = setInterval(function(){
x +=xspeed;
y +=yspeed;
// Math.方法相同===来回运动
var cleft = (Width/2-size/2)+Math.cos(x)*-r; // 圆心 + 半径
var ctop = (box.offsetHeight/2-size/2)+Math.sin(y)*-r;
ball.style.left = cleft+'px';
ball.style.top = ctop+'px';
ball.style.opacity = 1;
if(cleft+size>=Width||ctop+size>=Height){
clearInterval(timer);
box.removeChild(ball);
}
},30)
// range %= Width/2;
// console.log(range);
};
function randomNum(start,end){
return Math.floor(Math.random()*(end-start+1)+start);
};
function randomColor(){
var r = Math.floor(Math.random()*256);
var g = Math.floor(Math.random()*256);
var b = Math.floor(Math.random()*256);
var a = Math.random().toFixed(1);
return "rgba("+r+","+g+","+b+","+a+")";
};
function changeDeg(deg){//角度转换为弧度
return deg*Math.PI/180;
};
</script>
</body>
</html>