|
@@ -0,0 +1,138 @@
|
|
1
|
+/**
|
|
2
|
+ * A-Frame scan gimmick for encantar.js
|
|
3
|
+ * @author Alexandre Martins <alemartf(at)gmail.com> (https://github.com/alemart/encantar-js)
|
|
4
|
+ * @license LGPL-3.0-or-later
|
|
5
|
+ */
|
|
6
|
+
|
|
7
|
+(function() {
|
|
8
|
+
|
|
9
|
+const DEFAULT_IMAGE = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAAVklEQVRYR+2WMQ4AIAgD4f+PVlzFYGSpw3UlQHOaBjexXLzfMJAIjFD1LB6q6q/9RwO3Jd1/s8ztszEAAQiQhBCAAARIQghA4E8C0rO8e3J3+4hiOYEJMwaAIT1kBDMAAAAASUVORK5CYII=';
|
|
10
|
+
|
|
11
|
+AFRAME.registerComponent('ar-scan-gimmick', {
|
|
12
|
+
|
|
13
|
+ schema: {
|
|
14
|
+
|
|
15
|
+ /** URL of an image */
|
|
16
|
+ 'src': { type: 'string', default: DEFAULT_IMAGE },
|
|
17
|
+
|
|
18
|
+ /** opacity of the image */
|
|
19
|
+ 'opacity': { type: 'number', default: 1.0 }
|
|
20
|
+
|
|
21
|
+ },
|
|
22
|
+
|
|
23
|
+ init()
|
|
24
|
+ {
|
|
25
|
+ const scene = this.el.sceneEl;
|
|
26
|
+ const ar = scene.systems.ar;
|
|
27
|
+
|
|
28
|
+ this._ar = ar;
|
|
29
|
+ this._img = null;
|
|
30
|
+ this._hadGizmos = false;
|
|
31
|
+ this._onTargetFound = this._onTargetFound.bind(this);
|
|
32
|
+ this._onTargetLost = this._onTargetLost.bind(this);
|
|
33
|
+
|
|
34
|
+ scene.addEventListener('ar-started', () => {
|
|
35
|
+
|
|
36
|
+ this._validate();
|
|
37
|
+
|
|
38
|
+ const session = ar.session;
|
|
39
|
+ this._hadGizmos = session.gizmos.visible;
|
|
40
|
+
|
|
41
|
+ const img = this._createImage();
|
|
42
|
+ this.el.parentNode.appendChild(img);
|
|
43
|
+ this._img = img;
|
|
44
|
+
|
|
45
|
+ this._registerEvents();
|
|
46
|
+
|
|
47
|
+ });
|
|
48
|
+ },
|
|
49
|
+
|
|
50
|
+ remove()
|
|
51
|
+ {
|
|
52
|
+ if(this._img === null);
|
|
53
|
+ return;
|
|
54
|
+
|
|
55
|
+ this._unregisterEvents();
|
|
56
|
+
|
|
57
|
+ this.el.parentNode.removeChild(this._img);
|
|
58
|
+ this._img = null;
|
|
59
|
+ },
|
|
60
|
+
|
|
61
|
+ _registerEvents()
|
|
62
|
+ {
|
|
63
|
+ const imageTracker = this._findImageTracker();
|
|
64
|
+
|
|
65
|
+ if(imageTracker !== null) {
|
|
66
|
+ imageTracker.addEventListener('targetfound', this._onTargetFound);
|
|
67
|
+ imageTracker.addEventListener('targetlost', this._onTargetLost);
|
|
68
|
+ }
|
|
69
|
+ },
|
|
70
|
+
|
|
71
|
+ _unregisterEvents()
|
|
72
|
+ {
|
|
73
|
+ const imageTracker = this._findImageTracker();
|
|
74
|
+
|
|
75
|
+ if(imageTracker !== null) {
|
|
76
|
+ imageTracker.removeEventListener('targetlost', this._onTargetLost);
|
|
77
|
+ imageTracker.removeEventListener('targetfound', this._onTargetFound);
|
|
78
|
+ }
|
|
79
|
+ },
|
|
80
|
+
|
|
81
|
+ _onTargetFound(event)
|
|
82
|
+ {
|
|
83
|
+ const ar = this._ar;
|
|
84
|
+ const img = this._img;
|
|
85
|
+
|
|
86
|
+ ar.session.gizmos.visible = false;
|
|
87
|
+ img.style.display = 'none';
|
|
88
|
+ },
|
|
89
|
+
|
|
90
|
+ _onTargetLost(event)
|
|
91
|
+ {
|
|
92
|
+ const ar = this._ar;
|
|
93
|
+ const img = this._img;
|
|
94
|
+
|
|
95
|
+ ar.session.gizmos.visible = this._hadGizmos;
|
|
96
|
+ img.style.display = 'inline-block';
|
|
97
|
+ },
|
|
98
|
+
|
|
99
|
+ _findImageTracker()
|
|
100
|
+ {
|
|
101
|
+ const ar = this._ar;
|
|
102
|
+
|
|
103
|
+ if(ar !== null) {
|
|
104
|
+ for(const tracker of ar.session.trackers) {
|
|
105
|
+ if(tracker.type == 'image-tracker')
|
|
106
|
+ return tracker;
|
|
107
|
+ }
|
|
108
|
+ }
|
|
109
|
+
|
|
110
|
+ return null;
|
|
111
|
+ },
|
|
112
|
+
|
|
113
|
+ _createImage()
|
|
114
|
+ {
|
|
115
|
+ const img = document.createElement('img');
|
|
116
|
+
|
|
117
|
+ img.src = this.data.src;
|
|
118
|
+ img.style.width = '100%';
|
|
119
|
+ img.style.height = '100%';
|
|
120
|
+ img.style.objectFit = 'contain';
|
|
121
|
+ img.style.display = 'inline-block';
|
|
122
|
+ img.style.opacity = this.data.opacity;
|
|
123
|
+
|
|
124
|
+ if(img.src == DEFAULT_IMAGE)
|
|
125
|
+ img.style.imageRendering = 'pixelated';
|
|
126
|
+
|
|
127
|
+ return img;
|
|
128
|
+ },
|
|
129
|
+
|
|
130
|
+ _validate()
|
|
131
|
+ {
|
|
132
|
+ if(!this.el.parentNode.getAttribute('ar-hud'))
|
|
133
|
+ throw new Error('a-entity with ar-scan-gimmick must be a direct child of ar-hud');
|
|
134
|
+ },
|
|
135
|
+
|
|
136
|
+});
|
|
137
|
+
|
|
138
|
+})();
|