forked from DevinShine/MagicCircle
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMagicCircle.html
More file actions
174 lines (158 loc) · 3.89 KB
/
MagicCircle.html
File metadata and controls
174 lines (158 loc) · 3.89 KB
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
165
166
167
168
169
170
171
172
173
174
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Magic Circle</title>
<meta name="Keywords" content="">
<meta name="Description" content="">
<style type="text/css">
body, h1{margin:0;}
canvas{margin: 20px; }
</style>
</head>
<body>
<h1>三次贝塞尔曲线</h1>
<canvas id="canvas" width=1000 height=240 style="border: 1px solid #ccc;"></canvas>
<script>
function Point() {
var x = 0;
var y = 0;
}
function HPoint() {
this.x = 0;
this.y = 0;
this.left = new Point();
this.right = new Point();
this.setY = function(y){
this.y = y;
this.left.y = y;
this.right.y = y;
};
this.adjustAllX = function(offset){
this.x +=offset;
this.left.x +=offset;
this.right.x +=offset;
}
}
function VPoint() {
this.x = 0;
this.y = 0;
this.top = new Point();
this.bottom = new Point();
this.setX = function(x){
this.x = x;
this.top.x = x;
this.bottom.x = x;
};
this.adjustY = function(offset){
this.top.y -= offset;
this.bottom.y += offset;
};
this.adjustAllX = function(offset){
this.x+= offset;
this.top.x+= offset;
this.bottom.x+=offset;
}
}
var p2 = new VPoint();
var p4 = new VPoint();
var p1 = new HPoint();
var p3 = new HPoint();
var radius = 100;
var stretchDistance = radius;
var c = radius * 0.551915024494;
var cDistance = c * 0.45;
var maxLength = 1000 - radius - radius;
function model0(){
p1.setY(radius);
p3.setY(-radius);
p3.x = p1.x = 0;
p3.left.x = p1.left.x = -c;
p3.right.x = p1.right.x = c;
p2.setX(radius);
p4.setX(-radius);
p2.y = p4.y = 0;
p2.top.y = p4.top.y = -c;
p2.bottom.y = p4.bottom.y = c;
}
function model1(time){
model0();
p2.setX(radius+stretchDistance*time*5);
}
function model2(time){
model1(0.2);
time = (time - 0.2) * (10 / 3);
p1.adjustAllX(stretchDistance/2 * time );
p3.adjustAllX(stretchDistance/2 * time );
p2.adjustY(cDistance * time);
p4.adjustY(cDistance * time);
}
function model3(time){
model2(0.5);
time = (time - 0.5) * (10 / 3);
p1.adjustAllX(stretchDistance / 2 * time);
p3.adjustAllX(stretchDistance / 2 * time);
p2.adjustY(-cDistance * time);
p4.adjustY(-cDistance * time);
p4.adjustAllX(stretchDistance / 2 * time);
}
function model4(time){
model3(0.8);
time = (time - 0.8) * 10;
p4.adjustAllX(stretchDistance / 2 * time);
}
function model5(time){
model4(0.9);
time = time - 0.9;
p4.adjustAllX((Math.sin(Math.PI*time*10)*(2/10*radius)));
}
var canvas=document.getElementById('canvas');
var context=canvas.getContext('2d');
context.translate(radius,radius);
var i = 0;
var val = 10;
var count = 100;
function fuck() {
context.clearRect(-radius, -radius, canvas.width, canvas.height);
// context.clearRect(0, 0, canvas.width, canvas.height);
var time = i*val/1000;
console.log(time);
if(time>=0&&time<=0.2){
model1(time);
}else if(time>0.2&&time<=0.5){
model2(time);
}else if(time>0.5&&time<=0.8){
model3(time);
}else if(time>0.8&&time<=0.9){
model4(time);
}else if(time>0.9&&time<=1){
model5(time);
}
var offset = maxLength*(time-0.2);
offset = offset>0?offset:0;
p1.adjustAllX(offset);
p2.adjustAllX(offset);
p3.adjustAllX(offset);
p4.adjustAllX(offset);
// model0();
//绘制3次贝塞尔曲线
context.beginPath();
context.moveTo(p1.x,p1.y);
context.bezierCurveTo(p1.right.x, p1.right.y, p2.bottom.x, p2.bottom.y, p2.x,p2.y);
context.bezierCurveTo(p2.top.x, p2.top.y, p3.right.x, p3.right.y, p3.x,p3.y);
context.bezierCurveTo(p3.left.x, p3.left.y, p4.top.x, p4.top.y, p4.x,p4.y);
context.bezierCurveTo(p4.bottom.x,p4.bottom.y,p1.left.x,p1.left.y,p1.x,p1.y);
context.strokeStyle = "red";
context.fillStyle="red";
context.fill();
context.stroke();
// draw();
i++;
if(i >= count) {
clearInterval(itFuck);
}
}
var itFuck = setInterval(fuck, val);
</script>
</body>
</html>