-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcircular_cropper.html
More file actions
153 lines (132 loc) · 8.48 KB
/
circular_cropper.html
File metadata and controls
153 lines (132 loc) · 8.48 KB
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title>Pro Circular Cropper</title>
<link href="https://fonts.googleapis.com/css2?family=Poppins:wght@400;500;600;700&display=swap" rel="stylesheet">
<link href="https://cdnjs.cloudflare.com/ajax/libs/cropperjs/1.5.13/cropper.min.css" rel="stylesheet">
<style>
* { margin: 0; padding: 0; box-sizing: border-box; font-family: 'Poppins', sans-serif; user-select: none; -webkit-tap-highlight-color: transparent; }
body { background: linear-gradient(135deg, #0f0c29, #302b63, #24243e); color: #fff; min-height: 100vh; display: flex; justify-content: center; align-items: center; padding: 15px; }
.app-container {
width: 100%; max-width: 450px; background: rgba(25, 25, 35, 0.7); backdrop-filter: blur(20px);
border: 1px solid rgba(255, 255, 255, 0.1); padding: 25px; border-radius: 25px;
box-shadow: 0 30px 60px rgba(0,0,0,0.5); text-align: center;
}
.header { margin-bottom: 25px; }
.app-title { font-size: 24px; font-weight: 700; background: linear-gradient(135deg, #00f2fe, #4facfe); -webkit-background-clip: text; -webkit-text-fill-color: transparent; }
.app-subtitle { font-size: 13px; color: #a0a0b5; }
.upload-area {
display: flex; flex-direction: column; align-items: center; justify-content: center;
padding: 50px 20px; border: 2px dashed rgba(79, 172, 254, 0.5); border-radius: 20px;
background: rgba(255, 255, 255, 0.02); cursor: pointer; transition: 0.3s;
}
.upload-area:hover { background: rgba(79, 172, 254, 0.1); border-color: #00f2fe; }
.upload-area svg { width: 45px; height: 45px; stroke: #4facfe; fill: none; stroke-width: 1.5; margin-bottom: 15px; }
.upload-area span { font-size: 15px; font-weight: 500; color: #e0e0e0; }
#file-input { display: none; }
.workspace { display: none; animation: fadeIn 0.4s ease; }
@keyframes fadeIn { from { opacity: 0; transform: translateY(10px); } to { opacity: 1; transform: translateY(0); } }
.img-container { width: 100%; height: 320px; border-radius: 15px; overflow: hidden; background: #000; margin-bottom: 15px; }
.img-container img { max-width: 100%; display: block; }
.toolbar { display: flex; gap: 8px; margin-bottom: 15px; justify-content: space-between; }
.btn-icon {
flex: 1; padding: 12px; background: rgba(255, 255, 255, 0.05); border: 1px solid rgba(255, 255, 255, 0.1);
border-radius: 12px; cursor: pointer; color: #fff; display: flex; justify-content: center; transition: 0.2s;
}
.btn-icon:hover { background: rgba(79, 172, 254, 0.2); border-color: #4facfe; color: #4facfe; }
.btn-icon svg { width: 20px; height: 20px; stroke: currentColor; fill: none; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.btn-cancel { color: #ff4b4b; background: rgba(255, 75, 75, 0.1); border-color: rgba(255, 75, 75, 0.2); }
.btn-cancel:hover { background: rgba(255, 75, 75, 0.2); border-color: #ff4b4b; color: #ff4b4b; }
.btn-primary {
width: 100%; padding: 15px; border: none; border-radius: 12px; background: linear-gradient(135deg, #00f2fe, #4facfe);
color: #fff; font-size: 16px; font-weight: 600; cursor: pointer; display: flex; justify-content: center; align-items: center; gap: 10px; transition: 0.3s;
}
.btn-primary:active { transform: scale(0.98); }
.btn-primary svg { width: 22px; height: 22px; stroke: currentColor; fill: none; stroke-width: 2; }
/* Force Circular Cropper UI */
.cropper-view-box, .cropper-face { border-radius: 50%; }
.cropper-view-box { outline-color: #00f2fe; }
.cropper-line, .cropper-point { background-color: #00f2fe; }
</style>
</head>
<body>
<div class="app-container">
<div class="header">
<div class="app-title">Avatar Maker</div>
<div class="app-subtitle">Create Perfect Circular PNGs</div>
</div>
<label class="upload-area" id="upload-box">
<input type="file" id="file-input" accept="image/png, image/jpeg, image/webp">
<svg viewBox="0 0 24 24"><path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4"></path><polyline points="17 8 12 3 7 8"></polyline><line x1="12" y1="3" x2="12" y2="15"></line></svg>
<span>Upload Image to Crop</span>
</label>
<div class="workspace" id="workspace">
<div class="img-container">
<img id="image" src="">
</div>
<div class="toolbar">
<div class="btn-icon" onclick="cropper.rotate(-90)" title="Rotate"><svg viewBox="0 0 24 24"><path d="M3 12a9 9 0 1 0 9-9 9.75 9.75 0 0 0-6.74 2.74L3 8"></path><path d="M3 3v5h5"></path></svg></div>
<div class="btn-icon" onclick="cropper.zoom(0.1)" title="Zoom In"><svg viewBox="0 0 24 24"><circle cx="11" cy="11" r="8"></circle><line x1="21" y1="21" x2="16.65" y2="16.65"></line><line x1="11" y1="8" x2="11" y2="14"></line><line x1="8" y1="11" x2="14" y2="11"></line></svg></div>
<div class="btn-icon" onclick="cropper.zoom(-0.1)" title="Zoom Out"><svg viewBox="0 0 24 24"><circle cx="11" cy="11" r="8"></circle><line x1="21" y1="21" x2="16.65" y2="16.65"></line><line x1="8" y1="11" x2="14" y2="11"></line></svg></div>
<div class="btn-icon btn-cancel" onclick="location.reload()" title="Cancel"><svg viewBox="0 0 24 24"><line x1="18" y1="6" x2="6" y2="18"></line><line x1="6" y1="6" x2="18" y2="18"></line></svg></div>
</div>
<button class="btn-primary" onclick="downloadCircularPNG()">
<svg viewBox="0 0 24 24"><path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4"></path><polyline points="7 10 12 15 17 10"></polyline><line x1="12" y1="15" x2="12" y2="3"></line></svg>
Download Circle PNG
</button>
</div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/cropperjs/1.5.13/cropper.min.js"></script>
<script>
let cropper;
document.getElementById('file-input').addEventListener('change', function(e) {
if(e.target.files.length) {
const reader = new FileReader();
reader.onload = function(event) {
document.getElementById('image').src = event.target.result;
document.getElementById('upload-box').style.display = 'none';
document.getElementById('workspace').style.display = 'block';
cropper = new Cropper(document.getElementById('image'), {
aspectRatio: 1, // Fix to Square
viewMode: 1,
dragMode: 'move',
guides: false,
center: false,
background: false
});
}
reader.readAsDataURL(e.target.files[0]);
}
});
// The magic function that cuts out a circle from the square canvas
function getRoundedCanvas(sourceCanvas) {
const canvas = document.createElement('canvas');
const context = canvas.getContext('2d');
const width = sourceCanvas.width;
const height = sourceCanvas.height;
canvas.width = width; canvas.height = height;
context.imageSmoothingEnabled = true;
context.drawImage(sourceCanvas, 0, 0, width, height);
context.globalCompositeOperation = 'destination-in';
context.beginPath();
context.arc(width / 2, height / 2, Math.min(width, height) / 2, 0, 2 * Math.PI, true);
context.fill();
return canvas;
}
function downloadCircularPNG() {
if(!cropper) return;
// Get High Quality Square Canvas
const squareCanvas = cropper.getCroppedCanvas({ width: 1000, height: 1000 });
// Convert to Circle
const circularCanvas = getRoundedCanvas(squareCanvas);
// Download as PNG (Required for transparent background)
const link = document.createElement('a');
link.download = `Avatar_${new Date().getTime()}.png`;
link.href = circularCanvas.toDataURL("image/png", 1.0);
link.click();
}
</script>
</body>
</html>