Skip to content

Commit 01cbc81

Browse files
authored
Merge pull request #5 from MikiDi/master
Implement adding Multimarkers & NFT-markers
2 parents 465c054 + 9101039 commit 01cbc81

File tree

4 files changed

+87
-12
lines changed

4 files changed

+87
-12
lines changed

dist/ARToolkit.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/ARController.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,17 +58,17 @@ export default class ARController {
5858
if(this.options.canvas) {
5959
// in case you use Node.js, create a canvas with node-canvas
6060
this.canvas = this.options.canvas;
61-
} else {
61+
} else if(typeof document !== 'undefined') {
6262
// try creating a canvas from document
63-
if(typeof document === 'undefined') {
64-
throw 'No canvas available';
65-
}
6663
this.canvas = document.createElement('canvas');
6764
}
68-
69-
this.canvas.width = width;
70-
this.canvas.height = height;
71-
this.ctx = this.canvas.getContext('2d');
65+
if(this.canvas) {
66+
this.canvas.width = width;
67+
this.canvas.height = height;
68+
this.ctx = this.canvas.getContext('2d');
69+
} else {
70+
console.warn('No canvas available');
71+
}
7272

7373
// this is to workaround the introduction of "self" variable
7474
this.nftMarkerFound = false;

src/ARToolkit.js

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,12 +123,45 @@ export default class ARToolkit {
123123
return this.instance._addMarker(arId, target);
124124
}
125125

126-
addMultiMarker() {
126+
async addMultiMarker(arId, url) {
127127

128+
const target = '/multi_marker_' + this.multiMarkerCount++;
129+
130+
const data = await Utils.fetchRemoteData(url);
131+
const files = Utils.parseMultiFile(data);
132+
133+
const storeMarker = async function (file) {
134+
const markerUrl = (new URL(file, url)).toString();
135+
const data = await Utils.fetchRemoteData(markerUrl);
136+
this._storeDataFile(data, file);
137+
};
138+
139+
const promises = files.map(storeMarker, this);
140+
await Promise.all(promises);
141+
142+
const markerId = this.instance._addMultiMarker(arId, target);
143+
const markerNum = this.instance.getMultiMarkerNum(arId, markerId);
144+
145+
return [markerId, markerNum];
128146
}
129147

130-
addNFTMarker() {
148+
async addNFTMarker(arId, url) {
149+
// url doesn't need to be a valid url. Extensions to make it valid will be added here
150+
const targetPrefix = '/markerNFT_' + this.markerCount++;
151+
const extensions = ['fset', 'iset', 'fset3'];
152+
153+
const storeMarker = async function (ext) {
154+
const fullUrl = url + '.' + ext;
155+
const target = targetPrefix + '.' + ext;
156+
const data = await Utils.fetchRemoteData(fullUrl);
157+
this._storeDataFile(data, target);
158+
};
131159

160+
const promises = extensions.map(storeMarker, this);
161+
await Promise.all(promises);
162+
163+
// return the internal marker ID
164+
return this.instance._addNFTMarker(arId, targetPrefix);
132165
}
133166
//----------------------------------------------------------------------------
134167

src/Utils.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,46 @@ export default class Utils {
1919
}
2020
return data;
2121
}
22+
23+
static uint8Data2String(uint8Data) {
24+
return String.fromCharCode.apply(String, uint8Data);
25+
}
26+
27+
static parseMultiFile(bytes) {
28+
// Parse a multi-marker file to an array of file-paths
29+
const str = Utils.uint8Data2String(bytes);
30+
31+
const lines = str.split('\n');
32+
33+
const files = [];
34+
35+
let state = 0; // 0 - read,
36+
let markers = 0;
37+
38+
lines.forEach(function (line) {
39+
line = line.trim();
40+
if (!line || line.startsWith('#')) return; // FIXME: Should probably be `if (line.indexOf('#') === 0) { return; }`
41+
42+
switch (state) {
43+
case 0:
44+
markers = +line;
45+
state = 1;
46+
return;
47+
case 1: // filename or barcode
48+
if (!line.match(/^\d+$/)) {
49+
files.push(line);
50+
}
51+
case 2: // width
52+
case 3: // matrices
53+
case 4:
54+
state++;
55+
return;
56+
case 5:
57+
state = 1;
58+
return;
59+
}
60+
});
61+
62+
return files;
63+
}
2264
}

0 commit comments

Comments
 (0)