-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo01.js
165 lines (143 loc) · 4.18 KB
/
demo01.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/**
* Created by feiyu on 17/7/24.
*/
var canvas = document.getElementById("canvas01"),
ctx = canvas.getContext("2d"),
vpx = canvas.width/2,
vpy = canvas.height/2,
Radius = 150, //整体大球的坐标
LayerBallNum = 360 / 15, // 横向
LayerIntervalUp = 360 / 15; //
balls = [],
angleX = Math.PI/100,
angleY = Math.PI/100;
window.addEventListener("mousemove" , function(event){
var x = event.clientX - canvas.offsetLeft - vpx;
var y = event.clientY - canvas.offsetTop - vpy;
angleY = -x*0.0001;
angleX = -y*0.0001;
});
var Animation = function(){
this.init();
};
Animation.prototype = {
isrunning: false,
init: function () {
var num = LayerIntervalUp / 2; //layer 的数目 //假定每一层 间隔30 画上半球
for (var i = 0; i <=num; i++) {
var l = new layer(i, 1);
l.draw();
var l = new layer(i, -1);
l.draw();
}
},
start:function(){
this.isrunning = true;
animate();
},
stop:function(){
this.isrunning = false;
}
}
function animate(){
ctx.clearRect(0,0,canvas.width , canvas.height);
rotateX();
rotateY();
rotateZ();
for(var i=0;i<balls.length;i++){
balls[i].paint();
}
if(animation.isrunning) {
if("requestAnimationFrame" in window){
requestAnimationFrame(animate);
}
else if("webkitRequestAnimationFrame" in window){
webkitRequestAnimationFrame(animate);
}
else if("msRequestAnimationFrame" in window){
msRequestAnimationFrame(animate);
}
else if("mozRequestAnimationFrame" in window){
mozRequestAnimationFrame(animate);
}
}
}
var layer = function (num, up) {
this.radius = Math.sqrt(Math.pow(Radius,2) - Math.pow(Radius * Math.cos(num * Math.PI * 2 / LayerBallNum), 2))
this.x = 0;
this.y = 0;
this.up = up;
}
layer.prototype = {
setBalls: function (radius) {
for(var i=0; i<LayerBallNum; i++){
var angle = 2 * Math.PI / LayerBallNum * i;
var b = new ball(radius * Math.cos(angle), radius * Math.sin(angle), this.up * Math.sqrt(Math.pow(Radius, 2) - Math.pow(radius, 2)), 1.5);
b.paint();
balls.push(b);
}
},
draw: function () {
ctx.beginPath();
ctx.arc(vpx, vpy, this.radius , 0, 2*Math.PI, true);
ctx.strokeStyle = "#333";
ctx.stroke();
this.setBalls(this.radius);
}
}
var ball = function(x , y , z , r){
this.x = x;
this.y = y;
this.z = z;
this.r = r;
this.width = 2*r;
}
ball.prototype = {
paint:function(){
var fl = 450 //焦距
ctx.save();
ctx.beginPath();
var scale = fl / (fl - this.z);
var alpha = (this.z+Radius)/(2*Radius);
ctx.arc(vpx + this.x, vpy + this.y, this.r*scale , 0 , 2*Math.PI , true);
ctx.fillStyle = "rgba(0,0,0,"+(alpha+0.5)+")";
ctx.fill();
ctx.restore();
}
}
function rotateX(){
var cos = Math.cos(angleX);
var sin = Math.sin(angleX);
for(var i=0;i<balls.length;i++){
var y1 = balls[i].y * cos - balls[i].z * sin;
var z1 = balls[i].z * cos + balls[i].y * sin;
balls[i].y = y1;
balls[i].z = z1;
}
}
function rotateY(){
var cos = Math.cos(angleY);
var sin = Math.sin(angleY);
for(var i=0;i<balls.length;i++){
var x1 = balls[i].x * cos - balls[i].z * sin;
var z1 = balls[i].z * cos + balls[i].x * sin;
balls[i].x = x1;
balls[i].z = z1;
}
}
function rotateZ(){
var cos = Math.cos(angleY);
var sin = Math.sin(angleY);
for(var i=0;i<balls.length;i++){
var x1 = balls[i].x * cos - balls[i].y * sin;
var y1 = balls[i].y * cos + balls[i].x * sin;
balls[i].x = x1;
balls[i].y = y1;
}
}
var animation = new Animation();
animation.start();
// document.getElementById("controlBtn").onclick = function(){
// this.innerText === "开始" ? this.innerText="停止" : this.innerText="开始";
// this.innerText === "开始" ? animation.stop() : animation.start();;
// }