Skip to content

Commit 5d76d83

Browse files
authored
Merge pull request #35 from UZ9/ryder/dynamic-canvas-scaling
Dynamic viewport scaling
2 parents 577644c + d2f4d7c commit 5d76d83

6 files changed

Lines changed: 60 additions & 103 deletions

File tree

src/anim/AnimationMain.js

Lines changed: 2 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -84,55 +84,6 @@ function setCookie(cookieName, value, expireDays) {
8484

8585
const ANIMATION_SPEED_DEFAULT = 75;
8686

87-
// TODO: Move these out of global space into animation manager?
88-
89-
function controlKey(keyASCII) {
90-
return (
91-
keyASCII === 8 ||
92-
keyASCII === 9 ||
93-
keyASCII === 37 ||
94-
keyASCII === 38 ||
95-
keyASCII === 39 ||
96-
keyASCII === 40 ||
97-
keyASCII === 46
98-
);
99-
}
100-
101-
function returnSubmit(field, func, maxSize, intOnly) {
102-
if (maxSize !== undefined) {
103-
field.size = maxSize;
104-
}
105-
return function (event) {
106-
let keyASCII = 0;
107-
if (window.event) {
108-
// IE
109-
keyASCII = event.keyCode;
110-
} else if (event.which) {
111-
// Netscape/Firefox/Opera
112-
keyASCII = event.which;
113-
}
114-
115-
if (keyASCII === 13) {
116-
func();
117-
return false;
118-
} else if (
119-
keyASCII === 59 ||
120-
keyASCII === 45 ||
121-
keyASCII === 46 ||
122-
keyASCII === 190 ||
123-
keyASCII === 173
124-
) {
125-
return false;
126-
} else if (
127-
(maxSize !== undefined && field.value.length >= maxSize) ||
128-
(intOnly && (keyASCII < 48 || keyASCII > 57))
129-
) {
130-
if (!controlKey(keyASCII)) return false;
131-
}
132-
return true;
133-
};
134-
}
135-
13687
function addControlToAnimationBar(animBarRef, type, name, callback) {
13788
const element = document.createElement('input');
13889

@@ -276,7 +227,7 @@ export default class AnimationManager extends EventListener {
276227
/>
277228
);
278229

279-
let tableEntry = document.createElement('td');
230+
const tableEntry = document.createElement('td');
280231

281232
const controlBar = document.getElementById('GeneralAnimationControls');
282233

@@ -295,7 +246,7 @@ export default class AnimationManager extends EventListener {
295246
midLevel = document.createElement('tr');
296247
bottomLevel = document.createElement('td');
297248
bottomLevel.align = 'center';
298-
let txtNode = document.createTextNode('Animation Speed');
249+
const txtNode = document.createTextNode('Animation Speed');
299250
midLevel.appendChild(bottomLevel);
300251
bottomLevel.classList.add('txt-node');
301252
bottomLevel.appendChild(txtNode);
@@ -312,45 +263,10 @@ export default class AnimationManager extends EventListener {
312263

313264
addDivisorToAnimationBar(animBarRef);
314265

315-
let width = getCookie('VisualizationWidth');
316-
width = width == null || width === '' ? 1500 : parseInt(width);
317-
318-
let height = getCookie('VisualizationHeight');
319-
height = height == null || height === '' ? 555 : parseInt(height);
320-
321-
canvas.width = width;
322-
canvas.height = height;
323-
324-
tableEntry = document.createElement('td');
325-
txtNode = document.createTextNode('Canvas height:');
326-
tableEntry.classList.add('txt-node');
327-
tableEntry.appendChild(txtNode);
328-
controlBar.appendChild(tableEntry);
329-
330-
this.heightEntry = addControlToAnimationBar(animBarRef, 'Text', canvas.height, () =>
331-
returnSubmit(
332-
this.heightEntry,
333-
() =>
334-
this.changeSize(
335-
document.documentElement.clientWidth,
336-
parseInt(this.heightEntry.value),
337-
),
338-
4,
339-
true,
340-
),
341-
);
342-
343-
this.heightEntry.size = 4;
344-
this.sizeButton = addControlToAnimationBar(animBarRef, 'Button', 'Change Canvas Size', () =>
345-
this.changeSize(),
346-
);
347-
348266
this.addListener('AnimationStarted', this, this.animStarted);
349267
this.addListener('AnimationEnded', this, this.animEnded);
350268
this.addListener('AnimationWaiting', this, this.animWaiting);
351269
this.addListener('AnimationUndoUnavailable', this, this.animUndoUnavailable);
352-
this.objectManager.width = canvas.width;
353-
this.objectManager.height = canvas.height;
354270
}
355271

356272
lerp(from, to, percent) {
@@ -397,14 +313,11 @@ export default class AnimationManager extends EventListener {
397313
if (width > 100) {
398314
canvas.width = width;
399315
this.animatedObjects.width = width;
400-
setCookie('VisualizationWidth', String(width), 30);
401316
}
402317
if (height > 100) {
403318
canvas.height = height;
404319
this.animatedObjects.height = height;
405-
setCookie('VisualizationHeight', String(height), 30);
406320
}
407-
this.heightEntry.value = canvas.height;
408321

409322
this.animatedObjects.draw();
410323
this.fireEvent('CanvasSizeChanged', { width: canvas.width, height: canvas.height });

src/css/AlgoScreen.css

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,22 @@
3434
}
3535

3636
.VisualizationMainPage #mainContent {
37-
/* padding: 0 20px; /* remember that padding is the space inside the div box and margin is the space outside the div box */
3837
background: #ffffff;
38+
width: 100vw;
3939
font-family: 'Roboto', Helvetica Neue, Helvetica, Arial, sans-serif;
40+
flex: 1;
41+
flex-direction: column;
42+
display: flex;
43+
overflow: hidden;
4044
}
4145

4246
.VisualizationMainPage #container {
4347
background: #ffffff;
4448
/* margin: 0 auto; /* the auto margins (in conjunction with a width) center the page */
4549
text-align: left; /* this overrides the text-align: center on the body element. */
50+
height: 100vh;
51+
display: flex;
52+
flex-direction: column;
4653
}
4754

4855
.VisualizationMainPage #toggle {
@@ -83,7 +90,10 @@
8390
}
8491

8592
.VisualizationMainPage .viewport {
93+
display: flex;
8694
position: relative;
95+
flex: 1;
96+
overflow: hidden;
8797
}
8898

8999
.VisualizationMainPage #generalAnimationControlSection {
@@ -126,12 +136,6 @@
126136
color: white;
127137
}
128138

