-
Notifications
You must be signed in to change notification settings - Fork 2
/
script.js
88 lines (67 loc) · 2.51 KB
/
script.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
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
var file
var filename
var canvas
var context
const dropArea = document.querySelector(".container")
const button = dropArea.querySelector("button")
const input = dropArea.querySelector("input")
async function main() {
const image = document.getElementById('SourceImage')
canvas = document.getElementById('Canvas')
context = canvas.getContext('2d')
canvas.width = image.width
canvas.height = image.height
context.drawImage(image, 0, 0)
var imageData = context.getImageData(0, 0, canvas.width, canvas.height)
// apply filters loaded from filter.js file
global_canvas_filters.forEach(filter => filter(imageData.data))
context.putImageData(imageData, 0, 0)
var imageAspectRatio = canvas.width / canvas.height
var documentAspectRatio = document.documentElement.clientWidth / document.documentElement.clientHeight
if (imageAspectRatio < documentAspectRatio) {
canvas.style.height = "90vh"
} else {
canvas.style.width = "95vw"
}
document.querySelector('.card').classList.remove('card-picker')
document.getElementById("upload-text").style.display = "none"
document.getElementById("canvas-container").style.display = "block"
document.getElementById("back-on").style.display = "block"
}
function loadImage(file) {
const image = document.getElementById('SourceImage')
image.src = URL.createObjectURL(file)
image.addEventListener('load', () => {
main()
})
}
// ----------------- PASTE FROM CLIPBOARD -----------------
document.addEventListener('DOMContentLoaded', () => {
document.onpaste = function(event) {
const items = (event.clipboardData || event.originalEvent.clipboardData).items
for (let index in items) {
if (items[index].kind === 'file') loadImage(items[index].getAsFile())
}
}
})
// ----------------- DRAG AND DROP -----------------
function dropHandler(ev) {
ev.preventDefault()
loadImage(ev.dataTransfer.files[0])
}
function dragOverHandler(ev) {
ev.preventDefault()
}
// ----------------- CHOOSE FILE BUTTON -----------------
// clicks DOM input element
button.onclick = () => {
input.click()
}
// on file select
input.addEventListener("change", function(event) {
loadImage(event.target.files[0])
})
// help button overlay
const overlay = document.getElementById('overlay')
document.getElementById('help-on').addEventListener('click', () => overlay.style.display = "block")
document.getElementById('overlay').addEventListener('click', () => overlay.style.display = "none")