-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscripts.js
More file actions
84 lines (74 loc) · 1.78 KB
/
scripts.js
File metadata and controls
84 lines (74 loc) · 1.78 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// Init Canvas
let c = document.getElementById("canvas");
let ctx = c.getContext("2d");
// Global variables because I honestly don't have the time
let x, y, size, proba, offset, heart;
/*
Generates the pattern.
Fills the background and then calls draw function in loop until the
pattern is drawn.
*/
function generate(settings) {
// Draw background color
ctx.beginPath();
ctx.fillStyle=settings.bgColor;
ctx.fillRect(0, 0, c.width, c.height);
ctx.closePath();
// Initiate the line style
ctx.beginPath();
ctx.strokeStyle = settings.lineColor;
ctx.lineWidth = settings.lineWidth;
// Incremental variables
x = y = 0;
size = settings.patternSize;
proba = settings.slashProbability / 100;
offset = settings.lineWidth / 2 / Math.sqrt(2);
heart = setInterval(draw, 1000/75);
}
/*
Draw either a slash ot a backslash.
The values then move on to the next horizontal/vertical location.
*/
function draw() {
if (Math.random() < proba) {
slash(ctx, x, y, size)
} else {
backSlash(ctx, x, y, size);
}
x += size;
if (x >= c.width) {
y += size;
x = 0;
}
if (y >= c.height) {
clearInterval(heart);
}
}
/*
Draw a slash.
*/
function slash(ctx, x, y, size) {
ctx.moveTo(x - offset, y + offset + size);
ctx.lineTo(x + offset + size, y - offset);
ctx.stroke();
}
/*
Draw a backslash
*/
function backSlash(ctx, x, y, size) {
ctx.moveTo(x - offset, y - offset);
ctx.lineTo(x + offset + size, y + offset + size);
ctx.stroke();
}
/*
Start generating on page load.
*/
generate(getSettings());
/*
Register event on generate button click.
*/
let generateButton = document.querySelector('#submit-settings');
generateButton.addEventListener('click', function() {
clearInterval(heart);
generate(getSettings());
}, false);