-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrenderer.js
More file actions
420 lines (350 loc) · 14.7 KB
/
Copy pathrenderer.js
File metadata and controls
420 lines (350 loc) · 14.7 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
const { ipcRenderer } = require('electron');
class WatermarkApp {
constructor() {
this.canvas = null;
this.ctx = null;
this.originalImage = null;
this.logoImage = null;
this.currentColor = '#ffffff';
this.currentOpacity = 0.5;
this.currentSize = 0.6;
this.currentPosition = 0.04;
this.originalFileName = '';
this.quickMode = false;
this.init();
}
async init() {
this.setupCanvas();
this.setupEventListeners();
await this.loadLogo();
}
setupCanvas() {
this.canvas = document.getElementById('canvas');
this.ctx = this.canvas.getContext('2d');
// Canvas kalite ayarlarını maksimuma çıkar
this.ctx.imageSmoothingEnabled = true;
this.ctx.imageSmoothingQuality = 'high';
}
setupEventListeners() {
// Görsel seçme butonu
document.getElementById('selectImageBtn').addEventListener('click', () => {
this.selectImage();
});
// Yeni görsel butonu
document.getElementById('newImageBtn').addEventListener('click', () => {
this.selectImage();
});
// Hızlı çıktı modu toggle
const quickModeToggle = document.getElementById('quickModeToggle');
quickModeToggle.addEventListener('change', (e) => {
this.quickMode = e.target.checked;
if (this.quickMode) {
this.showQuickModeNotification();
}
});
// Upload area'ya tıklama
const uploadArea = document.getElementById('uploadArea');
uploadArea.addEventListener('click', () => {
this.selectImage();
});
// Drag & Drop
uploadArea.addEventListener('dragover', (e) => {
e.preventDefault();
uploadArea.classList.add('dragover');
});
uploadArea.addEventListener('dragleave', () => {
uploadArea.classList.remove('dragover');
});
uploadArea.addEventListener('drop', (e) => {
e.preventDefault();
uploadArea.classList.remove('dragover');
const files = e.dataTransfer.files;
if (files.length > 0) {
this.loadImageFile(files[0]);
}
});
// Renk paleti
document.querySelectorAll('.color-option').forEach(option => {
option.addEventListener('click', (e) => {
if (!e.target.classList.contains('custom-color')) {
document.querySelectorAll('.color-option').forEach(opt => opt.classList.remove('active'));
e.target.classList.add('active');
this.currentColor = e.target.dataset.color;
this.updateWatermark();
}
});
});
// Custom renk seçici
const customColorPicker = document.getElementById('customColorPicker');
customColorPicker.addEventListener('change', (e) => {
document.querySelectorAll('.color-option').forEach(opt => opt.classList.remove('active'));
document.querySelector('.custom-color').classList.add('active');
this.currentColor = e.target.value;
this.updateWatermark();
});
// Şeffaflık slider
const opacitySlider = document.getElementById('opacitySlider');
const opacityValue = document.getElementById('opacityValue');
opacitySlider.addEventListener('input', (e) => {
this.currentOpacity = parseFloat(e.target.value);
opacityValue.textContent = Math.round(this.currentOpacity * 100) + '%';
this.updateWatermark();
});
// Boyut slider
const sizeSlider = document.getElementById('sizeSlider');
const sizeValue = document.getElementById('sizeValue');
sizeSlider.addEventListener('input', (e) => {
this.currentSize = parseFloat(e.target.value);
sizeValue.textContent = Math.round(this.currentSize * 100) + '%';
this.updateWatermark();
});
// Pozisyon slider
const positionSlider = document.getElementById('positionSlider');
const positionValue = document.getElementById('positionValue');
positionSlider.addEventListener('input', (e) => {
this.currentPosition = parseFloat(e.target.value);
positionValue.textContent = Math.round(this.currentPosition * 100) + '%';
this.updateWatermark();
});
// Sıfırla butonu
document.getElementById('resetBtn').addEventListener('click', () => {
this.resetSettings();
});
// İndir butonu
document.getElementById('downloadBtn').addEventListener('click', () => {
this.downloadImage();
});
}
async loadLogo() {
try {
const logoData = await ipcRenderer.invoke('get-logo');
if (logoData) {
this.logoImage = new Image();
this.logoImage.onload = () => {
console.log('Logo yüklendi');
};
this.logoImage.src = logoData;
} else {
console.error('Logo dosyası bulunamadı');
}
} catch (error) {
console.error('Logo yüklenirken hata:', error);
}
}
async selectImage() {
try {
const imagePath = await ipcRenderer.invoke('select-image');
if (imagePath) {
this.loadImageFromPath(imagePath);
}
} catch (error) {
console.error('Görsel seçilirken hata:', error);
}
}
loadImageFromPath(imagePath) {
const img = new Image();
img.onload = () => {
this.originalImage = img;
this.originalFileName = imagePath.split('\\').pop().split('/').pop();
this.setupCanvasSize();
if (this.quickMode) {
// Hızlı mod: Otomatik işle ve kaydet
this.processQuickMode();
} else {
// Normal mod: Editörü göster
this.showEditor();
this.updateWatermark();
}
};
img.onerror = () => {
alert('Görsel yüklenirken hata oluştu!');
};
img.src = imagePath;
}
loadImageFile(file) {
if (!file.type.startsWith('image/')) {
alert('Lütfen geçerli bir görsel dosyası seçin!');
return;
}
const reader = new FileReader();
reader.onload = (e) => {
const img = new Image();
img.onload = () => {
this.originalImage = img;
this.originalFileName = file.name;
this.setupCanvasSize();
if (this.quickMode) {
// Hızlı mod: Otomatik işle ve kaydet
this.processQuickMode();
} else {
// Normal mod: Editörü göster
this.showEditor();
this.updateWatermark();
}
};
img.src = e.target.result;
};
reader.readAsDataURL(file);
}
setupCanvasSize() {
if (!this.originalImage) return;
// Orijinal boyutları sakla
this.originalWidth = this.originalImage.width;
this.originalHeight = this.originalImage.height;
// Görüntüleme için boyutlandırma (sadece CSS ile)
const maxDisplayWidth = 800;
const maxDisplayHeight = 600;
let displayWidth = this.originalWidth;
let displayHeight = this.originalHeight;
// Orantılı görüntüleme boyutlandırması
if (displayWidth > maxDisplayWidth || displayHeight > maxDisplayHeight) {
const ratio = Math.min(maxDisplayWidth / displayWidth, maxDisplayHeight / displayHeight);
displayWidth *= ratio;
displayHeight *= ratio;
}
// Canvas'ı ORİJİNAL boyutlarda ayarla (kalite kaybı olmasın)
this.canvas.width = this.originalWidth;
this.canvas.height = this.originalHeight;
// Sadece görüntüleme boyutunu CSS ile ayarla
this.canvas.style.width = displayWidth + 'px';
this.canvas.style.height = displayHeight + 'px';
this.canvas.style.maxWidth = '100%';
this.canvas.style.maxHeight = '100%';
}
showEditor() {
// Upload section'ı gizle
document.querySelector('.upload-section').style.display = 'none';
// Editor section'ı göster
document.getElementById('editorSection').style.display = 'flex';
}
updateWatermark() {
if (!this.originalImage || !this.logoImage) return;
// Canvas'ı temizle
this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
// Orijinal görseli FULL boyutunda çiz (kalite kaybı olmasın)
this.ctx.drawImage(this.originalImage, 0, 0, this.originalWidth, this.originalHeight);
// Logo boyutunu ORİJİNAL görsel boyutuna göre hesapla (çok daha büyük)
const logoBaseSize = Math.min(this.originalWidth, this.originalHeight) * 0.4; // Görsel boyutunun %40'ı
const logoWidth = logoBaseSize * this.currentSize;
const logoHeight = (logoBaseSize * this.logoImage.height / this.logoImage.width) * this.currentSize;
// Logo pozisyonunu ORİJİNAL boyutlara göre hesapla
const x = (this.originalWidth - logoWidth) / 2;
const y = this.originalHeight - logoHeight - (this.originalHeight * this.currentPosition); // Kullanıcı ayarına göre
// Renk filtresi uygula ve filtrelenmiş logoyu al
const filteredLogo = this.applyColorFilter();
// Şeffaflık ayarla
this.ctx.globalAlpha = this.currentOpacity;
// Filtrelenmiş logoyu çiz
if (filteredLogo) {
this.ctx.drawImage(filteredLogo, x, y, logoWidth, logoHeight);
} else {
this.ctx.drawImage(this.logoImage, x, y, logoWidth, logoHeight);
}
// Alpha'yı sıfırla
this.ctx.globalAlpha = 1.0;
}
applyColorFilter() {
if (!this.logoImage) return null;
// Geçici canvas oluştur
const tempCanvas = document.createElement('canvas');
const tempCtx = tempCanvas.getContext('2d');
tempCanvas.width = this.logoImage.width;
tempCanvas.height = this.logoImage.height;
// Logoyu geçici canvas'a çiz
tempCtx.drawImage(this.logoImage, 0, 0);
// Renk filtresi uygula
tempCtx.globalCompositeOperation = 'source-in';
tempCtx.fillStyle = this.currentColor;
tempCtx.fillRect(0, 0, tempCanvas.width, tempCanvas.height);
// Filtrelenmiş canvas'ı döndür
return tempCanvas;
}
resetSettings() {
// Ayarları sıfırla
this.currentColor = '#ffffff';
this.currentOpacity = 0.5;
this.currentSize = 0.6;
this.currentPosition = 0.04;
// UI'ı güncelle
document.querySelectorAll('.color-option').forEach(opt => opt.classList.remove('active'));
document.querySelector('.color-option[data-color="#ffffff"]').classList.add('active');
document.getElementById('opacitySlider').value = 0.5;
document.getElementById('opacityValue').textContent = '50%';
document.getElementById('sizeSlider').value = 0.6;
document.getElementById('sizeValue').textContent = '60%';
document.getElementById('positionSlider').value = 0.04;
document.getElementById('positionValue').textContent = '4%';
document.getElementById('customColorPicker').value = '#ffffff';
// Watermark'ı güncelle
this.updateWatermark();
}
async downloadImage() {
if (!this.canvas) return;
try {
// Canvas rendering kalitesini maksimuma çıkar
this.ctx.imageSmoothingEnabled = true;
this.ctx.imageSmoothingQuality = 'high';
// Canvas'ı en yüksek kalitede PNG olarak al (kalite kaybı olmasın)
const imageData = this.canvas.toDataURL('image/png');
const result = await ipcRenderer.invoke('save-image', imageData, this.originalFileName);
if (result.success) {
alert(`Görsel başarıyla kaydedildi!\nOrijinal kalitede: ${this.originalWidth}x${this.originalHeight}\nKonum: ${result.path}`);
} else {
alert(`Hata: ${result.error}`);
}
} catch (error) {
console.error('İndirme hatası:', error);
alert('Görsel indirilirken hata oluştu!');
}
}
showQuickModeNotification() {
// Geçici bildirim göster
const notification = document.createElement('div');
notification.className = 'quick-mode-notification';
notification.innerHTML = '⚡ Hızlı Çıktı Modu Aktif! Görsel seçer seçmez otomatik işlenecek.';
document.body.appendChild(notification);
setTimeout(() => {
notification.remove();
}, 3000);
}
async processQuickMode() {
try {
// Varsayılan ayarlarla watermark ekle
this.updateWatermark();
// Canvas'ı yüksek kalitede al
this.ctx.imageSmoothingEnabled = true;
this.ctx.imageSmoothingQuality = 'high';
const imageData = this.canvas.toDataURL('image/png');
// Masaüstüne hızlı kaydet
const result = await ipcRenderer.invoke('quick-save-image', imageData, this.originalFileName);
if (result.success) {
// Başarı bildirimi göster
this.showSuccessNotification(result.path);
} else {
alert(`Hata: ${result.error}`);
}
} catch (error) {
console.error('Hızlı işleme hatası:', error);
alert('Hızlı işleme sırasında hata oluştu!');
}
}
showSuccessNotification(filePath) {
const notification = document.createElement('div');
notification.className = 'success-notification';
notification.innerHTML = `
<div class="success-icon">✅</div>
<div class="success-text">
<strong>Başarılı!</strong><br>
Fligranlanmış görsel masaüstüne kaydedildi
</div>
`;
document.body.appendChild(notification);
setTimeout(() => {
notification.remove();
}, 4000);
}
}
// Uygulama başlat
document.addEventListener('DOMContentLoaded', () => {
new WatermarkApp();
});