-
Notifications
You must be signed in to change notification settings - Fork 0
/
example2.html
175 lines (154 loc) · 4.94 KB
/
example2.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
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
175
<html>
<head>
<script type="text/javascript">
var canvas; //deklaracja obiektu "płótno"
var c; //na płótnie wyświetlana jest grafika
var vertex=[[10,10],[10,210],[210,210],[210,10]]; //tablica punktów (wierzchołków) w R2
function clear_canvas(){ //czyszczenie płotna
canvas=document.getElementById("canvas");
if(!canvas.getContext){return;}
c=canvas.getContext("2d");
c.clearRect(0,0,canvas.width,canvas.height);}
function draw(){
canvas=document.getElementById("canvas1");
if(!canvas.getContext){return;}
c=canvas.getContext("2d");
c.fillStyle="rgb(200,0,0)";
c.fillRect(10,10,55,50);
c.fillStyle="rgba(0,0,200,0.5)";
c.fillRect(30,30,55,50);}
function line(a, b){ //odcinek o końach a i b
canvas=document.getElementById("canvas");
if(!canvas.getContext){return;}
c=canvas.getContext("2d");
c.beginPath();
c.moveTo(a[0],a[1]);
c.lineTo(b[0],b[1]);
c.stroke();
}
var p1=[100,0]; // deklaracja punktów na płaszczyźnie
var p2=[40,40];
function d(){line(p1,p2);} //funkcja rysujaca odcinek o koncach p1 i p2
function polyline(a){ //funkcja rysująca łamaną o wierzchołkach z tablicy "a"
canvas=document.getElementById("canvas");
if(!canvas.getContext){return;}
c=canvas.getContext("2d");
c.beginPath();
c.moveTo(a[0][0],a[0][1]);
for(i=0; i<a.length; i++)
{c.lineTo(a[i][0],a[i][1]);}
c.stroke();
}
function polygon(a){ //funkcja rysująca wielokąt o wierzchołkach z tablicy "a"
canvas=document.getElementById("canvas");
if(!canvas.getContext){return;}
c=canvas.getContext("2d");
c.beginPath();
c.moveTo(a[0][0],a[0][1]);
for(i=0; i<a.length; i++)
{c.lineTo(a[i][0],a[i][1]);}
c.closePath();
c.stroke();
}
function fillPolygon(a,color){ //funkcja rysująca wielokąt o wierzchołkach z tablicy "a" zamalowany kolorem "color"
canvas=document.getElementById("canvas");
if(!canvas.getContext){return;}
c=canvas.getContext("2d");
c.beginPath();
c.moveTo(a[0][0],a[0][1]);
for(i=0; i<a.length; i++)
{c.lineTo(a[i][0],a[i][1]);}
c.closePath();
c.fillStyle=color;
c.fill();
}
function degreesToRadians(angle) { //zamiana stopni na radiany
return radians = (Math.PI/180)*angle;
}
function rotate(angle,a,b){ //obrót punktu "b" wokół punktu "a" o kąt "angle"
angle=degreesToRadians(angle);
d0=b[0]; d1=b[1];
b[0]=(d0-a[0])*Math.cos(angle)-(d1-a[1])*Math.sin(angle) + a[0];
b[1]=(d0-a[0])*Math.sin(angle)+(d1-a[1])*Math.cos(angle) + a[1];
return b;
}
function rotateVertex(a,v){ //obrót tablicy wierzchołków wokół punktu "a"
for(i=0; i<v.length; i++)
{rotate(15,a,v[i]);}
return v;
}
function rotatePolygon(){ //obracanie wielokąta
rotateVertex(vertex[2],vertex); //obrót tablicy "vertex" wokół punktu "vertex[2]"
clear_canvas();
fillPolygon(vertex,'green');
}
var play; // animacja z ustaleniem przedziału czasowego (time interval)
function startAnimation(){
play=setInterval('rotatePolygon()',100);
}
function stopAnimation(){
clearInterval(play);
}
function translate(a,b){
a[0]=a[0]+b[0];
a[1]=a[1]+b[1];
return a;
}
function translateVertex(v,b){
for(i=0; i<v.length; i++)
{translate(v[i],b);}
return v;
}
function keyDownHandler(e) //zdarzenia przypisane do klawiszy
{ //sterowanie za pomoca klawiatury
e = e || event;
var keyCode = e.keyCode,
letter = (String.fromCharCode(e.keyCode) || '').toLowerCase();
if (37==keyCode)
{
translateVertex(vertex,[-5,0]);
clear_canvas();
fillPolygon(vertex,'green');
}
if (38==keyCode)
{
translateVertex(vertex,[0,-5]);
clear_canvas();
fillPolygon(vertex,'green');
}
if (39==keyCode)
{
translateVertex(vertex,[5,0]);
clear_canvas();
fillPolygon(vertex,'green');
}
if (40 === keyCode)
{
translateVertex(vertex,[0,5]);
clear_canvas();
fillPolygon(vertex,'green');
}
return false;
}
</script>
</head>
<body onkeyup="keyDownHandler(event)">
<!--okreslenie wymiarów płótna -->
<canvas id="canvas" width='600' height='400'>
<p>Sorry: Browser does not support Graphics Canvas</p></canvas><br/>
<button onclick="clear_canvas();">Wyczyść płótno</button>
<button onclick="line([0,0],[40,40]);">Odcinek</button>
<button onclick="line(p1,p2);">Odcinek 2</button>
<button onclick="polyline([[1,1],[1,30],[30,30],[100,100]]);">Łamana</button>
<button onclick="polygon([[1,1],[1,30],[30,30],[100,30]]);">Wielokąt</button>
<button onclick="fillPolygon(vertex,'green');">Wielokat wypełniony kolorem</button>
<button onclick="startAnimation();">START</button>
<button onclick="stopAnimation();">STOP</button>
<br />
Przesuwanie za pomocą klawiszy strzałek
<br/>
<canvas id="canvas1" width='150' height='150'>
<p>Sorry: Browser does not support Graphics Canvas</p></canvas><br/>
<button onclick="draw();">Wyświetl</button>
</body>
</html>