-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
146 lines (125 loc) · 4.67 KB
/
script.js
File metadata and controls
146 lines (125 loc) · 4.67 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
const textInput = document.getElementById('textInput');
const fontSizeInput = document.getElementById('fontSizeInput');
const yPositionInput = document.getElementById('yPositionInput');
const resetBtn = document.getElementById('resetBtn');
const download16x9 = document.getElementById('download16x9');
const download4x3 = document.getElementById('download4x3');
const svgText = document.getElementById('text_yle_tv2');
const defaults = {
text: 'YLE TV2',
size: 41,
y: 325
};
function updateSvg() {
svgText.textContent = textInput.value;
svgText.style.fontSize = fontSizeInput.value + 'px';
svgText.setAttribute('y', yPositionInput.value);
}
function saveToUrl() {
const params = new URLSearchParams();
params.set('text', textInput.value);
params.set('size', fontSizeInput.value);
params.set('y', yPositionInput.value);
history.replaceState(null, '', '?' + params.toString());
}
function loadFromUrl() {
const params = new URLSearchParams(window.location.search);
textInput.value = params.get('text') || defaults.text;
fontSizeInput.value = params.get('size') || defaults.size;
yPositionInput.value = params.get('y') || defaults.y;
updateSvg();
}
function reset() {
textInput.value = defaults.text;
fontSizeInput.value = defaults.size;
yPositionInput.value = defaults.y;
updateSvg();
saveToUrl();
}
function downloadPng(width, height, filename, fillMode) {
const svgEl = document.querySelector('svg');
const svgData = new XMLSerializer().serializeToString(svgEl);
const svgBlob = new Blob([svgData], { type: 'image/svg+xml;charset=utf-8' });
const url = URL.createObjectURL(svgBlob);
const img = new Image();
img.onload = function() {
const canvas = document.createElement('canvas');
canvas.width = width;
canvas.height = height;
const ctx = canvas.getContext('2d');
// Fill with black background (in case SVG has transparency)
ctx.fillStyle = '#000000';
ctx.fillRect(0, 0, width, height);
// Calculate scaling
const svgAspect = 768 / 576; // 4:3
const targetAspect = width / height;
let drawWidth, drawHeight, offsetX, offsetY;
if (fillMode === 'fill') {
// Fill mode: zoom to fill entire canvas, crop edges
if (targetAspect > svgAspect) {
// Target is wider - fit by width, crop top/bottom
drawWidth = width;
drawHeight = width / svgAspect;
offsetX = 0;
offsetY = (height - drawHeight) / 2;
} else {
// Target is taller - fit by height, crop sides
drawHeight = height;
drawWidth = height * svgAspect;
offsetX = (width - drawWidth) / 2;
offsetY = 0;
}
} else {
// Fit mode: fit entire image, letterbox if needed
if (targetAspect > svgAspect) {
// Target is wider - fit by height, center horizontally
drawHeight = height;
drawWidth = height * svgAspect;
offsetX = (width - drawWidth) / 2;
offsetY = 0;
} else {
// Target is taller - fit by width, center vertically
drawWidth = width;
drawHeight = width / svgAspect;
offsetX = 0;
offsetY = (height - drawHeight) / 2;
}
}
ctx.drawImage(img, offsetX, offsetY, drawWidth, drawHeight);
URL.revokeObjectURL(url);
canvas.toBlob(function(blob) {
const link = document.createElement('a');
link.download = filename;
link.href = URL.createObjectURL(blob);
link.click();
URL.revokeObjectURL(link.href);
}, 'image/png');
};
img.src = url;
}
textInput.addEventListener('keyup', function() {
updateSvg();
saveToUrl();
});
fontSizeInput.addEventListener('input', function() {
updateSvg();
saveToUrl();
});
yPositionInput.addEventListener('input', function() {
updateSvg();
saveToUrl();
});
resetBtn.addEventListener('click', reset);
// Download buttons: 4K resolutions
// 4:3 = 2880x2160 (same height as 4K)
// 16:9 = 3840x2160 (zoomed/cropped to fill)
download4x3.addEventListener('click', function() {
const text = textInput.value.replace(/\s+/g, '');
downloadPng(2880, 2160, 'testikuva-' + text + '-4x3.png', 'fit');
});
download16x9.addEventListener('click', function() {
const text = textInput.value.replace(/\s+/g, '');
downloadPng(3840, 2160, 'testikuva-' + text + '-16x9.png', 'fill');
});
// Load state from URL on page load
loadFromUrl();