-
Notifications
You must be signed in to change notification settings - Fork 0
/
create-placeholder-image.js
56 lines (48 loc) · 1.94 KB
/
create-placeholder-image.js
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
(function () {
const form = document.querySelector('#form-placeholder-dimensions');
const downloadCanvasButton = document.querySelector('#download-canvas-button');
const widthInput = form.width;
const heightInput = form.height;
const colorPickerInput = form['color-picker'];
let canvas = document.querySelector('#canvas');
let context = canvas.getContext('2d');
canvas.width = widthInput.value;
canvas.height = heightInput.value;
context.fillStyle = colorPickerInput.value;
context.fillRect(0, 0, widthInput.value, heightInput.value);
widthInput.addEventListener('input', (ev) => {
let widthInputValue = ev.target.value;
let heightInputValue = heightInput.value;
updateCanvasDimensions(widthInputValue, heightInputValue);
});
heightInput.addEventListener('input', (ev) => {
let heightInputValue = ev.target.value;
let widthInputValue = widthInput.value;
updateCanvasDimensions(widthInputValue, heightInputValue);
});
colorPickerInput.addEventListener('input', (ev) => {
context.fillStyle = ev.target.value;
context.fillRect(0, 0, widthInput.value, heightInput.value);
});
downloadCanvasButton.addEventListener('click', downloadCanvasAsImage);
function downloadCanvasAsImage (){
let anchorTag = document.createElement('a');
let downloadedFileName = `placeholder-${widthInput.value}x${heightInput.value}.png`;
anchorTag.download = downloadedFileName;
anchorTag.href = canvas.toDataURL();
anchorTag.click();
}
function updateCanvasDimensions (width, height) {
canvas.width = width;
canvas.height = height;
context.fillStyle = colorPickerInput.value;
context.fillRect(0, 0, width, height);
}
})();
(function showAndHideOverlay() {
const editButton = document.querySelector('#edit-button');
const overlay = document.querySelector('#form-placeholder-dimensions');
editButton.addEventListener('click', function () {
overlay.classList.toggle('hide');
});
}());