-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSorter.js
96 lines (69 loc) · 2.09 KB
/
Sorter.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
const canvas = document.getElementById("canvas");
const ctx = canvas.getContext('2d');
// add a colum number value to have a value to sort by
ImageData.prototype.colNum = "should be number";
var imgcols = new Array(); // colloms of image pixels
var img = new Image(); // image
var sortType
var offset =0;
SetUpSorter(parent.image, parent.sortType);
async function SetUpSorter(imgSrc = img.src, sortType = "Quick Sort"){
// make new img.src if it ex
img.src = imgSrc;
// do when finished loading images
await new Promise(resolve => { img.onload = img.onerror = resolve; }).then(() => {
//console.log('images finished loading');
});
ctx.canvas.width = img.width+ (parent.recording? 0 : img.width + 10 );
ctx.canvas.height = img.height;
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.drawImage(img, 0, 0);
// get image data
for (var i=0; i<img.width; i++)
{
imgcols[i] = ctx.getImageData(i, 0, 1, img.height);
imgcols[i].colNum = i;
//console.log(imgcols[i].colNum)
}
offset = parent.recording? 0 : (img.width+10)
// make randomized image
imgcols = shuffle(imgcols);
for (var i=0; i<img.width; i++)
{
ctx.putImageData(imgcols[i], i+offset, 0);
}
// start sorting image
runSort(sortType);
// start loop
thing();
}
//console.log(imgcols);
function draw(){
for (var i=0; i<img.width; i++)
{
ctx.putImageData(imgcols[i], i+offset, 0);
}
}
function thing(){
window.setTimeout(function(){
draw();
window.requestAnimationFrame(thing);
},1000/30)
}
async function runSort(sortType){
switch (sortType){
case "Quick Sort":
await quickSort(imgcols, 0, imgcols.length - 1);
break;
case "Bubble Sort":
await bubbleSort(imgcols, imgcols.length);
break;
}
console.log("done sorting");
if(parent.recording)
{
await sleep(1000);
parent.mediaRecorder.stop();
parent.recording = false;
}
}