forked from leander2189/four-colors
-
Notifications
You must be signed in to change notification settings - Fork 0
/
four_colors.js
151 lines (125 loc) · 3.89 KB
/
four_colors.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
class FourColors
{
constructor(canvasId, colors)
{
this.PaintColors = colors;
this.PaintCol = -1;
this.canvas = document.getElementById(canvasId);
this.ctx = this.canvas.getContext("2d", { willReadFrequently: true});
this.canvas.addEventListener('mousemove', this.handleMouseMove.bind(this));
this.canvas.addEventListener('mouseout', this.handleMouseLeave.bind(this));
this.canvas.addEventListener('click', this.handleMouseClick.bind(this));
// init
/*
this.clock = setInterval(function(t0){
var now = new Date().getTime();
var passed = now - t0;
var timer = document.getElementById('timer');
timer.innerHTML = passed / 1000;
}, 1000, this.timeInit);
*/
}
init(N)
{
var w = this.canvas.width;
var h = this.canvas.height;
this.n = N;
var positions = Float64Array.from({length: this.n * 2}, (_, i) => Math.random() * (i & 1 ? h : w))
this.voronoi = new d3.Delaunay(positions).voronoi([0, 0, w, h])
this.selected = null;
// Init color array
this.colors = Array(this.n);
for (var i = 0; i < this.n; i++) this.colors[i] = 0;
this.error = Array(this.n);
for (var i = 0; i < this.n; i++) this.error[i] = false;
this.timeInit = new Date().getTime();
this.draw();
}
handleMouseMove(event) {
const mouseX = event.clientX - this.canvas.offsetLeft;
const mouseY = event.clientY - this.canvas.offsetTop;
this.selected = null;
for (var i = 0; i < this.n; i++)
{
if (this.voronoi.contains(i, mouseX, mouseY))
{
this.selected = i;
break;
}
}
this.draw();
}
handleMouseLeave(event)
{
this.selected = null;
this.draw();
}
draw()
{
this.checkErrors();
// Print cells
this.ctx.lineWidth = 1;
for (var i = 0; i < this.n; i++)
{
this.ctx.fillStyle = this.PaintColors[this.colors[i]];
if (this.error[i]) this.ctx.strokeStyle = 'red';
else this.ctx.strokeStyle = 'black';
this.ctx.beginPath();
this.voronoi.renderCell(i, this.ctx);
this.ctx.stroke();
this.ctx.fill();
}
// Print selected
if (this.selected != null)
{
this.ctx.lineWidth = 3;
this.ctx.beginPath();
this.voronoi.renderCell(this.selected, this.ctx);
this.ctx.stroke();
}
}
handleMouseClick(event)
{
if (this.selected != null && this.PaintCol > -1)
this.colors[this.selected] = this.PaintCol;
this.draw();
var finished = this.checkComplete();
if (finished)
{
clearInterval(this.clock);
// Show finisher popup
var modal = document.getElementById("congrats-box");
modal.style.display = "block";
}
}
checkErrors()
{
for (var i = 0; i < this.n; i++)
{
var neighbors = this.voronoi.neighbors(i);
var error = false;
if (this.colors[i] > 0)
{
for (const k of neighbors)
{
if (this.colors[i] == this.colors[k])
error = true;
}
}
this.error[i] = error;
}
}
checkComplete()
{
var everythingPainted = true;
this.colors.forEach(x => {
if (x == 0) everythingPainted = false;
});
this.checkErrors();
var noErrors = true;
this.error.forEach(x => {
if (x == true) noErrors = false;
});
return everythingPainted && noErrors;
}
}