-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdrop.js
executable file
·134 lines (119 loc) · 3.91 KB
/
drop.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
var refreshRate = 45;
var gravity = 10 / refreshRate;
var canvas = document.getElementById('HTMLCanvas');
var context = canvas.getContext('2d');
var scene = {
balls: [],
lines: [],
droppers: []
};
function display() {
display_clear();
var dead = [];
var i;
for (i=scene.balls.length-1; i>=0; i--) {
scene.balls[i].update();
if (scene.balls[i].isNowDead(scene)) {
dead[dead.length] = i;
continue;
}
scene.balls[i].applyGravity();
scene.balls[i].draw(context);
}
for (i=dead.length-1; i>0; i--) {
scene.balls.splice(dead[i],1);
}
for (i=scene.droppers.length-1; i>=0; i--) {
scene.droppers[i].draw(context);
}
for (i=scene.lines.length-1; i>=0; i--) {
scene.lines[i].draw(context);
}
hittest();
setTimeout(display, refreshRate);
}
function display_clear() {
context.clearRect(0, 0, canvas.width, canvas.height);
context.fillStyle='#fff';
context.fillRect(0, 0, canvas.width, canvas.height);
}
function hittest() {
var i,j;
for (i=scene.balls.length-1; i>=0; i--) {
for (j=scene.lines.length-1; j>=0; j--) {
if (scene.balls[i].intersects(scene.lines[j])) {
scene.balls[i].bounceOffLine(scene.lines[j]);
scene.lines[j].ding();
}
}
}
}
var lineStart = false;
var lineMove = null;
var droppedPoint = {};
var lineHalfx, lineHalfy;
function init() {
var thickness = 10; //test thickness
setTimeout(display, refreshRate);
scene.width = canvas.width;
scene.height = canvas.height;
scene.droppers[scene.droppers.length] = new Dropper(150, 150, 1000, scene);
document.oncontextmenu = function () {return false;};
document.onmouseup = function (e) {
if (e.button==2) scene.droppers[scene.droppers.length] = new Dropper(e.pageX - 10, e.pageY - 10, 500, scene);
else if (e.button==0) {
if (lineStart) {
if (Math.pow(lineHalfx - e.pageX, 2) + Math.pow(lineHalfy - e.pageY, 2) > Math.pow(thickness, 2)) {
scene.lines[scene.lines.length] = new Line(lineHalfx, lineHalfy, e.pageX, e.pageY);
scene.lines[scene.lines.length-1].ding();
}
lineStart = false;
}
else if (lineMove != null) {
var xMove = droppedPoint['x'] - e.pageX;
var yMove = droppedPoint['y'] - e.pageY;
xMove = Math.abs(xMove) > thickness ? xMove : 0;
yMove = Math.abs(yMove) > thickness ? yMove : 0;
lineMove.x1 -= xMove;
lineMove.x2 -= xMove;
lineMove.y1 -= yMove;
lineMove.y2 -= yMove;
lineMove.recompute();
lineMove.color = lineMove.BASE_COLOR;
lineMove = null;
}
else {
lineHalfx = e.pageX;
lineHalfy = e.pageY;
lineStart = true;
}
}
};
document.onmousedown = function(e) {
if (e.button == 0 && !lineStart) {
for (var i = 0; i < scene.lines.length; i++) {
if (intersects(e.pageX, e.pageY, scene.lines[i], thickness)) {
droppedPoint['x'] = e.pageX;
droppedPoint['y'] = e.pageY;
lineMove = scene.lines[i];
lineMove.color = '#B0171F';
return;
}
}
}
}
document.ondblclick = function(e) {
lineStart = false;
lineMove = null;
dead = [];
for (var i = 0; i < scene.lines.length; i++) {
if (intersects(e.pageX, e.pageY, scene.lines[i], thickness)) {
dead[dead.length] = i;
}
}
for (var i = dead.length-1; i >= 0; i--) {
scene.lines.splice(dead[i], 1);
}
}
}
init();