-
Notifications
You must be signed in to change notification settings - Fork 0
/
ui.html
144 lines (125 loc) · 3.53 KB
/
ui.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
<style>
</style>
<button id="process" class="controls" disabled>Process</button>
<button id="undo" class="controls" disabled>Undo</button>
<button id="clear" class="controls" disabled>Clear</button>
<img id="image" style="display: none;" />
<canvas id="canvas" />
<script>
let points = [];
let img = document.getElementById("image");
let canvas = document.getElementById("canvas");
let ctx = canvas.getContext("2d");
let controls = document.getElementsByClassName("controls");
let enabled = false;
const url = 'http://127.0.0.1:5000/v1.0/removebg'
const api_key = 'this_is_api_key'
const hdrs = {
//'X-Api-Key': api_key,
//'Accept': 'application/json'
}
function enableControls() {
for(let i = 0; i < controls.length; ++i) {
controls[i].disabled = false;
}
enabled = true;
}
function disableControls() {
for(let i = 0; i < controls.length; ++i) {
controls[i].disabled = true;
}
enabled = false;
}
function sendPoints() {
if (!enabled) {
return;
}
fetch(url, { method: 'POST', headers: hdrs, body: JSON.stringify({'points': points}) }).then(response => {
if(!response.ok) {
throw response
}
// TODO: apply mask from response
return response;
});
}
function _drawPoint(x, y, color) {
if (!enabled) {
return;
}
var circle = new Path2D();
circle.arc(x, y, 3, 0, 2 * Math.PI);
ctx.fillStyle = color;
ctx.fill(circle);
}
let typeToColor = (type) => type === 0 ? "red" : "blue";
function _addPoint(x_, y_, type_) {
if (!enabled) {
return;
}
points.push({x: x_, y: y_, type: type_});
_drawPoint(x_, y_, typeToColor(type_));
sendPoints();
}
function _removePoint() {
if (!enabled) {
return;
}
points.pop();
ctx.drawImage(img, 0, 0);
for (const i in points) {
let p = points[i];
_drawPoint(p.x, p.y, typeToColor(p.type));
}
sendPoints();
}
function _clearPoints() {
if (!enabled) {
return;
}
points = [];
ctx.drawImage(img, 0, 0);
// TODO: clear mask
}
document.getElementById('process').onclick = () => {
if (!enabled) {
return;
}
parent.postMessage({ pluginMessage: { type: 'process' } }, '*')
}
document.getElementById('undo').onclick = _removePoint;
document.getElementById('clear').onclick = _clearPoints;
canvas.onclick = (event) => {
_addPoint(event.offsetX, event.offsetY, 0);
}
canvas.oncontextmenu = (event) => {
_addPoint(event.offsetX, event.offsetY, 1);
}
// Create an event handler to receive messages from the main thread
window.onmessage = async (event) => {
img.onload = () => {
canvas.width = img.width;
canvas.height = img.height;
ctx.drawImage(img, 0, 0);
parent.postMessage({ pluginMessage: { type: 'resize', width: img.width, height: img.height } }, '*');
}
// Just get the bytes directly from the pluginMessage since that's
// the only type of message we'll receive in this plugin. In more
// complex plugins, you'll want to check the type of the message.
const bytes = event.data.pluginMessage;
//let data = new FormData();
let base64 = btoa(new Uint8Array(bytes).reduce((data, byte) => {
return data + String.fromCharCode(byte)
}, ''));
fetch(url, { method: 'POST', headers: hdrs, body: JSON.stringify({'image': base64}) }).then(response => {
if(!response.ok) {
throw response
}
//img.src = "data:image/png;base64," + base64;
//enableControls();
return response;
});
// TODO: enable controls only after response
img.src = "data:image/png;base64," + base64;
enableControls();
}
</script>