-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanimate.js
More file actions
66 lines (47 loc) · 2 KB
/
animate.js
File metadata and controls
66 lines (47 loc) · 2 KB
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
import { ctx, rectArray, graph } from './canvas.js'
// promise highlight
export function promise_hightlight(i, c) {
return new Promise( (resolve) => {
setTimeout( () => {
ctx.clearRect(0, 0, innerWidth, innerHeight);
for( var j = 0; j < 10; j++) {
rectArray[i].draw(rectArray[i].x, rectArray[i].y, rectArray[i].width, rectArray[i].height, c );
rectArray[i + 1].draw(rectArray[i + 1].x, rectArray[i + 1].y, rectArray[i + 1].width, rectArray[i + 1].height, c );
if(j != i && j != i+1) {
rectArray[j].draw(rectArray[j].x, rectArray[j].y, rectArray[j].width, rectArray[j].height, "turquoise");
}
}
graph();
resolve();
}, 1000 );
} )
}
// swap function
export function swap(idx, buffer_x1, buffer_x2, callBack) {
ctx.clearRect(0, 0, innerWidth, innerHeight);
console.log("we are in the swap function");
console.log("i is" + idx);
console.log(rectArray[idx].x);
console.log(rectArray[idx+1].x);
rectArray[idx].update(buffer_x2, "forward");
rectArray[idx+1].update(buffer_x1, "backward");
for(var j=0; j<10; j++) {
if(j != idx && j != idx+1) {
rectArray[j].draw(rectArray[j].x, rectArray[j].y, rectArray[j].width, rectArray[j].height, "turquoise");
}
}
graph();
if(rectArray[idx].x < buffer_x2 && rectArray[idx+1].x > buffer_x1) {
var requestId = window.requestAnimationFrame(() => {swap(idx, buffer_x1, buffer_x2, callBack)});
}else if(rectArray[idx].x >= buffer_x2 && rectArray[idx+1].x <= buffer_x1) {
console.log(requestId);
callBack();
window.cancelAnimationFrame(requestId);
}
}
// promise_swap
export function promise_swap(idx, buffer_x1, buffer_x2) {
return new Promise( (resolve) => {
window.requestAnimationFrame(() => {swap(idx, buffer_x1, buffer_x2, () => {resolve()}) });
} )
}