Spaces:
Sleeping
Sleeping
fix
Browse files- js/interactive_grid.js +13 -50
js/interactive_grid.js
CHANGED
|
@@ -107,7 +107,7 @@ function drawGridBefore() {
|
|
| 107 |
|
| 108 |
if (grid_bef[row][col]) {
|
| 109 |
ctx_bef.fillStyle = 'rgba(0, 255, 0, 0.5)';
|
| 110 |
-
ctx_bef.fillRect(col * cellSizeX, row * cellSizeY, cellSizeX, cellSizeY);
|
| 111 |
}
|
| 112 |
}
|
| 113 |
}
|
|
@@ -119,27 +119,10 @@ function drawGridAfter() {
|
|
| 119 |
ctx_aft.clearRect(0, 0, canvas_after.width, canvas_after.height);
|
| 120 |
drawBackgroundAfter();
|
| 121 |
|
| 122 |
-
const canvasWidth = canvas_after.width;
|
| 123 |
-
const canvasHeight = canvas_after.height;
|
| 124 |
-
|
| 125 |
-
const bgWidth = canvasBg_aft.width;
|
| 126 |
-
const bgHeight = canvasBg_aft.height;
|
| 127 |
-
|
| 128 |
-
const scaleX = canvasWidth / bgWidth;
|
| 129 |
-
const scaleY = canvasHeight / bgHeight;
|
| 130 |
-
|
| 131 |
-
const scale = Math.min(scaleX, scaleY);
|
| 132 |
-
|
| 133 |
-
const newWidth = bgWidth * scale;
|
| 134 |
-
const newHeight = bgHeight * scale;
|
| 135 |
-
|
| 136 |
-
const xOffset = (canvasWidth - newWidth) / 2;
|
| 137 |
-
const yOffset = (canvasHeight - newHeight) / 2;
|
| 138 |
-
|
| 139 |
for (let row = 0; row < gridSize; row++) {
|
| 140 |
for (let col = 0; col < gridSize; col++) {
|
| 141 |
ctx_aft.beginPath();
|
| 142 |
-
ctx_aft.rect(
|
| 143 |
ctx_aft.strokeStyle = 'black';
|
| 144 |
ctx_aft.lineWidth = 1;
|
| 145 |
ctx_aft.stroke();
|
|
@@ -200,8 +183,8 @@ function initializeEditorBefore() {
|
|
| 200 |
// const scaleY = canvas_before.height / rect.height;
|
| 201 |
// const x = (event.clientX - rect.left) * scaleX;
|
| 202 |
// const y = (event.clientY - rect.top) * scaleY;
|
| 203 |
-
const x = (event.clientX - xOffset) *
|
| 204 |
-
const y = (event.clientY - yOffset) *
|
| 205 |
const row = Math.floor(y / cellSizeY);
|
| 206 |
const col = Math.floor(x / cellSizeX);
|
| 207 |
|
|
@@ -241,37 +224,15 @@ function initializeEditorAfter() {
|
|
| 241 |
canvas_after.addEventListener('mouseup', handleMouseUp);
|
| 242 |
canvas_after.addEventListener('mouseleave', handleMouseLeave);
|
| 243 |
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
const bgWidth = canvasBg_aft.width;
|
| 248 |
-
const bgHeight = canvasBg_aft.height;
|
| 249 |
-
|
| 250 |
-
const scaleX = canvasWidth / bgWidth;
|
| 251 |
-
const scaleY = canvasHeight / bgHeight;
|
| 252 |
-
|
| 253 |
-
const scale = Math.min(scaleX, scaleY);
|
| 254 |
-
|
| 255 |
-
const newWidth = bgWidth * scale;
|
| 256 |
-
const newHeight = bgHeight * scale;
|
| 257 |
-
|
| 258 |
-
const xOffset = (canvasWidth - newWidth) / 2;
|
| 259 |
-
const yOffset = (canvasHeight - newHeight) / 2;
|
| 260 |
-
|
| 261 |
-
// cellSizeX = canvas_after.width / gridSize;
|
| 262 |
-
// cellSizeY = canvas_after.height / gridSize;
|
| 263 |
-
|
| 264 |
-
cellSizeX = newWidth / gridSize;
|
| 265 |
-
cellSizeY = newHeight / gridSize;
|
| 266 |
|
| 267 |
canvas_after.addEventListener('click', (event) => {
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
const x = (event.clientX - xOffset) * scaleX;
|
| 274 |
-
const y = (event.clientY - yOffset) * scaleY;
|
| 275 |
const row = Math.floor(y / cellSizeY);
|
| 276 |
const col = Math.floor(x / cellSizeX);
|
| 277 |
|
|
@@ -365,6 +326,8 @@ function importBackgroundAfter(image_after) {
|
|
| 365 |
let m = new Image();
|
| 366 |
m.src = image_after;
|
| 367 |
m.onload = function () {
|
|
|
|
|
|
|
| 368 |
canvasBg_aft = m;
|
| 369 |
drawGridAfter();
|
| 370 |
}
|
|
|
|
| 107 |
|
| 108 |
if (grid_bef[row][col]) {
|
| 109 |
ctx_bef.fillStyle = 'rgba(0, 255, 0, 0.5)';
|
| 110 |
+
ctx_bef.fillRect(xOffset + col * cellSizeX, yOffset + row * cellSizeY, cellSizeX, cellSizeY);
|
| 111 |
}
|
| 112 |
}
|
| 113 |
}
|
|
|
|
| 119 |
ctx_aft.clearRect(0, 0, canvas_after.width, canvas_after.height);
|
| 120 |
drawBackgroundAfter();
|
| 121 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
for (let row = 0; row < gridSize; row++) {
|
| 123 |
for (let col = 0; col < gridSize; col++) {
|
| 124 |
ctx_aft.beginPath();
|
| 125 |
+
ctx_aft.rect(col * cellSizeX, row * cellSizeY, cellSizeX, cellSizeY);
|
| 126 |
ctx_aft.strokeStyle = 'black';
|
| 127 |
ctx_aft.lineWidth = 1;
|
| 128 |
ctx_aft.stroke();
|
|
|
|
| 183 |
// const scaleY = canvas_before.height / rect.height;
|
| 184 |
// const x = (event.clientX - rect.left) * scaleX;
|
| 185 |
// const y = (event.clientY - rect.top) * scaleY;
|
| 186 |
+
const x = (event.clientX - xOffset) * scale;
|
| 187 |
+
const y = (event.clientY - yOffset) * scale;
|
| 188 |
const row = Math.floor(y / cellSizeY);
|
| 189 |
const col = Math.floor(x / cellSizeX);
|
| 190 |
|
|
|
|
| 224 |
canvas_after.addEventListener('mouseup', handleMouseUp);
|
| 225 |
canvas_after.addEventListener('mouseleave', handleMouseLeave);
|
| 226 |
|
| 227 |
+
cellSizeX = canvas_after.width / gridSize;
|
| 228 |
+
cellSizeY = canvas_after.height / gridSize;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 229 |
|
| 230 |
canvas_after.addEventListener('click', (event) => {
|
| 231 |
+
const rect = canvas_after.getBoundingClientRect();
|
| 232 |
+
const scaleX = canvas_after.width / rect.width;
|
| 233 |
+
const scaleY = canvas_after.height / rect.height;
|
| 234 |
+
const x = (event.clientX - rect.left) * scaleX;
|
| 235 |
+
const y = (event.clientY - rect.top) * scaleY;
|
|
|
|
|
|
|
| 236 |
const row = Math.floor(y / cellSizeY);
|
| 237 |
const col = Math.floor(x / cellSizeX);
|
| 238 |
|
|
|
|
| 326 |
let m = new Image();
|
| 327 |
m.src = image_after;
|
| 328 |
m.onload = function () {
|
| 329 |
+
canvas_after.width = m.width;
|
| 330 |
+
canvas_after.height = m.height;
|
| 331 |
canvasBg_aft = m;
|
| 332 |
drawGridAfter();
|
| 333 |
}
|