@@ -15,6 +15,7 @@ var color_choices = [
15
15
16
16
var radiansPer45Degrees = Math . PI / 4 ;
17
17
18
+ var imageContainer = document . querySelector ( '.image-container' ) ;
18
19
var canvas = document . getElementById ( 'canvas' ) ;
19
20
var mainCtx = canvas . getContext ( '2d' ) ;
20
21
var offScreenCanvas = document . createElement ( 'canvas' ) ;
@@ -82,16 +83,30 @@ function isClockwise(vertices) {
82
83
}
83
84
84
85
function zoom ( clicks ) {
85
- // if w > 60em, stop
86
- if ( ( scaleFactor + clicks * scaleSpeed ) * img . width > 40 * 16 ) {
87
- return ;
86
+ var newScaleFactor = scaleFactor + clicks * scaleSpeed ;
87
+ newScaleFactor = Math . max ( 0.1 , Math . min ( newScaleFactor , 1 ) ) ;
88
+
89
+ const maxWidth = imageContainer . offsetWidth * 0.95 ;
90
+ const maxHeight = imageContainer . offsetHeight * 0.95 ;
91
+
92
+ let newWidth = img . width * newScaleFactor ;
93
+ let newHeight = img . height * newScaleFactor ;
94
+
95
+ if ( newWidth > maxWidth ) {
96
+ newHeight = ( maxWidth / newWidth ) * newHeight ;
97
+ newWidth = maxWidth ;
98
+ newScaleFactor = newWidth / img . width ;
88
99
}
89
- scaleFactor += clicks * scaleSpeed ;
90
- scaleFactor = Math . max ( 0.1 , Math . min ( scaleFactor , 0.8 ) ) ;
91
- var w = img . width * scaleFactor ;
92
- var h = img . height * scaleFactor ;
93
- canvas . style . width = w + 'px' ;
94
- canvas . style . height = h + 'px' ;
100
+
101
+ if ( newHeight > maxHeight ) {
102
+ newWidth = ( maxHeight / newHeight ) * newWidth ;
103
+ newHeight = maxHeight ;
104
+ newScaleFactor = newHeight / img . height ;
105
+ }
106
+
107
+ scaleFactor = newScaleFactor ;
108
+ canvas . style . width = newWidth + 'px' ;
109
+ canvas . style . height = newHeight + 'px' ;
95
110
}
96
111
97
112
function onPathClose ( ) {
@@ -356,6 +371,27 @@ canvas.addEventListener('drop', function(e) {
356
371
canvas . height = img . height ;
357
372
offScreenCanvas . width = img . width ;
358
373
offScreenCanvas . height = img . height ;
374
+
375
+ const maxWidth = imageContainer . offsetWidth * 0.95 ;
376
+ const maxHeight = imageContainer . offsetHeight * 0.95 ;
377
+
378
+ let newWidth = img . width ;
379
+ let newHeight = img . height ;
380
+
381
+ if ( newWidth > maxWidth ) {
382
+ newHeight = ( maxWidth / newWidth ) * newHeight ;
383
+ newWidth = maxWidth ;
384
+ }
385
+
386
+ if ( newHeight > maxHeight ) {
387
+ newWidth = ( maxHeight / newHeight ) * newWidth ;
388
+ newHeight = maxHeight ;
389
+ }
390
+
391
+ scaleFactor = newWidth / img . width ;
392
+
393
+ canvas . style . width = newWidth + 'px' ;
394
+ canvas . style . height = newHeight + 'px' ;
359
395
canvas . style . borderRadius = '10px' ;
360
396
offScreenCtx . drawImage ( img , 0 , 0 ) ;
361
397
blitCachedCanvas ( ) ;
0 commit comments