-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
177 lines (154 loc) · 4.12 KB
/
index.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
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="lib/paper/dist/paper-full-modified.js"></script>
<script type="text/javascript">
function pos_to_hs(x, y){
var saturation = Math.sqrt(Math.pow(x, 2)+Math.pow(y, 2));
var hue = Math.atan2(x, y)/(2*Math.PI);
return {
hue: hue,
saturation: saturation,
}
}
function draw_hue_circle(center, radius, ctx){
var s= 0;
var color;
for(var i=-radius; i<=radius; i++){
for(var j=-radius; j<=radius; j++){
var location = {x: center.x+i, y:center.y+j}
var hs = pos_to_hs(i/radius, j/radius)
if(hs.saturation<=1){
ctx.fillStyle = "hsl(" + hs.hue*360 + ", " + hs.saturation*100 + "%, 50%)";
}else{
ctx.fillStyle = "white";
}
ctx.fillRect(location.x, location.y, 1, 1)
s++;
}
}
}
function getGamutPos(x_norm, y_norm){
var point = new Point((width*(x_norm+1))/2, (width*(y_norm+1))/2);
if(mask.contains(point)){
return {x:x_norm, y:y_norm};
}else{
var nearest = mask.getNearestPoint(point);
return {x:2*nearest.x/width -1, y:2*nearest.y/width -1};
}
}
function normalize(value){
return value/(width/2)-1;
}
function denormalize(value){
return (value+1)*(width/2);
}
paper.install(window);
window.onload = function() {
var canvas = document.getElementById("circle");
window.width = canvas.width;
var context = canvas.getContext("2d");
draw_hue_circle({x:width/2, y:width/2}, width/2, context);
Path.prototype.findNearestSegment = function(point){
var nearest = null;
var min_dist = Infinity;
for(var i in this.segments){
var dist = this.segments[i]._point.getDistance(point);
if(dist < min_dist){
nearest = this.segments[i]._point;
var min_dist = dist;
}
}
return nearest;
}
paper.setup('circle-overlay');
var tool = new Tool();
var mask = new Path();
window.mask = mask;
mask.strokeColor = "black";
mask.fillColor = new Color(1, 0, 0);
mask.strokeWidth = 5;
mask.selected = true;
mask.clockwise = false;
mask.add(new Point(60, 100));
mask.add(new Point(350, 100));
mask.add(new Point(200, 350));
mask.closePath();
mask.closed=true;
var chosen_segment = null;
tool.onMouseDown = function(event) {
var nearest = mask.findNearestSegment(event.point);
var distance = nearest.getDistance(event.point);
if(distance < 15){
chosen_segment = nearest;
}else{
mask.add(event.point);
chosen_segment = mask.findNearestSegment(event.point);
mask.clockwise = false;
if(mask.area < 0){
mask.reverse();
}
if(mask.segments.length==3){
mask.closePath();
}
}
}
tool.onMouseDrag = function(event) {
chosen_segment.x = event.point.x;
chosen_segment.y = event.point.y;
if(mask.area < 0){
mask.reverse();
}
}
tool.onMouseUp = function(event){
chosen_segment = null;
}
var dot = new Path.Circle(new Point(0,0), 3);
dot.fillColor = "red";
tool.onMouseMove = function(event){
var new_pos = getGamutPos(normalize(event.point.x), normalize(event.point.y));
dot.position = new Point(denormalize(new_pos.x), denormalize(new_pos.y));
dot.bringToFront();
}
var overlay = new CompoundPath({
children:[
new Path.Rectangle(new Point(0, 0), canvas.width),
mask,
],
fillColor: "black",
opacity: 0.5,
clockwise: false
})
view.draw();
/*
window.worker = new Worker("/worker3.js");
worker.onmessage = function(e) {
console.log(e);
console.log('Message received from worker');
}
*/
}
</script>
<style>
.circle-container{
position:absolute;
}
#circle-overlay{
position:absolute;
top:0;
left:0;
}
</style>
</head>
<body>
<div class="circle-container">
<canvas id="circle" width="400" height="400"></canvas>
<canvas id="circle-overlay" width="400" height="400"></canvas>
<button onClick="applyFilter()">Apply</button>
</div>
<div class="output-wrapper">
<canvas id="output-canvas" width="200" height="200" style="margin-left:400px;"></canvas>
</div>
</body>
<script src="main.js"></script>
</html>