@@ -4,7 +4,7 @@ window.shark = window.shark || {};
4
4
5
5
window . shark . info = window . shark . info || { } ;
6
6
window . shark . screenshots = window . shark . screenshots || { } ;
7
- window . shark . screenshot_counter = 0 ;
7
+ window . shark . info [ ' screenshot_counter' ] = 0 ;
8
8
9
9
function goToLoadingScreen ( ) {
10
10
document . querySelector ( '.file-upload-screen' ) . style . display = 'none' ;
@@ -243,8 +243,8 @@ function addScreenshot(screenshot) {
243
243
</div>
244
244
<div>
245
245
<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>
248
248
</span>
249
249
</div>
250
250
<div>
@@ -258,30 +258,29 @@ function addScreenshot(screenshot) {
258
258
}
259
259
260
260
// 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 ) {
263
262
let img = document . querySelector ( `.screenshot[data-id="${ screenshot_id } "] img` ) ;
264
263
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
+
265
277
let canvas = document . createElement ( 'canvas' ) ;
266
278
canvas . width = img . naturalWidth ;
267
279
canvas . height = img . naturalHeight ;
268
280
269
281
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 ) ;
285
284
img . src = canvas . toDataURL ( 'image/jpeg' ) ;
286
285
287
286
// Update image metadata.
@@ -357,8 +356,8 @@ function takeVideoScreenshot(query, xPercent, yPercent, widthPercent, heightPerc
357
356
let id = randomHex ( ) ;
358
357
359
358
// 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' ] ++ ;
362
361
363
362
let name = window . shark . info [ 'video' ] . name + "_" + index_string ;
364
363
@@ -399,9 +398,8 @@ function takeScreenshot(source, x, y, width, height, format = 'image/jpeg') {
399
398
400
399
function deleteScreenshot ( screenshot_id ) {
401
400
// 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
+
405
403
delete window . shark . screenshots [ screenshot_id ] ;
406
404
}
407
405
0 commit comments