Skip to content

Commit c70d0eb

Browse files
authored
Merge pull request #16 from brunopicinin/fix-image-sizing
Fix image sizing after drop. Also improve zoom behavior.
2 parents 062f948 + 8acbb09 commit c70d0eb

File tree

3 files changed

+67
-17
lines changed

3 files changed

+67
-17
lines changed

index.html

+4-2
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
<li><a href="https://github.com/roboflow/polygonzone" target="_blank">View Source Code</a></li>
3636
</ul>
3737
</nav>
38-
<div class="flex">
38+
<div class="content">
3939
<div class="left">
4040
<div class="how-to section">
4141
<h1>PolygonZone</h1>
@@ -139,7 +139,9 @@ <h2>JSON Points</h2>
139139
</div>
140140
</div>
141141

142-
<canvas id="canvas"></canvas>
142+
<div class="image-container">
143+
<canvas id="canvas"></canvas>
144+
</div>
143145
</div>
144146
</div>
145147
</main>

script.js

+45-9
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ var color_choices = [
1515

1616
var radiansPer45Degrees = Math.PI / 4;
1717

18+
var imageContainer = document.querySelector('.image-container');
1819
var canvas = document.getElementById('canvas');
1920
var mainCtx = canvas.getContext('2d');
2021
var offScreenCanvas = document.createElement('canvas');
@@ -82,16 +83,30 @@ function isClockwise(vertices) {
8283
}
8384

8485
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;
8899
}
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';
95110
}
96111

97112
function onPathClose() {
@@ -356,6 +371,27 @@ canvas.addEventListener('drop', function(e) {
356371
canvas.height = img.height;
357372
offScreenCanvas.width = img.width;
358373
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';
359395
canvas.style.borderRadius = '10px';
360396
offScreenCtx.drawImage(img, 0, 0);
361397
blitCachedCanvas();

styles.css

+18-6
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,16 @@ pre {
1818
}
1919
body {
2020
background: white;
21-
min-height: 50vh;
21+
height: 100vh;
22+
margin: 0;
23+
padding: 0;
24+
}
25+
main {
26+
display: flex;
27+
flex-direction: column;
28+
height: 100vh;
29+
margin: 0;
30+
padding: 0;
2231
}
2332
h1 {
2433
font-size: 28px;
@@ -31,9 +40,6 @@ details {
3140
margin: 0;
3241
text-align: left;
3342
}
34-
.right {
35-
min-height: calc(100vh - 10em);
36-
}
3743
nav {
3844
border-bottom: 1px solid lightgrey;
3945
display: flex;
@@ -59,8 +65,9 @@ a {
5965
color: #5400ec;
6066
text-decoration: none;
6167
}
62-
.flex {
68+
.content {
6369
display: flex;
70+
flex-grow: 1;
6471
}
6572
.left {
6673
flex: 40%;
@@ -72,9 +79,14 @@ a {
7279
flex: 60%;
7380
padding: 0 2em;
7481
box-sizing: border-box;
82+
display: flex;
83+
flex-direction: column;
84+
}
85+
.image-container {
86+
flex-grow: 1;
7587
}
7688
@media screen and (max-width: 900px) {
77-
.flex {
89+
.content {
7890
display: block;
7991
}
8092
.left, .right {

0 commit comments

Comments
 (0)