-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbubbles.html
139 lines (127 loc) · 3.5 KB
/
bubbles.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
<html>
<body>
<table border=1 width=1150>
<tr>
<td width=950>
<canvas id='draw' width=950 height=550
onmousedown = "stopDraw(); return false;"
style='border:1px solid'></canvas>
</td>
<td width=200>
<form name="set" onsubmit="return false;">
Fill: <br> <input type="radio" id="f1" name="fill" value="0">
<label for="f1">yes</label><br>
<input type="radio" id="f2"name="fill" value="1">
<label for="f2">no</label><br><br>
Color: <br> <input type="radio" id="Choice1" name="color" value="0">
<label for="Choice1">green</label><br>
<input type="radio" id="Choice2"name="color" value="1">
<label for="Choice2">pink</label><br>
<input type="radio" id="Choice3"name="color" value="2">
<label for="Choice3">random</label><br><br>
Radius: <br> <input type="text" name="t1" ><br>
CenterX: <br> <input type="text" name="t2" ><br>
CenterY: <br> <input type="text" name="t3" ><br>
<br><input type="button" value="Start" onclick="start();"><br>
</form>
</td>
</tr>
</table>
<script type='text/javascript'>
var canva = document.getElementById('draw');
var ctx = canva.getContext('2d');
ctx.strokeStyle = "rgb(170,10,170)";
var dx = 13, dy = 13, centerX = 963, centerY = 563, radius = 13, newRadius = 0;
ctx.beginPath();
function takeCoords(){
canva.addEventListener('mousemove', e => {
drawBubble(e.clientX, e.clientY)
}, {once: true});
}
function drawBubble(newX, newY){
if (flag===1){
s = "#"+((1<<24)*Math.random()|0).toString(16);
ctx.strokeStyle = s;
ctx.fillStyle = s;
}
//ctx.strokeStyle = "rgba(170,10,170,"+newX/(2*canva.width)+")";
g = Math.sqrt(Math.pow(newX-centerX,2) + Math.pow(newY-centerY, 2))
if (g > radius){
newRadius = (g - radius) / 2;
if (newX > centerX){
centerX = centerX + ((radius+newRadius)*(newX-centerX)/g);
}
else{
centerX = centerX - ((radius+newRadius)*(centerX-newX)/g);
}
if (newY > centerY){
centerY = centerY + ((radius+newRadius)*(newY-centerY)/g);
}
else{
centerY = centerY - ((radius+newRadius)*(centerY-newY)/g);
}
}
else{
newRadius = (radius - g) / 2;
if (newX > centerX){
centerX = centerX + ((radius-newRadius)*(newX-centerX)/g);
}
else{
centerX = centerX - ((radius-newRadius)*(centerX-newX)/g);
}
if (newY > centerY){
centerY = centerY + ((radius-newRadius)*(newY-centerY)/g);
}
else{
centerY = centerY - ((radius-newRadius)*(centerY-newY)/g);
}
}
ctx.arc(centerX, centerY, newRadius, 0, Math.PI*2);
if (document.set.fill.value === "1"){
ctx.stroke();
}
else{
ctx.stroke();
ctx.fill();
}
ctx.closePath();
ctx.beginPath();
radius = newRadius;
document.set.t1.value = radius;
document.set.t2.value = centerX;
document.set.t3.value = centerY;
}
function stopDraw(){
ctx.closePath();
clearInterval(timerT);
ctx.fillStyle = "white";
ctx.fillRect(0, 0, 950, 550);
}
let timerT = null;
var flag = 0;
function start(){
stopDraw();
flag = 0;
if (document.set.color.value === "0"){
ctx.strokeStyle = "green";
ctx.fillStyle = "green";
}
else{
if (document.set.color.value === "1"){
ctx.strokeStyle = "rgb(170,10,170)";
ctx.fillStyle = "rgb(170,10,170)";
}
else{
flag = 1;
}
}
ctx.beginPath();
centerX = 890;
centerY = 510;
radius = 15;
newRadius = 0;
timerT = setInterval(takeCoords, 100);
}
</script>
</body>
</html>