|
1 | | -const canvas = document.getElementById("cvs"); |
2 | | -const ctx = canvas.getContext("2d"); |
| 1 | +const canvas = document.getElementById('cvs') |
| 2 | +const ctx = canvas.getContext('2d') |
| 3 | +const CH = (canvas.height = 300) |
| 4 | +const CW = (canvas.width = 600) |
| 5 | +canvas.style.background = 'black' |
3 | 6 |
|
4 | | -const CH = (canvas.height = 300); |
5 | | -const CW = (canvas.width = 600); |
6 | | -canvas.style.background = "black"; |
7 | | -let cond = true; |
8 | | -let i = 0, j = 0; |
9 | | -let value = [CH]; |
10 | | -let range = CW / 20; |
11 | | -for (let i = 0; i <= range; i++) { |
12 | | - value[i] = Math.random() * (CH - 0 + 1) + 0; |
13 | | -} |
14 | | -console.log(value) |
15 | | - |
16 | | - |
17 | | -const RandomWalker = { |
18 | | - borderStyle: { size: 1, color: "white", style: "solid" }, |
19 | | - color: "white", |
20 | | - draw(value) { |
21 | | - canvas.style.border = `${this.borderStyle.size}px ${this.borderStyle.style} ${this.borderStyle.color}`; |
22 | | - ctx.clearRect(0, 0, CH, CW); |
23 | | - const lineSpacing = 10; |
24 | | - ctx.lineWidth = 5; |
25 | | - |
26 | | - |
27 | | - ctx.beginPath(); |
28 | | - for (let j = 0; j < value.length; j++) { |
29 | | - const y = j * lineSpacing; |
30 | | - ctx.moveTo(y, CH); |
31 | | - ctx.lineTo(y, CH - value[j]); |
32 | | - |
33 | | - } |
34 | | - ctx.closePath(); |
35 | | - ctx.strokeStyle = this.color; |
36 | | - ctx.stroke(); |
37 | | - }, |
38 | | - update() { |
39 | | - |
40 | | - |
41 | | - if (value[j] > value[j + 1]) { |
42 | | - [value[j], value[j + 1]] = [value[j + 1], value[j]]; |
43 | | - this.color = "white" |
44 | | - } |
45 | | - if (i < value.length) { |
46 | | - j++; |
47 | | - console.log(i, j) |
48 | | - if (j >= value.length - i - 1) { |
49 | | - this.color = "red" |
50 | | - j = 0; |
51 | | - i = i + 1; |
52 | | - } |
53 | | - } else { |
54 | | - |
55 | | - cond = false; |
56 | | - } |
57 | | - this.draw(value); |
| 7 | +const BubbleSort = { |
| 8 | + i: 0, |
| 9 | + j: 0, |
| 10 | + seedValue: [CH], |
| 11 | + loopSpeed: 100, |
| 12 | + isLoop: true, |
| 13 | + borderStyle: { size: 1, color: 'white', style: 'solid' }, |
| 14 | + color: 'white', |
| 15 | + sortedLine: null, |
| 16 | + draw() { |
| 17 | + canvas.style.border = `${this.borderStyle.size}px ${this.borderStyle.style} ${this.borderStyle.color}` |
| 18 | + ctx.clearRect(0, 0, CH, CW) |
| 19 | + const lineSpacing = 10 |
| 20 | + ctx.lineWidth = 5 |
| 21 | + |
| 22 | + for (let j = 0; j < this.seedValue.length; j++) { |
| 23 | + const y = j * lineSpacing |
| 24 | + ctx.beginPath() |
| 25 | + ctx.moveTo(y, CH) |
| 26 | + ctx.lineTo(y, CH - this.seedValue[j]) |
| 27 | + ctx.closePath() |
| 28 | + if (this.sortedLine === this.seedValue[j]) { |
| 29 | + this.color = 'red' |
| 30 | + ctx.strokeStyle = this.color |
| 31 | + ctx.stroke() |
| 32 | + } else { |
| 33 | + this.color = 'white' |
| 34 | + ctx.strokeStyle = this.color |
| 35 | + ctx.stroke() |
| 36 | + } |
| 37 | + } |
| 38 | + }, |
| 39 | + update() { |
| 40 | + if (this.seedValue[this.j] > this.seedValue[this.j + 1]) { |
| 41 | + ;[this.seedValue[this.j], this.seedValue[this.j + 1]] = [ |
| 42 | + this.seedValue[this.j + 1], |
| 43 | + this.seedValue[this.j], |
| 44 | + ] |
| 45 | + this.sortedLine = this.seedValue[this.j + 1] |
| 46 | + } |
| 47 | + if (this.i < this.seedValue.length) { |
| 48 | + this.j++ |
| 49 | + if (this.j >= this.seedValue.length - this.i - 1) { |
| 50 | + this.color = 'red' |
| 51 | + this.j = 0 |
| 52 | + this.i = this.i + 1 |
| 53 | + } |
| 54 | + } else { |
| 55 | + this.isLoop = false |
| 56 | + } |
| 57 | + this.draw() |
| 58 | + }, |
| 59 | + startup() { |
| 60 | + let range = CW / 20 |
58 | 61 |
|
| 62 | + for (this.i = 0; this.i <= range; this.i++) { |
| 63 | + this.seedValue[this.i] = Math.random() * (CH - 0 + 1) + 0 |
| 64 | + } |
| 65 | + this.i = 0 |
| 66 | + }, |
| 67 | +} |
59 | 68 |
|
60 | | - }, |
61 | | - // BubbleSort(arr) { |
62 | | - // if (arr.length === 0 || arr.length === 1) return; |
63 | | - // // for (let i = 0; i < arr.length; i++) { |
64 | | - // // for (let j = 0; j <= arr.length; j++) { |
65 | | - // if (arr[j] > arr[j + 1]) { |
66 | | - // [arr[j], arr[j + 1]] = [arr[j + 1], arr[j]]; |
67 | | - // } |
68 | | - // // } |
69 | | - // // } |
70 | | - // return arr; |
71 | | - // }, |
72 | | -}; |
| 69 | +// You could use window.requestAnimationFrame as well. but, for simplicity i'm using setInterval. |
73 | 70 |
|
74 | 71 | function render() { |
75 | | - if (cond) { |
76 | | - |
77 | | - setInterval(() => { |
78 | | - RandomWalker.update(); |
79 | | - }, 100) |
80 | | - } |
| 72 | + BubbleSort.startup() |
| 73 | + if (BubbleSort.isLoop) { |
| 74 | + setInterval(() => { |
| 75 | + BubbleSort.update(BubbleSort.seedValue) |
| 76 | + }, BubbleSort.loopSpeed) |
| 77 | + } |
81 | 78 | } |
82 | 79 |
|
83 | | -render(); |
| 80 | +render() // calling the main functions. |
0 commit comments