pooyanrg commited on
Commit
778a606
·
1 Parent(s): 9735689
Files changed (1) hide show
  1. js/interactive_grid.js +98 -25
js/interactive_grid.js CHANGED
@@ -79,12 +79,30 @@ function handleMouseLeave(event) {
79
  function drawGridBefore() {
80
  ctx_bef.clearRect(0, 0, canvas_before.width, canvas_before.height);
81
  drawBackgroundBefore();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
82
  for (let row = 0; row < gridSize; row++) {
83
  for (let col = 0; col < gridSize; col++) {
84
  ctx_bef.beginPath();
85
- ctx_bef.rect(col * cellSizeX, row * cellSizeY, cellSizeX, cellSizeY);
86
  ctx_bef.strokeStyle = 'black';
87
- ctx_bef.lineWidth = 0;
88
  ctx_bef.stroke();
89
 
90
  if (grid_bef[row][col]) {
@@ -100,12 +118,30 @@ function drawGridBefore() {
100
  function drawGridAfter() {
101
  ctx_aft.clearRect(0, 0, canvas_after.width, canvas_after.height);
102
  drawBackgroundAfter();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
103
  for (let row = 0; row < gridSize; row++) {
104
  for (let col = 0; col < gridSize; col++) {
105
  ctx_aft.beginPath();
106
- ctx_aft.rect(col * cellSizeX, row * cellSizeY, cellSizeX, cellSizeY);
107
  ctx_aft.strokeStyle = 'black';
108
- ctx_aft.lineWidth = 0;
109
  ctx_aft.stroke();
110
 
111
  if (grid_aft[row][col]) {
@@ -134,19 +170,38 @@ function initializeEditorBefore() {
134
  canvas_before.addEventListener('mouseup', handleMouseUp);
135
  canvas_before.addEventListener('mouseleave', handleMouseLeave);
136
 
137
- cellSizeX = canvas_before.width / gridSize;
138
- cellSizeY = canvas_before.height / gridSize;
139
 
140
- canvas_before.addEventListener('click', (event) => {
141
- const rect = canvas_before.getBoundingClientRect();
142
 
143
- const bgWidth = canvasBg_bef.width;
144
- const bgHeight = canvasBg_bef.height;
145
 
146
- const scaleX = canvas_before.width / bgWidth//rect.width;
147
- const scaleY = canvas_before.height / bgHeight//rect.height;
148
- const x = (event.clientX - rect.left) * scaleX;
149
- const y = (event.clientY - rect.top) * scaleY;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
150
  const row = Math.floor(y / cellSizeY);
151
  const col = Math.floor(x / cellSizeX);
152
 
@@ -186,19 +241,37 @@ function initializeEditorAfter() {
186
  canvas_after.addEventListener('mouseup', handleMouseUp);
187
  canvas_after.addEventListener('mouseleave', handleMouseLeave);
188
 
189
- cellSizeX = canvas_after.width / gridSize;
190
- cellSizeY = canvas_after.height / gridSize;
191
 
192
- canvas_after.addEventListener('click', (event) => {
193
- const rect = canvas_after.getBoundingClientRect();
194
-
195
- const bgWidth = canvasBg_aft.width;
196
- const bgHeight = canvasBg_aft.height;
 
 
 
 
 
197
 
198
- const scaleX = canvas_after.width / bgWidth//rect.width;
199
- const scaleY = canvas_after.height / bgHeight//rect.height;
200
- const x = (event.clientX - rect.left) * scaleX;
201
- const y = (event.clientY - rect.top) * scaleY;
 
 
 
 
 
 
 
 
 
 
 
 
 
202
  const row = Math.floor(y / cellSizeY);
203
  const col = Math.floor(x / cellSizeX);
204
 
 
79
  function drawGridBefore() {
80
  ctx_bef.clearRect(0, 0, canvas_before.width, canvas_before.height);
81
  drawBackgroundBefore();
82
+
83
+ const canvasWidth = canvas_before.width;
84
+ const canvasHeight = canvas_before.height;
85
+
86
+ const bgWidth = canvasBg_bef.width;
87
+ const bgHeight = canvasBg_bef.height;
88
+
89
+ const scaleX = canvasWidth / bgWidth;
90
+ const scaleY = canvasHeight / bgHeight;
91
+
92
+ const scale = Math.min(scaleX, scaleY);
93
+
94
+ const newWidth = bgWidth * scale;
95
+ const newHeight = bgHeight * scale;
96
+
97
+ const xOffset = (canvasWidth - newWidth) / 2;
98
+ const yOffset = (canvasHeight - newHeight) / 2;
99
+
100
  for (let row = 0; row < gridSize; row++) {
101
  for (let col = 0; col < gridSize; col++) {
102
  ctx_bef.beginPath();
103
+ ctx_bef.rect(xOffset + col * cellSizeX, yOffset + row * cellSizeY, cellSizeX, cellSizeY);
104
  ctx_bef.strokeStyle = 'black';
105
+ ctx_bef.lineWidth = 1;
106
  ctx_bef.stroke();
107
 
108
  if (grid_bef[row][col]) {
 
118
  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(xOffset + col * cellSizeX, yOffset + row * cellSizeY, cellSizeX, cellSizeY);
143
  ctx_aft.strokeStyle = 'black';
144
+ ctx_aft.lineWidth = 1;
145
  ctx_aft.stroke();
146
 
147
  if (grid_aft[row][col]) {
 
170
  canvas_before.addEventListener('mouseup', handleMouseUp);
171
  canvas_before.addEventListener('mouseleave', handleMouseLeave);
172
 
 
 
173
 
174
+ const canvasWidth = canvas_before.width;
175
+ const canvasHeight = canvas_before.height;
176
 
177
+ const bgWidth = canvasBg_bef.width;
178
+ const bgHeight = canvasBg_bef.height;
179
 
180
+ const scaleX = canvasWidth / bgWidth;
181
+ const scaleY = canvasHeight / bgHeight;
182
+
183
+ const scale = Math.min(scaleX, scaleY);
184
+
185
+ const newWidth = bgWidth * scale;
186
+ const newHeight = bgHeight * scale;
187
+
188
+ const xOffset = (canvasWidth - newWidth) / 2;
189
+ const yOffset = (canvasHeight - newHeight) / 2;
190
+
191
+ //cellSizeX = canvas_before.width / gridSize;
192
+ //cellSizeY = canvas_before.height / gridSize;
193
+
194
+ cellSizeX = newWidth / gridSize;
195
+ cellSizeY = newHeight / gridSize;
196
+
197
+ canvas_before.addEventListener('click', (event) => {
198
+ // const rect = canvas_before.getBoundingClientRect();
199
+ // const scaleX = canvas_before.width / rect.width;
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) * scaleX;
204
+ const y = (event.clientY - yOffset) * scaleY;
205
  const row = Math.floor(y / cellSizeY);
206
  const col = Math.floor(x / cellSizeX);
207
 
 
241
  canvas_after.addEventListener('mouseup', handleMouseUp);
242
  canvas_after.addEventListener('mouseleave', handleMouseLeave);
243
 
244
+ const canvasWidth = canvas_after.width;
245
+ const canvasHeight = canvas_after.height;
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
+ // const rect = canvas_after.getBoundingClientRect();
269
+ // const scaleX = canvas_after.width / rect.width;
270
+ // // const scaleY = canvas_after.height / rect.height;
271
+ // const x = (event.clientX - rect.left) * scaleX;
272
+ // const y = (event.clientY - rect.top) * scaleY;
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