-
Notifications
You must be signed in to change notification settings - Fork 0
/
lines.js
64 lines (56 loc) · 1.67 KB
/
lines.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
const random = require("canvas-sketch-util/random");
const palletes = require("nice-color-palettes");
const { range } = require("canvas-sketch-util/random");
const { lerp } = require("canvas-sketch-util/math");
const pallete = random.shuffle(random.pick(palletes)).slice(0, 3);
const canvas = document.getElementById("canvas");
const ctx = canvas.getContext("2d");
ctx.canvas.width = innerWidth;
ctx.canvas.height = innerHeight;
const colors = pallete;
let width = innerWidth - 100;
let height = innerHeight - 100;
const margin = 20;
window.addEventListener("resize", () => {
width = innerWidth;
height = innerHeight;
ctx.canvas.width = width;
ctx.canvas.height = height;
init();
});
const count = 50;
const getGridPoints = () => {
const gridPoints = [];
for (let i = 0; i < count; i++) {
for (let j = 0; j < count; j++) {
const u = i / (count - 1);
const v = j / (count - 1);
gridPoints.push({ position: [u, v] });
}
}
return gridPoints;
};
function init() {
ctx.rect(0, 0, width, height);
// ctx.strokeStyle = random.pick(pallete);
// ctx.stroke();
const gridPoints = getGridPoints();
const dx = lerp(0, width, 1 / (count - 1));
const dy = lerp(0, height, 1 / (count - 1));
gridPoints.forEach(({ position }) => {
const [u, v] = position;
const x = lerp(margin, width, u);
const y = lerp(margin, height, v);
const isVerticle = random.value() > 0.5;
ctx.beginPath();
ctx.moveTo(x, y);
if (isVerticle) {
ctx.lineTo(x, y + dy);
} else {
ctx.lineTo(x + dx, y);
}
ctx.strokeStyle = "#000000";
ctx.stroke();
});
}
init();