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
|
import Animation from './animation.js'
import config from './config'
export default class Shadow {
isOpen = false;
/**
* ============
* Constructors
* ============
*/
constructor(image) {
this.original = image;
const rect = this.original.getBoundingClientRect();
this.originalPosition = {
top: rect.top + window.scrollY + config.offset.y,
left: rect.left + window.scrollX + config.offset.x,
width: rect.width,
height: rect.height
}
this.create(image);
return this;
}
create(image) {
// create shadow image
const shadow = document.createElement('img');
shadow.src = image.src;
shadow.id = `libra-shadow-${image.id}`;
shadow.classList.add('libra-shadow');
// style and position shadow image
shadow.style.top = this.originalPosition.top + 'px';
shadow.style.left = this.originalPosition.left + 'px';
shadow.style.width = this.originalPosition.width + 'px';
shadow.style.height = this.originalPosition.height + 'px';
// Set transition duration from config
shadow.style.transitionDuration = config.transitionDuration + 'ms';
document.body.appendChild(shadow);
this.element = shadow;
}
/**
* ============
* Actions
* ============
*/
updateOriginalPosition() {
const rect = this.original.getBoundingClientRect();
this.originalPosition = {
top: rect.top + window.scrollY + config.offset.y,
left: rect.left + window.scrollX + config.offset.x,
width: rect.width,
height: rect.height,
};
}
open() {
this.isOpen = true;
this.element.classList.add('open');
// Update original position before animation in case viewport changed
this.updateOriginalPosition();
this.element.style.top = `${this.originalPosition.top}px`;
this.element.style.left = `${this.originalPosition.left}px`;
this.element.style.width = `${this.originalPosition.width}px`;
this.element.style.height = `${this.originalPosition.height}px`;
this.original.style.visibility = 'hidden';
if (!document.body.contains(this.element)) this.placeItself();
// play animation
const animate = () =>
Animation.transformMatrix(
this.element,
this.originalPosition,
Animation.calculateFinalState(this.element),
);
if (this.element.naturalWidth === 0 || this.element.naturalHeight === 0) {
// Wait for image to load before animating
this.element.addEventListener('load', animate, { once: true });
} else {
// animate directly if loaded
animate();
}
}
close() {
this.isOpen = false;
this.element.classList.remove('open');
Animation.resetTransformMatrix(this.element);
// Wait for animation to complete before showing original and destroying shadow
setTimeout(() => {
this.original.style.visibility = 'visible';
this.destroyItself();
}, config.transitionDuration);
}
placeItself() {
document.body.appendChild(this.element);
}
destroyItself() {
if (!document.body.contains(this.element)) return;
document.body.removeChild(this.element);
}
}
|