-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.html
117 lines (104 loc) · 4.29 KB
/
test.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
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Компьютерная Графика</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div onClick="start()" style="width: 250px; height: 50px; background-color: #ff6b6b; float: left;" onClick="start()">
<div style="margin: 0 0 0 100px ; width: 0; height: 0; border-top: 25px solid transparent; border-left: 50px solid #4b4bff; border-bottom: 25px solid transparent;"></div>
</div>
<div onClick="pause()" style="width: 250px; height: 50px; background-color: #4b4bff; float: left;" onClick="start()">
<div style="margin: 0 5px 0 100px; width: 20px; height: 50px; background-color: #ff4b4b; float: left;"></div>
<div style="width: 20px; height: 50px; background-color: #ff4b4b; float: left;"></div>
</div>
<div style="clear: both">
<canvas id="canvas" width="500" height="500">
<p>Ваш браузер не поддерживает HTML5</p>
<p>Рекомендую поставить <a href="http://google.com/chrome">Google Chrome</a></p>
</canvas>
</div>
<ul>
<li><a href="bezier.html">Bezier Curve</a></li>
<li><a href="test.html">Polygon rotate</a></li>
<li><a href="cube.html">Cube</a></li>
</ul>
<script src="util.js"></script>
<script src="shapes.js"></script>
<script>
var animation = false;
function start() {
animation = true;
}
function pause() {
animation = false;
}
window.onload = function () {
var canvas = document.getElementById('canvas'), //опеределяем канвас
context = canvas.getContext('2d'),
mouse = utils.captureMouse(canvas), //mouse возвращает координаты мыши
balls = new Array; //массив для хранения шариков
//инициализируем первые две точки
balls[0] = new Ball(10, '#f00', 50, 50);
balls[1] = new Ball(10, '#0f0', 120, 67);
//инициализируем квадратик
var rect = new Rectangle('#6b6bff', 0.23, balls[0].x, balls[0].y);
//добавляем событие при двойном клике ЛКМ
canvas.addEventListener('dblclick', function(){
var tmp_ball = new Ball(10, get_random_color(), mouse.x, mouse.y);
balls.push(tmp_ball);
}, false);
//при нажатии на ЛКМ
canvas.addEventListener('mousedown', function(event){
event.preventDefault();
for(var i=balls.length-1; i>=0; i--){
if(utils.containsPoint(balls[i].getBounds() , mouse.x, mouse.y)){
balls[i].dragged = true;
return true;
}
}
}, false);
//При отпускании ЛКМ
canvas.addEventListener('mouseup', function(){
if(balls.some(function(ball){return ball.dragged})){
balls.forEach(function(ball, i){
ball.dragged = false;
});
}
}, false);
//При перемещении ЛКМ
canvas.addEventListener('mousemove', function () {
balls.forEach(function(ball, i){
if (ball.dragged) {
ball.x = mouse.x;
ball.y = mouse.y;
}
});
}, false);
//Функция анимации
(function drawFrame () {
window.requestAnimationFrame(drawFrame, canvas);
context.clearRect(0, 0, canvas.width, canvas.height);
//Рисуем и квадратик
rect.move(animation, balls);
rect.draw(context);
context.beginPath();
for(var i=0; i<balls.length-1; i++){
context.moveTo(balls[i].x, balls[i].y);
context.lineTo(balls[i+1].x, balls[i+1].y);
}
context.lineTo(balls[0].x, balls[0].y);
context.stroke();
balls.forEach(function(ball){
ball.draw(context);
});
context.beginPath();
context.moveTo(balls[balls.length-1].x, balls[balls.length-1].y);
context.lineTo(balls[0].x, balls[0].y);
context.stroke();
})();
};
</script>
</body>
</html>