-
Notifications
You must be signed in to change notification settings - Fork 22
/
hexagonify.html
103 lines (83 loc) · 3.41 KB
/
hexagonify.html
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
---
layout: page
title: Hexagonify
---
<h1>😎😎 Select your profile photo to make an NFT-style hexagon image ⬣⬣</h1>
<input type="file" id="file-input" accept="image/*">
<div class="output" id="output"></div>
<script>
document.addEventListener('DOMContentLoaded', function() {
const fileInput = document.getElementById('file-input');
fileInput.addEventListener('change', function(e) {
if (this.files && this.files[0]) {
processFile(this.files[0]);
}
});
function processFile(file) {
const reader = new FileReader();
reader.readAsDataURL(file);
reader.onloadend = function() {
const img = new Image();
img.src = reader.result;
img.onload = function() {
const canvas = document.createElement('canvas');
const maxSize = Math.min(img.width, img.height);
canvas.width = canvas.height = maxSize;
const ctx = canvas.getContext('2d');
ctx.drawImage(img, (img.width - maxSize) / 2, (img.height - maxSize) / 2, maxSize, maxSize, 0, 0, maxSize, maxSize);
ctx.fillStyle = 'black';
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.globalCompositeOperation = 'source-over';
drawHexagon(ctx, maxSize / 2, maxSize / 2, maxSize / 2);
ctx.clip();
ctx.drawImage(img, (img.width - maxSize) / 2, (img.height - maxSize) / 2, maxSize, maxSize, 0, 0, maxSize, maxSize);
updateOutputArea(canvas.toDataURL('image/png'));
};
};
}
/*
function drawHexagon(ctx, x, y, radius) {
ctx.beginPath();
for (let i = 0; i < 6; i++) {
ctx.lineTo(x + radius * Math.cos(i * 2 * Math.PI / 6), y + radius * Math.sin(i * 2 * Math.PI / 6));
}
ctx.closePath();
}
*/
function drawHexagon(ctx, x, y, radius) {
const sideLength = radius * Math.cos(Math.PI / 6); // Calculate side length based on radius
const cornerRadius = sideLength * 0.1; // Adjust corner radius for appearance
// Calculate start point (center-right of hexagon)
let startX = x + radius * Math.cos(0);
let startY = y + radius * Math.sin(0);
ctx.beginPath();
ctx.moveTo(startX, startY);
for (let i = 0; i < 6; i++) {
// Calculate end point of current edge
let endX = x + radius * Math.cos(i * 2 * Math.PI / 6);
let endY = y + radius * Math.sin(i * 2 * Math.PI / 6);
// Calculate the control points for rounded corners
let cp1X = endX + cornerRadius * Math.cos(i * 2 * Math.PI / 6 - Math.PI / 6);
let cp1Y = endY + cornerRadius * Math.sin(i * 2 * Math.PI / 6 - Math.PI / 6);
let nextI = (i + 1) % 6;
let nextEndX = x + radius * Math.cos(nextI * 2 * Math.PI / 6);
let nextEndY = y + radius * Math.sin(nextI * 2 * Math.PI / 6);
let cp2X = nextEndX + cornerRadius * Math.cos(i * 2 * Math.PI / 6 + Math.PI / 6);
let cp2Y = nextEndY + cornerRadius * Math.sin(i * 2 * Math.PI / 6 + Math.PI / 6);
// Draw the edges with rounded corners
ctx.bezierCurveTo(cp1X, cp1Y, cp2X, cp2Y, nextEndX, nextEndY);
}
ctx.closePath();
// Optional: Apply styles
ctx.stroke(); // Draw the outline of the hexagon
// ctx.fill(); // Optionally, fill the hexagon
}
function updateOutputArea(dataURL) {
const outputImg = document.createElement('img');
outputImg.src = dataURL;
const output = document.getElementById('output');
output.innerHTML = ''; // Clear previous output
output.appendChild(outputImg);
}
});
</script>