-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path10print.js
88 lines (68 loc) · 1.6 KB
/
10print.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
//SETUP canvas
const canvas = document.querySelector("#canvas1");
canvas.width = 1000;
canvas.height = 600;
let context = canvas.getContext("2d");
// VARIABLES
let xpos = 0;
let ypos = 0;
let size = 20;
let threshold = 0.5;
let speed = 100;
// SIZE SLIDER
const sizeControl = document.querySelector("#size-slider");
sizeControl.oninput = function() {
let sizeVal = sizeControl.value;
size = Number(sizeVal);
resetDraw();
}
// SPEED SLIDER
const speedControl = document.querySelector("#speed-slider");
speedControl.oninput = function() {
let speedVal = speedControl.value;
speed = Number(speedVal);
resetDraw();
}
// BALANCE SLIDER
const balanceControl = document.querySelector("#balance-slider");
balanceControl.oninput = function() {
let balanceVal = balanceControl.value;
threshold = Number(balanceVal);
resetDraw();
}
// RESET DRAW FUNCTION
function resetDraw() {
context.clearRect(0, 0, canvas.width, canvas.height);
xpos = 0;
ypos = 0;
draw();
clearInterval(drawInterval);
drawInterval = setInterval(draw, speed);
}
// DRAW FUNCTION
function draw() {
console.log(speed);
let a = Math.random();
if ( a < threshold ) {
makeLine(xpos, ypos, xpos + size, ypos + size);
} else {
makeLine(xpos + size, ypos, xpos, ypos + size);
}
xpos += size;
if ( xpos > (canvas.width - size)) {
xpos = 0;
ypos += size;
}
}
// MAKE EACH LINE FUNCTION
function makeLine (x1, y1, x2, y2) {
context.strokeStyle = "#FFFFFF";
context.beginPath();
context.moveTo(x1, y1);
context.lineTo(x2, y2);
context.stroke();
}
// SET INTERVAL
let drawInterval = setInterval(function(){
draw();
}, speed);