-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDemo 1.html
140 lines (110 loc) · 3.03 KB
/
Demo 1.html
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
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Ch2BaseFile - Template For Chapter 2 Examples</title>
<script type="text/javascript">
window.addEventListener('load', eventWindowLoaded, false);
function eventWindowLoaded() {
canvasApp();
}
function canvasApp(){
var theCanvas = document.getElementById("canvas"),
context = theCanvas.getContext("2d"),
center = xy(250,250),
viewPoint = xy(300,200),
maxRadius = 80,
minRadius = 20,
coils = [
{color:'#339944', radius: 20},
{color:'#33aa44', radius: 30},
{color:'#33bb44', radius: 40},
{color:'#33cc44', radius: 50},
{color:'#33dd44', radius: 60},
{color:'#33ee44', radius: 70},
{color:'#33ff44', radius: 80},
],
coilsLength = coils.length,
backCoils = [
{color:'#339944', radius: 15},
{color:'#339944', radius: 10},
{color:'#339944', radius: 5}
]
backCoilsLength = backCoils.length;
function animate() {
var i = 0, x, y;
clearCanvas();
context.fillStyle= '#dedede';
context.fillRect(0, 0, theCanvas.width, theCanvas.height);
xr = viewPoint.x - center.x;
yr = viewPoint.y - center.y;
for ( i; i < coilsLength; i+=1) {
x = center.x + xr/coilsLength * i;
y = center.y + yr/coilsLength * i;
new Circle( x, y , coils[i].radius, coils[i].color ).draw();
}
for ( i = 0; i < backCoilsLength; i+=1) {
x = center.x - xr/(coilsLength+backCoilsLength) * (i+1) /2 ;
y = center.y - yr/(coilsLength+backCoilsLength) * (i+1) /2 ;
new Circle( x, y , backCoils[i].radius, backCoils[i].color ).draw();
}
requestAnimationFrame(animate)
}
animate();
function Circle(x,y,r,color){
this.x = x;
this.y = y;
this.radius = r;
this.color = color || '#ccffcc';
Circle.prototype.draw = function() {
context.save();
context.fillStyle = this.color;
context.lineWidth = 2;
context.strokeStyle = this.color;
context.beginPath();
context.arc(this.x, this.y, this.radius, rad(0) , rad(360), false );
context.stroke();
context.closePath();
context.restore();
}
return this;
}
function rad( angle ) {
var radians = (Math.PI/180)*(angle-90);
return radians;
}
function xy(x,y){
return {x:x,y:y}
}
theCanvas.addEventListener('mousemove',function(e){
viewPoint = xy( e.clientX, e.clientY)
});
function clearCanvas() {
context.clearRect(0,0,theCanvas.width, theCanvas.height)
}
window.Square = function (centerX, centerY, width, color ) {
this.x = centerX;
this.y = centerY;
this.width = width;
this.color = color || '#ccffcc';
return this;
}
Square.prototype.draw = function(){
var x = this.x - this.width/2,
y = this.y - this.width/2;
context.save();
context.fillStyle = this.color;
context.fillRect(x, y, this.width, this.width);
context.restore();
}
//s1 = new Square(100,100,40, '#ccffff');
//s1.draw();
}
</script>
</head>
<body style="background:#ccc">
<div style="position: absolute; top: 0px; left: 0px;" >
<canvas id="canvas" width="500" height="500" style="background:#fff" ></canvas>
</div>
</body>
</html>