-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProjection.ts
82 lines (69 loc) · 2.08 KB
/
Projection.ts
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
import { defaultClassPrefix, Offset, Size } from './common';
export type ProjectionOptions = {
classPrefix?: string;
imageUrl: string;
position?: Offset;
size?: Size;
tagName?: string;
};
export class Projection {
$el: HTMLElement | null;
$image: HTMLImageElement;
readonly className: string;
readonly width: number;
readonly height: number;
static readonly elClassName = 'projection';
static readonly defaultOptions = {
classPrefix: defaultClassPrefix,
size: {
height: 0,
width: 0,
},
tagName: 'div',
};
constructor(inpOptions: ProjectionOptions, glbl = window) {
const options = { ...Projection.defaultOptions, ...inpOptions };
this.className = `${options.classPrefix}${Projection.elClassName}`;
this.$el = glbl.document.createElement(options.tagName);
this.$el.classList.add(this.className);
if (options.position) {
this.$el.style.left = options.position.left.toString();
this.$el.style.top = options.position.top.toString();
}
this.$image = new Image();
this.$image.src = options.imageUrl;
this.width = options.size.width;
this.height = options.size.height;
this.$image.onload = () => {
if (this.$el) {
this.$el.style['background-image'] = `url('${this.$image.src}')`;
this.$el.style['background-repeat'] = 'no-repeat';
const event = new glbl.CustomEvent('ip.projection.imageLoaded', { bubbles: true });
this.$el?.dispatchEvent(event);
}
};
}
setImagePosition(position: Offset) {
if (this.$el) {
this.$el.style['background-position'] = `${position.left}px ${position.top}px`;
}
}
show() {
if (this.$el) {
this.$el.classList.toggle(this.className + '--visible');
this.$el.style.height = this.height.toString();
this.$el.style.width = this.width.toString();
}
}
hide() {
if (this.$el) {
this.$el.classList.toggle(this.className + '--visible');
this.$el.style.height = '0';
this.$el.style.width = '0';
}
}
destroy() {
this.$el?.remove();
this.$el = null;
}
}