-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
128 lines (121 loc) · 4.26 KB
/
main.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
(function () {
// const algorithmWorker = new Worker('worker.js');
const marging = 15;
const height = window.innerHeight;
const canvasLabyrinth = document.getElementById('labyrinth');
const canvasSolution = document.getElementById('solution');
canvasLabyrinth.width = height;
canvasLabyrinth.height = height;
canvasSolution.width = height;
canvasSolution.height = height;
const ctxLabyrinth = canvasLabyrinth.getContext('2d');
const ctxSolution = canvasSolution.getContext('2d');
const squares = 100;
const printStepNumber = false;
let algorithm;
let solution;
let labyrinthShape;
let stepCount;
let steps;
function generateSolvableLabyrinth() {
stepCount = 0;
do {
algorithm = new BreadthFirstSearch({
width: squares,
height: squares,
verticalProbability: 0.5,
horizontalProbability: 0.5,
});
solution = algorithm.findSolution();
labyrinthShape = algorithm.getLabyrinth();
} while (!solution || !solution.length);
steps = algorithm.getSteps();
}
generateSolvableLabyrinth();
const squareSize = (height - marging * 2) / squares;
let anim;
function printLabyrinth(labyrinthShape) {
for (let x = 0; x < labyrinthShape.width; x++) {
for (let y = 0; y < labyrinthShape.height; y++) {
const positionShape = labyrinthShape.matrix[x][y];
ctxLabyrinth.beginPath();
ctxLabyrinth.strokeStyle = '#222222';
if (positionShape.top) {
ctxLabyrinth.moveTo(x * squareSize, y * squareSize);
ctxLabyrinth.lineTo((x + 1) * squareSize, y * squareSize);
}
if (positionShape.bottom) {
ctxLabyrinth.moveTo(x * squareSize, (y + 1) * squareSize);
ctxLabyrinth.lineTo((x + 1) * squareSize, (y + 1) * squareSize);
}
if (positionShape.left) {
ctxLabyrinth.moveTo(x * squareSize, y * squareSize);
ctxLabyrinth.lineTo(x * squareSize, (y + 1) * squareSize);
}
if (positionShape.right) {
ctxLabyrinth.moveTo((x + 1) * squareSize, y * squareSize);
ctxLabyrinth.lineTo((x + 1) * squareSize, (y + 1) * squareSize);
}
ctxLabyrinth.stroke();
if (printStepNumber && positionShape.step) {
ctxLabyrinth.textAlign = 'center';
ctxLabyrinth.textBaseline = 'middle';
ctxLabyrinth.strokeText(
positionShape.step,
x * squareSize + squareSize / 2,
y * squareSize + squareSize / 2,
);
}
ctxLabyrinth.closePath();
}
}
}
function printStep(step) {
for (const { x, y } of step.getPositions()) {
const probability = step.getProbability({ x, y });
ctxSolution.fillStyle = `rgb(255 248 117 / ${probability})`;
ctxSolution.fillRect(
x * squareSize + ctxSolution.lineWidth,
y * squareSize + ctxSolution.lineWidth,
squareSize - ctxSolution.lineWidth * 2,
squareSize - ctxSolution.lineWidth * 2,
);
}
}
function printSolution(solution) {
for (const { x, y } of solution) {
ctxSolution.fillStyle = 'rgb(255 248 117)';
ctxSolution.fillRect(
x * squareSize + ctxSolution.lineWidth,
y * squareSize + ctxSolution.lineWidth,
squareSize - ctxSolution.lineWidth * 2,
squareSize - ctxSolution.lineWidth * 2,
);
}
}
function play() {
if (!stepCount) {
ctxLabyrinth.clearRect(0, 0, canvasLabyrinth.width, canvasLabyrinth.height);
ctxLabyrinth.translate(squareSize, squareSize);
printLabyrinth(labyrinthShape);
ctxLabyrinth.translate(-squareSize, -squareSize);
}
ctxSolution.clearRect(0, 0, canvasSolution.width, canvasSolution.height);
ctxSolution.translate(squareSize, squareSize);
if (stepCount < steps.length) {
printStep(steps[stepCount]);
} else if (stepCount < steps.length + 20 && Math.random() < 0.5) {
printSolution(solution);
}
ctxSolution.translate(-squareSize, -squareSize);
if (stepCount++ < steps.length + 21) {
anim = window.requestAnimationFrame(play);
} else {
setTimeout(() => {
generateSolvableLabyrinth();
anim = window.requestAnimationFrame(play);
}, Math.random() * 500 + 200);
}
}
anim = window.requestAnimationFrame(play);
})();