Skip to content

Commit c5d0833

Browse files
committed
Addressing further comments
1 parent 545ea88 commit c5d0833

File tree

1 file changed

+23
-25
lines changed

1 file changed

+23
-25
lines changed

static/js/main.js

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ window.shark = window.shark || {};
44

55
window.shark.info = window.shark.info || {};
66
window.shark.screenshots = window.shark.screenshots || {};
7-
window.shark.screenshot_counter = 0;
7+
window.shark.info['screenshot_counter'] = 0;
88

99
function goToLoadingScreen() {
1010
document.querySelector('.file-upload-screen').style.display = 'none';
@@ -243,8 +243,8 @@ function addScreenshot(screenshot) {
243243
</div>
244244
<div>
245245
<span>
246-
<button onclick='flipScreenshot("${screenshot.id}", "x")'>Vertical Flip</button>
247-
<button onclick='flipScreenshot("${screenshot.id}", "y")'>Horizontal Flip</button>
246+
<button onclick='flipScreenshot("${screenshot.id}", true)'>Vertical Flip</button>
247+
<button onclick='flipScreenshot("${screenshot.id}", false)'>Horizontal Flip</button>
248248
</span>
249249
</div>
250250
<div>
@@ -258,30 +258,29 @@ function addScreenshot(screenshot) {
258258
}
259259

260260
// Flip a screenshot by adding it the the canvas flipped horizontally or vertically.
261-
function flipScreenshot(screenshot_id, axis) {
262-
// Find correct image, add to canvas to flip.
261+
function flipScreenshot(screenshot_id, flip_vertical) {
263262
let img = document.querySelector(`.screenshot[data-id="${screenshot_id}"] img`);
264263

264+
// Default values for horizontal flip;
265+
let direction = 'flipped_horizontally';
266+
let scale_x = -1;
267+
let scale_y = 1;
268+
let draw_x = -img.naturalWidth;
269+
let draw_y = 0;
270+
271+
if (flip_vertical) {
272+
direction = 'flipped_vertically';
273+
scale_x = 1, scale_y = -1;
274+
draw_x = 0, draw_y = -img.naturalHeight;
275+
}
276+
265277
let canvas = document.createElement('canvas');
266278
canvas.width = img.naturalWidth;
267279
canvas.height = img.naturalHeight;
268280

269281
let context = canvas.getContext('2d');
270-
271-
let direction = ''
272-
if (axis === 'y') {
273-
direction = 'flipped_horizontally';
274-
context.scale(-1, 1);
275-
context.drawImage(img, -img.naturalWidth, 0);
276-
} else if (axis === 'x') {
277-
direction = 'flipped_vertically';
278-
context.scale(1, -1);
279-
context.drawImage(img, 0, -img.naturalHeight);
280-
} else {
281-
throw("Invalid flip axis.")
282-
}
283-
284-
// Draw the canvas to the image area.
282+
context.scale(scale_x, scale_y);
283+
context.drawImage(img, draw_x, draw_y);
285284
img.src = canvas.toDataURL('image/jpeg');
286285

287286
// Update image metadata.
@@ -357,8 +356,8 @@ function takeVideoScreenshot(query, xPercent, yPercent, widthPercent, heightPerc
357356
let id = randomHex();
358357

359358
// Get screenshot counter value and update counter.
360-
let index_string = String(window.shark.screenshot_counter).padStart(3, '0');
361-
window.shark.screenshot_counter ++;
359+
let index_string = String(window.shark.info['screenshot_counter']).padStart(3, '0');
360+
window.shark.info['screenshot_counter']++;
362361

363362
let name = window.shark.info['video'].name + "_" + index_string;
364363

@@ -399,9 +398,8 @@ function takeScreenshot(source, x, y, width, height, format = 'image/jpeg') {
399398

400399
function deleteScreenshot(screenshot_id) {
401400
// Find screenshot area to remove.
402-
let screenshot_area = document.querySelector(`.screenshot[data-id="${screenshot_id}"]`);
403-
screenshot_area.remove();
404-
401+
document.querySelector(`.screenshot[data-id="${screenshot_id}"]`).remove();
402+
405403
delete window.shark.screenshots[screenshot_id];
406404
}
407405

0 commit comments

Comments
 (0)