-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
84 lines (75 loc) · 2.36 KB
/
index.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="robots" content="noindex, nofollow">
<title>Image Pixelizer</title>
<style>
body {
font-family: Arial, sans-serif;
text-align: center;
margin-top: 40px;
}
canvas {
border: 1px solid #ccc;
margin-top: 20px;
}
.controls {
margin-bottom: 20px;
}
</style>
</head>
<body>
<h1>Image Pixelizer</h1>
<div class="controls">
<input type="file" id="upload" accept="image/*">
<br><br>
<label for="pixelSize">Pixelation Level (block size): </label>
<input type="range" id="pixelSize" min="1" max="50" value="10">
<span id="pixelValue">10</span>
</div>
<canvas id="canvas"></canvas>
<script>
const upload = document.getElementById('upload');
const pixelSizeInput = document.getElementById('pixelSize');
const pixelValueDisplay = document.getElementById('pixelValue');
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
let image = new Image();
pixelSizeInput.addEventListener('input', () => {
pixelValueDisplay.textContent = pixelSizeInput.value;
if (image.src) {
pixelate();
}
});
upload.addEventListener('change', (e) => {
const file = e.target.files[0];
if (!file) return;
const reader = new FileReader();
reader.onload = function(event) {
image = new Image();
image.onload = function() {
canvas.width = image.width;
canvas.height = image.height;
pixelate();
};
image.src = event.target.result;
};
reader.readAsDataURL(file);
});
function pixelate() {
const pixelSize = parseInt(pixelSizeInput.value, 10);
const tempCanvas = document.createElement('canvas');
const tempCtx = tempCanvas.getContext('2d');
const scaledWidth = Math.ceil(canvas.width / pixelSize);
const scaledHeight = Math.ceil(canvas.height / pixelSize);
tempCanvas.width = scaledWidth;
tempCanvas.height = scaledHeight;
tempCtx.drawImage(image, 0, 0, scaledWidth, scaledHeight);
ctx.imageSmoothingEnabled = false;
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.drawImage(tempCanvas, 0, 0, scaledWidth, scaledHeight, 0, 0, canvas.width, canvas.height);
}
</script>
</body>
</html>