Spaces:
Running
Running
fix
Browse files- app.py +2 -2
- js/interactive_grid.js +163 -77
app.py
CHANGED
@@ -316,14 +316,14 @@ with gr.Blocks() as demo:
|
|
316 |
fn=None,
|
317 |
inputs=[image_bef],
|
318 |
outputs=[],
|
319 |
-
_js="(image) => { initializeEditorBefore();
|
320 |
)
|
321 |
|
322 |
image_aft.change(
|
323 |
fn=None,
|
324 |
inputs=[image_aft],
|
325 |
outputs=[],
|
326 |
-
_js="(image) => { initializeEditorAfter();
|
327 |
)
|
328 |
|
329 |
|
|
|
316 |
fn=None,
|
317 |
inputs=[image_bef],
|
318 |
outputs=[],
|
319 |
+
_js="(image) => { initializeEditorBefore(); importBackgroundBefore(image); return []; }",
|
320 |
)
|
321 |
|
322 |
image_aft.change(
|
323 |
fn=None,
|
324 |
inputs=[image_aft],
|
325 |
outputs=[],
|
326 |
+
_js="(image) => { initializeEditorAfter(); importBackgroundAfter(image); return []; }",
|
327 |
)
|
328 |
|
329 |
|
js/interactive_grid.js
CHANGED
@@ -1,31 +1,58 @@
|
|
1 |
const gridSize = 14;
|
2 |
var cellSize = null;
|
3 |
var inputImage = null;
|
4 |
-
var
|
5 |
-
var
|
6 |
-
var
|
7 |
-
var
|
8 |
-
|
9 |
-
var
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
|
11 |
-
let
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
-
function
|
14 |
console.log('createGrid')
|
15 |
|
16 |
for (let i = 0; i < 196; i++) {
|
17 |
const div = document.createElement('div');
|
18 |
div.classList.add('checkbox');
|
19 |
div.innerHTML = '<input type="checkbox">';
|
20 |
-
|
21 |
}
|
22 |
}
|
23 |
|
24 |
-
function
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
const file = event.target.files[0];
|
26 |
const reader = new FileReader();
|
27 |
reader.onload = function (e) {
|
28 |
-
|
29 |
}
|
30 |
reader.readAsDataURL(file);
|
31 |
}
|
@@ -48,127 +75,170 @@ function handleMouseLeave(event) {
|
|
48 |
}
|
49 |
|
50 |
|
51 |
-
function
|
52 |
-
|
53 |
-
|
54 |
for (let row = 0; row < gridSize; row++) {
|
55 |
for (let col = 0; col < gridSize; col++) {
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
|
62 |
if (grid[row][col]) {
|
63 |
-
|
64 |
-
|
65 |
}
|
66 |
}
|
67 |
}
|
68 |
}
|
69 |
|
70 |
-
|
71 |
function initializeEditorBefore() {
|
72 |
console.log("initializeEditorBefore");
|
73 |
|
74 |
-
if (
|
75 |
return;
|
76 |
}
|
77 |
-
|
78 |
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
|
83 |
// Add click event listener to canvas
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
|
89 |
cellSize = canvas.width / gridSize;
|
90 |
|
91 |
-
|
92 |
-
const rect =
|
93 |
-
const scaleX =
|
94 |
-
const scaleY =
|
95 |
const x = (event.clientX - rect.left) * scaleX;
|
96 |
const y = (event.clientY - rect.top) * scaleY;
|
97 |
const row = Math.floor(y / cellSize);
|
98 |
const col = Math.floor(x / cellSize);
|
99 |
|
100 |
// If the cell is already selected, it's always allowed to deselect it
|
101 |
-
if (
|
102 |
-
|
103 |
-
|
104 |
} else {
|
105 |
// Only select a new cell if less than 50 cells are already selected
|
106 |
-
if (
|
107 |
-
|
108 |
-
|
109 |
}
|
110 |
}
|
111 |
-
|
112 |
});
|
113 |
|
114 |
-
|
115 |
}
|
116 |
|
117 |
|
118 |
function initializeEditorAfter() {
|
119 |
console.log("initializeEditorAfter");
|
120 |
|
121 |
-
if (
|
122 |
return;
|
123 |
}
|
124 |
-
|
125 |
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
|
130 |
// Add click event listener to canvas
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
|
136 |
-
cellSize =
|
137 |
|
138 |
-
|
139 |
-
const rect =
|
140 |
-
const scaleX =
|
141 |
-
const scaleY =
|
142 |
const x = (event.clientX - rect.left) * scaleX;
|
143 |
const y = (event.clientY - rect.top) * scaleY;
|
144 |
const row = Math.floor(y / cellSize);
|
145 |
const col = Math.floor(x / cellSize);
|
146 |
|
147 |
// If the cell is already selected, it's always allowed to deselect it
|
148 |
-
if (
|
149 |
-
|
150 |
-
|
151 |
} else {
|
152 |
// Only select a new cell if less than 50 cells are already selected
|
153 |
-
if (
|
154 |
-
|
155 |
-
|
156 |
}
|
157 |
}
|
158 |
-
|
159 |
});
|
160 |
|
161 |
-
|
162 |
}
|
163 |
|
164 |
|
165 |
-
function
|
166 |
-
if (
|
167 |
-
const canvasWidth =
|
168 |
-
const canvasHeight =
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
169 |
|
170 |
-
const bgWidth =
|
171 |
-
const bgHeight =
|
172 |
|
173 |
const scaleX = canvasWidth / bgWidth;
|
174 |
const scaleY = canvasHeight / bgHeight;
|
@@ -181,22 +251,38 @@ function drawBackground() {
|
|
181 |
const xOffset = (canvasWidth - newWidth) / 2;
|
182 |
const yOffset = (canvasHeight - newHeight) / 2;
|
183 |
|
184 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
185 |
}
|
186 |
}
|
187 |
|
188 |
-
|
|
|
189 |
if (image == null) {
|
190 |
-
|
191 |
-
|
192 |
return;
|
193 |
}
|
194 |
|
195 |
let m = new Image();
|
196 |
m.src = image;
|
197 |
m.onload = function () {
|
198 |
-
|
199 |
-
|
200 |
}
|
201 |
}
|
202 |
|
|
|
1 |
const gridSize = 14;
|
2 |
var cellSize = null;
|
3 |
var inputImage = null;
|
4 |
+
var image_bef = null;
|
5 |
+
var image_aft = null;
|
6 |
+
var canvas_before = null;
|
7 |
+
var canvas_after = null;
|
8 |
+
var ctx_bef = null;
|
9 |
+
var ctx_aft = null;
|
10 |
+
var canvasBg_bef = null;
|
11 |
+
var canvasBg_aft = null;
|
12 |
+
let grid_bef = new Array(gridSize).fill(null).map(() => new Array(gridSize).fill(false));
|
13 |
+
let grid_aft = new Array(gridSize).fill(null).map(() => new Array(gridSize).fill(false));
|
14 |
+
var isInitialized_bef = false;
|
15 |
+
var isInitialized_aft = false;
|
16 |
+
|
17 |
+
let selectedCells_bef = 0;
|
18 |
+
let selectedCells_aft = 0;
|
19 |
+
|
20 |
+
function createGridBefore() {
|
21 |
+
console.log('createGrid')
|
22 |
|
23 |
+
for (let i = 0; i < 196; i++) {
|
24 |
+
const div = document.createElement('div');
|
25 |
+
div.classList.add('checkbox');
|
26 |
+
div.innerHTML = '<input type="checkbox">';
|
27 |
+
grid_bef.appendChild(div);
|
28 |
+
}
|
29 |
+
}
|
30 |
|
31 |
+
function createGridAfter() {
|
32 |
console.log('createGrid')
|
33 |
|
34 |
for (let i = 0; i < 196; i++) {
|
35 |
const div = document.createElement('div');
|
36 |
div.classList.add('checkbox');
|
37 |
div.innerHTML = '<input type="checkbox">';
|
38 |
+
grid_aft.appendChild(div);
|
39 |
}
|
40 |
}
|
41 |
|
42 |
+
function loadImageBefore(event) {
|
43 |
+
const file = event.target.files[0];
|
44 |
+
const reader = new FileReader();
|
45 |
+
reader.onload = function (e) {
|
46 |
+
image_bef.src = e.target.result;
|
47 |
+
}
|
48 |
+
reader.readAsDataURL(file);
|
49 |
+
}
|
50 |
+
|
51 |
+
function loadImageAfter(event) {
|
52 |
const file = event.target.files[0];
|
53 |
const reader = new FileReader();
|
54 |
reader.onload = function (e) {
|
55 |
+
image_aft.src = e.target.result;
|
56 |
}
|
57 |
reader.readAsDataURL(file);
|
58 |
}
|
|
|
75 |
}
|
76 |
|
77 |
|
78 |
+
function drawGridBefore() {
|
79 |
+
ctx_bef.clearRect(0, 0, canvas_before.width, canvas_before.height);
|
80 |
+
drawBackgroundBefore();
|
81 |
for (let row = 0; row < gridSize; row++) {
|
82 |
for (let col = 0; col < gridSize; col++) {
|
83 |
+
ctx_bef.beginPath();
|
84 |
+
ctx_bef.rect(col * cellSize, row * cellSize, cellSize, cellSize);
|
85 |
+
ctx_bef.strokeStyle = 'black';
|
86 |
+
ctx_bef.lineWidth = 2;
|
87 |
+
ctx_bef.stroke();
|
88 |
+
|
89 |
+
if (grid_bef[row][col]) {
|
90 |
+
ctx_bef.fillStyle = 'rgba(0, 255, 0, 0.5)';
|
91 |
+
ctx_bef.fillRect(col * cellSize, row * cellSize, cellSize, cellSize);
|
92 |
+
}
|
93 |
+
}
|
94 |
+
}
|
95 |
+
}
|
96 |
+
|
97 |
+
|
98 |
+
|
99 |
+
function drawGridAfter() {
|
100 |
+
ctx_aft.clearRect(0, 0, canvas_after.width, canvas_after.height);
|
101 |
+
drawBackgroundAfter();
|
102 |
+
for (let row = 0; row < gridSize; row++) {
|
103 |
+
for (let col = 0; col < gridSize; col++) {
|
104 |
+
ctx_aft.beginPath();
|
105 |
+
ctx_aft.rect(col * cellSize, row * cellSize, cellSize, cellSize);
|
106 |
+
ctx_aft.strokeStyle = 'black';
|
107 |
+
ctx_aft.lineWidth = 2;
|
108 |
+
ctx_aft.stroke();
|
109 |
|
110 |
if (grid[row][col]) {
|
111 |
+
ctx_aft.fillStyle = 'rgba(0, 255, 0, 0.5)';
|
112 |
+
ctx_aft.fillRect(col * cellSize, row * cellSize, cellSize, cellSize);
|
113 |
}
|
114 |
}
|
115 |
}
|
116 |
}
|
117 |
|
|
|
118 |
function initializeEditorBefore() {
|
119 |
console.log("initializeEditorBefore");
|
120 |
|
121 |
+
if (isInitialized_bef) {
|
122 |
return;
|
123 |
}
|
124 |
+
isInitialized_bef = true;
|
125 |
|
126 |
+
image_bef = document.getElementById('image-before');
|
127 |
+
canvas_before = document.getElementById('before');
|
128 |
+
ctx_bef = canvas.getContext('2d');
|
129 |
|
130 |
// Add click event listener to canvas
|
131 |
+
canvas_before.addEventListener('mousedown', handleMouseDown);
|
132 |
+
canvas_before.addEventListener('mousemove', handleMouseMove);
|
133 |
+
canvas_before.addEventListener('mouseup', handleMouseUp);
|
134 |
+
canvas_before.addEventListener('mouseleave', handleMouseLeave);
|
135 |
|
136 |
cellSize = canvas.width / gridSize;
|
137 |
|
138 |
+
canvas_before.addEventListener('click', (event) => {
|
139 |
+
const rect = canvas_before.getBoundingClientRect();
|
140 |
+
const scaleX = canvas_before.width / rect.width;
|
141 |
+
const scaleY = canvas_before.height / rect.height;
|
142 |
const x = (event.clientX - rect.left) * scaleX;
|
143 |
const y = (event.clientY - rect.top) * scaleY;
|
144 |
const row = Math.floor(y / cellSize);
|
145 |
const col = Math.floor(x / cellSize);
|
146 |
|
147 |
// If the cell is already selected, it's always allowed to deselect it
|
148 |
+
if (grid_bef[row][col]) {
|
149 |
+
grid_bef[row][col] = false;
|
150 |
+
selectedCells_bef--; // Decrement the selected cell count
|
151 |
} else {
|
152 |
// Only select a new cell if less than 50 cells are already selected
|
153 |
+
if (selectedCells_bef < 50) {
|
154 |
+
grid_bef[row][col] = true;
|
155 |
+
selectedCells_bef++; // Increment the selected cell count
|
156 |
}
|
157 |
}
|
158 |
+
drawGridBefore();
|
159 |
});
|
160 |
|
161 |
+
drawGridBefore();
|
162 |
}
|
163 |
|
164 |
|
165 |
function initializeEditorAfter() {
|
166 |
console.log("initializeEditorAfter");
|
167 |
|
168 |
+
if (isInitialized_aft) {
|
169 |
return;
|
170 |
}
|
171 |
+
isInitialized_aft = true;
|
172 |
|
173 |
+
image_aft = document.getElementById('image-after');
|
174 |
+
canvas_after = document.getElementById('after');
|
175 |
+
ctx_aft = canvas_after.getContext('2d');
|
176 |
|
177 |
// Add click event listener to canvas
|
178 |
+
canvas_after.addEventListener('mousedown', handleMouseDown);
|
179 |
+
canvas_after.addEventListener('mousemove', handleMouseMove);
|
180 |
+
canvas_after.addEventListener('mouseup', handleMouseUp);
|
181 |
+
canvas_after.addEventListener('mouseleave', handleMouseLeave);
|
182 |
|
183 |
+
cellSize = canvas_after.width / gridSize;
|
184 |
|
185 |
+
canvas_after.addEventListener('click', (event) => {
|
186 |
+
const rect = canvas_after.getBoundingClientRect();
|
187 |
+
const scaleX = canvas_after.width / rect.width;
|
188 |
+
const scaleY = canvas_after.height / rect.height;
|
189 |
const x = (event.clientX - rect.left) * scaleX;
|
190 |
const y = (event.clientY - rect.top) * scaleY;
|
191 |
const row = Math.floor(y / cellSize);
|
192 |
const col = Math.floor(x / cellSize);
|
193 |
|
194 |
// If the cell is already selected, it's always allowed to deselect it
|
195 |
+
if (grid_aft[row][col]) {
|
196 |
+
grid_aft[row][col] = false;
|
197 |
+
selectedCells_aft--; // Decrement the selected cell count
|
198 |
} else {
|
199 |
// Only select a new cell if less than 50 cells are already selected
|
200 |
+
if (selectedCells_aft < 50) {
|
201 |
+
grid_aft[row][col] = true;
|
202 |
+
selectedCells_aft++; // Increment the selected cell count
|
203 |
}
|
204 |
}
|
205 |
+
drawGridAfter();
|
206 |
});
|
207 |
|
208 |
+
drawGridAfter();
|
209 |
}
|
210 |
|
211 |
|
212 |
+
function drawBackgroundBefore() {
|
213 |
+
if (canvasBg_bef != null) {
|
214 |
+
const canvasWidth = canvas_before.width;
|
215 |
+
const canvasHeight = canvas_before.height;
|
216 |
+
|
217 |
+
const bgWidth = canvasBg_bef.width;
|
218 |
+
const bgHeight = canvasBg_bef.height;
|
219 |
+
|
220 |
+
const scaleX = canvasWidth / bgWidth;
|
221 |
+
const scaleY = canvasHeight / bgHeight;
|
222 |
+
|
223 |
+
const scale = Math.max(scaleX, scaleY);
|
224 |
+
|
225 |
+
const newWidth = bgWidth * scale;
|
226 |
+
const newHeight = bgHeight * scale;
|
227 |
+
|
228 |
+
const xOffset = (canvasWidth - newWidth) / 2;
|
229 |
+
const yOffset = (canvasHeight - newHeight) / 2;
|
230 |
+
|
231 |
+
ctx_bef.drawImage(canvasBg_bef, 0, 0, bgWidth, bgHeight, xOffset, yOffset, newWidth, newHeight);
|
232 |
+
}
|
233 |
+
}
|
234 |
+
|
235 |
+
function drawBackgroundAfter() {
|
236 |
+
if (canvasBg_aft != null) {
|
237 |
+
const canvasWidth = canvas_after.width;
|
238 |
+
const canvasHeight = canvas_after.height;
|
239 |
|
240 |
+
const bgWidth = canvasBg_aft.width;
|
241 |
+
const bgHeight = canvasBg_aft.height;
|
242 |
|
243 |
const scaleX = canvasWidth / bgWidth;
|
244 |
const scaleY = canvasHeight / bgHeight;
|
|
|
251 |
const xOffset = (canvasWidth - newWidth) / 2;
|
252 |
const yOffset = (canvasHeight - newHeight) / 2;
|
253 |
|
254 |
+
ctx_aft.drawImage(canvasBg_aft, 0, 0, bgWidth, bgHeight, xOffset, yOffset, newWidth, newHeight);
|
255 |
+
}
|
256 |
+
}
|
257 |
+
|
258 |
+
function importBackgroundBefore(image) {
|
259 |
+
if (image == null) {
|
260 |
+
canvasBg_bef = null;
|
261 |
+
drawGridBefore();
|
262 |
+
return;
|
263 |
+
}
|
264 |
+
|
265 |
+
let m = new Image();
|
266 |
+
m.src = image;
|
267 |
+
m.onload = function () {
|
268 |
+
canvasBg_bef = m;
|
269 |
+
drawGridBefore();
|
270 |
}
|
271 |
}
|
272 |
|
273 |
+
|
274 |
+
function importBackgroundAfter(image) {
|
275 |
if (image == null) {
|
276 |
+
canvasBg_aft = null;
|
277 |
+
drawGridAfter();
|
278 |
return;
|
279 |
}
|
280 |
|
281 |
let m = new Image();
|
282 |
m.src = image;
|
283 |
m.onload = function () {
|
284 |
+
canvasBg_aft = m;
|
285 |
+
drawGridAfter();
|
286 |
}
|
287 |
}
|
288 |
|