129-
.VisualizationMainPage #container {
130-
background: #ffffff;
131-
margin: 0 auto; /* the auto margins (in conjunction with a width) center the page */
132-
text-align: left; /* this overrides the text-align: center on the body element. */
133-
}
134-
135139
.VisualizationMainPage #footer a:visited {
136140
text-decoration: none;
137141
color: white;
@@ -623,6 +627,10 @@ input[type='checkbox'] {
623627
flex-direction: column;
624628
gap: 8px;
625629
}
630+
631+
.VisualizationMainPage #mainContent {
632+
overflow-x: auto;
633+
}
626634
}
627635

628636
/* Pseudocode Icon */
@@ -1055,3 +1063,8 @@ input[type='checkbox'] {
10551063
color: white !important;
10561064
border: none !important;
10571065
}
1066+
1067+
#canvas {
1068+
width: 100%;
1069+
height: 100%;
1070+
}

src/css/App.css

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -676,11 +676,35 @@ input::placeholder {
676676

677677
.side-panel {
678678
width: 100%;
679+
position: static;
680+
padding: 0;
681+
border: none;
679682
}
680683

681684
.content a {
682685
width: 100%;
683686
}
687+
688+
.outer-flex {
689+
flex-direction: column;
690+
width: 100%;
691+
margin: 0;
692+
}
693+
694+
.inner-flex button.button {
695+
margin: 5% 0 0 0;
696+
width: 100%;
697+
border: none;
698+
}
699+
700+
.dsa-filter {
701+
border: none;
702+
}
703+
704+
#blob-container {
705+
position: static;
706+
padding-bottom: 1rem;
707+
}
684708
}
685709

686710
@media screen and (min-width: 1000px) {

src/modals/BigOModals.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ const ToggleBlurCell = ({ text, width, force }) => {
5858
<td
5959
style={{ width: width }}
6060
className={isBlurred ? 'blur big_o_cell' : 'big_o_cell'}
61-
onClick={() => isBlurred ? setIsBlurred(false) : setIsBlurred(true)}
61+
onClick={() => (isBlurred ? setIsBlurred(false) : setIsBlurred(true))}
6262
>
6363
{applyEquationClass(text, force)}
6464
</td>

src/screens/AlgoScreen.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,16 @@ const AlgoScreen = ({ theme, toggleTheme }) => {
7171
}
7272

7373
const updateDimensions = () => {
74-
animManagRef.current.changeSize(document.body.clientWidth);
74+
animManagRef.current.changeSize(
75+
canvasRef.current.clientWidth,
76+
canvasRef.current.clientHeight,
77+
);
7578
};
7679

7780
window.addEventListener('resize', updateDimensions);
7881

82+
updateDimensions();
83+
7984
return () => {
8085
window.removeEventListener('resize', updateDimensions);
8186
};
@@ -284,7 +289,7 @@ const AlgoScreen = ({ theme, toggleTheme }) => {
284289
</div>
285290

286291
<div className="viewport">
287-
<canvas id="canvas" width={0} height="505" ref={canvasRef}></canvas>
292+
<canvas id="canvas" ref={canvasRef}></canvas>
288293
{infoModalEnabled && (
289294
<div
290295
className={`modal info-modal ${

src/screens/HomeScreen.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,6 @@ const HomeScreen = ({ theme, toggleTheme }) => {
109109
}
110110
/>
111111
</div>
112-
{/* Blob Gimmick */}
113-
<div id="blob-container">
114-
<Blob />
115-
</div>
116112
<div className="mid-flex">
117113
<div className="inner-flex">
118114
<SearchFilter filteredAlgoList={filteredAlgoList} />
@@ -131,9 +127,15 @@ const HomeScreen = ({ theme, toggleTheme }) => {
131127
)}
132128
</div>
133129
</div>
130+
131+
{/* Blob Gimmick */}
132+
<div id="blob-container">
133+
<Blob />
134+
</div>
134135
</>
135136
}
136137
/>
138+
137139
<Route path="/about" element={<AboutScreen />} />
138140
</Routes>
139141
</div>

0 commit comments

Comments
 (0)