-
Notifications
You must be signed in to change notification settings - Fork 0
/
testColorWheel.html
242 lines (190 loc) · 5.79 KB
/
testColorWheel.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
<html>
<head>
<script src="js/jquery-1.11.0.min.js"></script>
<style>
.marker {
position:absolute;
width:10px;
height:10px;
border-radius:10px;
border:2px solid black;
}
#canvas {
position:fixed;
top:0px;
left:0px;
}
</style>
<script type="text/javascript">
var markers = [];
var canvas;
var ctxt;
var width = 200;
var height = 200;
var startX = 100;
var startY = 100;
var markerRad = 10;
var selectedmark = -1;
var mdx;
var mdy;
function markerIndex(x, y) {
var index = -1;
for (var i = 0; i < markers.length; i++) {
var marker = markers[i];
var px = marker.x;
var py = marker.y;
var check = Math.pow(x - px, 2) + Math.pow(y - py, 2);
if (check < Math.pow(markerRad, 2)) {
index = i;
break;
}
}
return index;
}
function drawMarkers(){
ctxt.clearRect(0, 0, width, height);
ctxt.drawImage(colorwheel,startX, startY, width, height);
for(var i = 0; i < markers.length; i++) {
ctxt.beginPath();
var marker = markers[i];
ctxt.strokeStyle = "#FFFFFF";
if (selectedmark == i) {
ctxt.lineWidth = 5;
}
else {
ctxt.lineWidth = 3;
}
var px = marker.x;
var py = marker.y;
var imgData = ctxt.getImageData(px, py, 1, 1).data;
var r = imgData[0];
var g = imgData[1];
var b = imgData[2];
var a = imgData[3];
ctxt.fillStyle = "rgba( " + r + " , " + g + " , " + b + " , " + a + " )";
ctxt.arc(px, py, markerRad, 0, Math.PI * 2);
ctxt.stroke();
ctxt.fill();
}
}
$(document).ready(function(){
for (var i = 0; i < 3; i++) {
var x = startX + width/2 + 90 * Math.cos(-Math.PI/6 * i);
var y = startX + width/2 + 90 * Math.sin(-Math.PI/6 * i);
var next = {x:x, y:y};
markers[i] = next;
}
canvas = document.getElementById("canvas");
canvas.width = 500;
canvas.height = 500;
ctxt = canvas.getContext("2d");
ctxt.strokeStyle = "#000000";
$("#colorwheel").hide();
var colorwheel = document.getElementById("colorwheel");
colorwheel.onload = function() {
drawMarkers();
}
$("#canvas").mousedown(function(ev) {
var x = ev.pageX;
var y = ev.pageY;
selectedmark = markerIndex(x, y);
});
$("#canvas").mouseup(function(ev) {
selectedmark = -1;
drawMarkers();
});
$("#canvas").mousemove(function(ev) {
if (selectedmark != -1) {
var marker = markers[selectedmark];
var ux = ev.pageX;
var uy = ev.pageY;
var oldx = marker.x;
var oldy = marker.y;
var offsetx = startX + width/2;
var offsety = startY + width/2;
var radius = width/2 - markerRad;
var ax = ux - offsetx;
var ay = uy - offsety;
var currRadius = Math.sqrt(Math.pow(ax, 2) + Math.pow(ay, 2));
var boundscheck = Math.pow(ux - offsetx, 2) + Math.pow(uy - offsety,2);
if (boundscheck < Math.pow(width/2 - markerRad, 2)) {
marker.x = ux;
marker.y = uy;
}
else {
marker.x = offsetx + radius * ax/currRadius;
marker.y = offsety + radius * ay/currRadius;
}
switch(selectedmark) {
case -1:
return;
break;
// complementary color
case 0:
var complement = markers[2];
var da = Math.atan2(oldy - offsety, oldx - offsetx) -
Math.atan2(marker.y - offsety, marker.x - offsetx);
var cx = complement.x - offsetx;
var cy = complement.y - offsety;
var r = Math.sqrt(Math.pow(cx, 2) + Math.pow(cy, 2));
var angle = Math.atan2(cy, cx);
angle += da;
complement.x = offsetx + r * Math.cos(angle);
complement.y = offsety + r * Math.sin(angle);
break;
// base color
case 1:
var oldR = Math.sqrt(Math.pow(oldx - (startX + width/2), 2) +
Math.pow(oldy-(startY + width/2), 2));
var dr = Math.min(width/2, currRadius) - oldR;
var da = Math.atan2(oldy - offsety, oldx - offsetx) -
Math.atan2(marker.y - offsety, marker.x - offsetx);
var c1 = markers[0];
var c2 = markers[2];
var c1x = c1.x - (startX + width/2);
var c1y = c1.y - (startY + width/2);
var ang1 = Math.atan2(c1y, c1x);
ang1 -= da;
var c2x = c2.x - (startX + width/2);
var c2y = c2.y - (startY + width/2);
var ang2 = Math.atan2(c2y, c2x); ang2 -= da;
var r1 = Math.sqrt(Math.pow(c1x, 2) + Math.pow(c1y, 2));
var r2 = Math.sqrt(Math.pow(c2x, 2) + Math.pow(c2y, 2));
if (r1 + dr < (width/2 - markerRad)) {
r1 += dr;
}
if (r2 + dr < (width/2 - markerRad)) {
r2 += dr;
}
c1.x = offsetx + r1 * Math.cos(ang1);
c1.y = offsety + r1 * Math.sin(ang1);
c2.x = offsetx + r2 * Math.cos(ang2);
c2.y = offsety + r2 * Math.sin(ang2);
break;
// complementary color
case 2:
var complement = markers[0];
var da = Math.atan2(oldy - offsety, oldx - offsetx) -
Math.atan2(marker.y - offsety, marker.x - offsetx);
var cx = complement.x - offsetx;
var cy = complement.y - offsety;
var r = Math.sqrt(Math.pow(cx, 2) + Math.pow(cy, 2));
var angle = Math.atan2(cy, cx);
angle += da;
complement.x = offsetx + r * Math.cos(angle);
complement.y = offsety + r * Math.sin(angle);
break;
default:
break;
}
}
drawMarkers();
});
});
</script>
</head>
<body>
<canvas id = "canvas" ondragover="allowDrop(event)" ondrop="onDrop(event)"> </canvas>
<img src="images/colorwheel.png" id="colorwheel"></img>
</body>
</html>