-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript.js
More file actions
90 lines (71 loc) · 2.54 KB
/
script.js
File metadata and controls
90 lines (71 loc) · 2.54 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
function initImageUpload(box) {
let uploadField = box.querySelector('.image-upload');
uploadField.addEventListener('change', getFile);
function getFile(e){
let file = e.currentTarget.files[0];
checkType(file);
}
function previewImage(file){
let thumb = box.querySelector('.js--image-preview'),
reader = new FileReader();
reader.onload = function() {
thumb.style.backgroundImage = 'url(' + reader.result + ')';
}
reader.readAsDataURL(file);
thumb.className += ' js--no-default';
}
function checkType(file){
let imageType = /image.*/;
if (!file.type.match(imageType)) {
throw 'Datei ist kein Bild';
} else if (!file){
throw 'Kein Bild gewählt';
} else {
previewImage(file);
}
}
}
// initialize box-scope
var boxes = document.querySelectorAll('.box');
for (let i = 0; i < boxes.length; i++) {
let box = boxes[i];
initDropEffect(box);
initImageUpload(box);
}
/// drop-effect
function initDropEffect(box){
let area, drop, areaWidth, areaHeight, maxDistance, dropWidth, dropHeight, x, y;
// get clickable area for drop effect
area = box.querySelector('.js--image-preview');
area.addEventListener('click', fireRipple);
function fireRipple(e){
area = e.currentTarget
// create drop
if(!drop){
drop = document.createElement('span');
drop.className = 'drop';
this.appendChild(drop);
}
// reset animate class
drop.className = 'drop';
// calculate dimensions of area (longest side)
areaWidth = getComputedStyle(this, null).getPropertyValue("width");
areaHeight = getComputedStyle(this, null).getPropertyValue("height");
maxDistance = Math.max(parseInt(areaWidth, 10), parseInt(areaHeight, 10));
// set drop dimensions to fill area
drop.style.width = maxDistance + 'px';
drop.style.height = maxDistance + 'px';
// calculate dimensions of drop
dropWidth = getComputedStyle(this, null).getPropertyValue("width");
dropHeight = getComputedStyle(this, null).getPropertyValue("height");
// calculate relative coordinates of click
// logic: click coordinates relative to page - parent's position relative to page - half of self height/width to make it controllable from the center
x = e.pageX - this.offsetLeft - (parseInt(dropWidth, 10)/2);
y = e.pageY - this.offsetTop - (parseInt(dropHeight, 10)/2) - 30;
// position drop and animate
drop.style.top = y + 'px';
drop.style.left = x + 'px';
drop.className += ' animate';
e.stopPropagation();
}
}