Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added start/stop feature #8

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 70 additions & 13 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,49 @@ const CANVAS_WIDTH = 3000
const CANVAS_HEIGHT = 600
const MAX_VALUE = CANVAS_HEIGHT
const NUM_ELEMENTS = Math.floor(CANVAS_WIDTH / (MAX_RECT_WIDTH * 2))
let stop = false;
let lastCtx;
let laststeps;
let lastspeed;
let lastindex = -1;

setDisplay(false);

function setDisplay(stopping) {
if (stopping) {
let el = document.getElementById("start");
el.style.display = 'unset';
el = document.getElementById("stop");
el.style.display = 'none';
stop = true;
}
else {
let el = document.getElementById("stop");
el.style.display = 'unset';
el = document.getElementById("start");
el.style.display = 'none';
stop = false;
}
}

document.getElementById("start").addEventListener("click", function () {
setDisplay(false);
if (lastindex >= 0) {
drawSteps(lastCtx, laststeps, lastindex, lastspeed).then(function (index) {
console.log('index', index);
lastindex = index;
})
}
});

document.getElementById("stop").addEventListener("click", function () {
// document.getElementById("demo").innerHTML = "Hello World";
setDisplay(true);
});





function drawRect(ctx, x, y, height, color = DEFAULT_RECT_COLOR) {
ctx.fillStyle = color
Expand All @@ -26,17 +69,19 @@ function drawStep(ctx, step) {
clearCanvas(ctx)
let xPosition = 0
let yPosition = CANVAS_HEIGHT

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove this

const values = step.array
const [i, j] = step.swapPositions || [-1, -1]


//console.log('valuse', values.entries());
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove the line and comment

for (const [index, value] of values.entries()) {

const color =
index === i
? PIVOT_RECT_COLOR
: index === j
? SWAPPED_RECT_COLOR
: DEFAULT_RECT_COLOR
index === i
? PIVOT_RECT_COLOR
: index === j
? SWAPPED_RECT_COLOR
: DEFAULT_RECT_COLOR
Copy link
Owner

@nicodelpiano nicodelpiano Oct 8, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you rever the format that was before? see the diff on the left

drawRect(ctx, xPosition, yPosition, -value, color)
xPosition += 2 * MAX_RECT_WIDTH
}
Expand All @@ -48,23 +93,35 @@ function clearCanvas(ctx) {
ctx.fillRect(0, 0, CANVAS_WIDTH, CANVAS_HEIGHT)
}

async function drawSteps(ctx, steps, speed = DEFAULT_SPEED) {
for (const step of steps) {


async function drawSteps(ctx, steps, index = 0, speed = DEFAULT_SPEED) {
lastCtx = ctx;
laststeps = steps;
lastspeed = speed;
let i;
for (i = index; i < steps.length; i++) {
if (stop) {
return i;
}
await new Promise((resolve, reject) => {
setTimeout(() => resolve(drawStep(ctx, step)), speed)
setTimeout(() => resolve(drawStep(ctx, steps[i])), speed)
})

}
return -1;
}

document.addEventListener('DOMContentLoaded', function(event) {
document.addEventListener('DOMContentLoaded', function (event) {
const canvas = document.getElementById('myCanvas')
canvas.width = CANVAS_WIDTH
canvas.height = CANVAS_HEIGHT
const ctx = canvas.getContext('2d')

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rm this

const burger = document.querySelector('.burger')
const menu = document.querySelector('.menu')
const menuItems = document.querySelectorAll('.menu li')

let isExecuting = false
let currentlyExecuting = ''

Expand Down Expand Up @@ -115,7 +172,7 @@ document.addEventListener('DOMContentLoaded', function(event) {
}
})
})

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rm this

burger.addEventListener('click', () => {
menu.classList.toggle('menu-active')
burger.classList.toggle('toggle')
Expand Down
5 changes: 4 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ <h1>Sorting Algorithms Visualizer</h1>
<p class="subheader">
Small project to visualize sorting algorithms. The purpose was to play around drawing with canvas.
</p>
<br>
<button id='start'>START</button>
<button id='stop'>STOP</button>
</header>
<main>
<div class="canvas-container">
Expand All @@ -39,4 +42,4 @@ <h1>Sorting Algorithms Visualizer</h1>
</main>
</body>

</html>
</html>
2 changes: 1 addition & 1 deletion sorting-algorithms/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ function quickSortHelper(array, startIndex, endIndex, steps = []) {
}
swapAndTrackSteps(array, pivotIndex, rightIndex, steps)
const isLeftSubarraySmaller =
rightIndex - 1 - startIndex < endIndex - leftIndex - 1
rightIndex - 1 - startIndex < endIndex - leftIndex - 1
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See the diff on the left, there's two spaces identation here

if (isLeftSubarraySmaller) {
quickSortHelper(array, startIndex, rightIndex - 1, steps)
quickSortHelper(array, rightIndex + 1, endIndex, steps)
Expand Down