Skip to content

Commit 2941a5c

Browse files
committed
feat: added new sketch hexagon-tiling
1 parent b6cfe7f commit 2941a5c

File tree

3 files changed

+92
-0
lines changed

3 files changed

+92
-0
lines changed

p5/hexagon-tiling/README.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Hexagon Tiling
2+
3+
![print (1)](https://i.imgur.com/P1YMHlP.png)

p5/hexagon-tiling/index.html

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
4+
<head>
5+
<meta charset="UTF-8">
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
7+
<title>
8+
Hexagon Tiling
9+
</title>
10+
11+
<link rel="stylesheet" type="text/css" href="../../assets/css/style.css">
12+
13+
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.9.4/p5.js"></script>
14+
<!-- <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.9.4/addons/p5.sound.min.js"></script> -->
15+
</head>
16+
17+
<body>
18+
<main></main>
19+
<script src="../../assets/js/color-palettes.js"></script>
20+
<script src="sketch.js"></script>
21+
</body>
22+
23+
</html>

p5/hexagon-tiling/sketch.js

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
var config = {
2+
scl: 30,
3+
};
4+
const one = (Math.PI * 2) / 6;
5+
6+
const colors = [
7+
'#ff7b00',
8+
'#ff8800',
9+
'#ff9500',
10+
'#ffa200',
11+
'#ffaa00',
12+
'#ffb700',
13+
'#ffc300',
14+
'#ffd000',
15+
'#ffdd00',
16+
'#ffea00'
17+
];
18+
19+
const spacing = config.scl / 13 * 6;
20+
21+
function setup() {
22+
const canvas = document.querySelector('canvas');
23+
if (canvas) canvas.remove();
24+
createCanvas(window.innerWidth, window.innerHeight);
25+
frameRate(30);
26+
background(random(colors));
27+
28+
const cols = (width / config.scl) + 1;
29+
const rows = (height / config.scl) + (config.scl * 2);
30+
31+
for (let col = 0; col < cols; col++) {
32+
for (let row = 0; row < rows; row++) {
33+
if (row % 2 === 0) {
34+
//rect(col * config.scl - config.scl / 2, row * config.scl, config.scl);
35+
hexagon(col, row, config.scl, true);
36+
} else {
37+
//rect(col * config.scl, row * config.scl, config.scl);
38+
hexagon(col, row, config.scl, false);
39+
}
40+
}
41+
}
42+
}
43+
44+
function hexagon(col, row, scl, odd) {
45+
fill(random(colors));
46+
noStroke();
47+
const offset = odd ? -scl / 2 : 0;
48+
push();
49+
translate(col * scl + offset, row * scl - row * scl / 8);
50+
rotate(map(30, 0, 360, 0, TWO_PI));
51+
beginShape();
52+
for (let i = 0; i < 6; i++) {
53+
const angle = i * one;
54+
const x = cos(angle) * (spacing);
55+
const y = sin(angle) * (spacing);
56+
vertex(x, y);
57+
}
58+
endShape();
59+
pop();
60+
}
61+
62+
function keyPressed() {
63+
if (keyCode === 32) {
64+
saveCanvas("print.png");
65+
}
66+
}

0 commit comments

Comments
 (0)