-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
arcode.html
291 lines (246 loc) · 11.6 KB
/
arcode.html
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
<!DOCTYPE html>
<html>
<head>
<!--Import Google Icon Font-->
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<!--Import materialize.css-->
<link type="text/css" rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.98.1/css/materialize.min.css" media="screen,projection"/>
<!--Let browser know website is optimized for mobile-->
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<!-- https://davidshimjs.github.io/qrcodejs/ -->
<script src='https://raw.githack.com/davidshimjs/qrcodejs/gh-pages/qrcode.min.js'></script>
<!-- Include pdfMake - http://pdfmake.org/ -->
<script src='vendor/pdfMake/pdfmake.min.js'></script>
<script src='vendor/pdfMake/vfs_fonts.js'></script>
</head>
<body>
<div class="row" id='row-ui' >
<div class="col s12">
<a class="waves-effect waves-light btn" onclick='hideUI()'><i class="material-icons right">reorder</i>Hide</a>
<h1 class='center-align'>AR-Code Generator</h1>
</div>
<div class="col s12">
<input id="urlQrCode" type="text">
</div>
<div class="input-field col s3">
<input type="checkbox" id="saveInUrl" />
<label for="saveInUrl">Save in url</label>
<input type="checkbox" id="hideUiEnabled" type="hidden"/>
</div>
<div class="input-field col s4 offset-s4">
<a href='https://medium.com/arjs/ar-code-a-fast-path-to-augmented-reality-60e51be3cbdf' target='_blank' class="waves-effect waves-light btn-large">
<i class="material-icons right">info</i>
Info
</a>
<a onclick='generatePdf()' class="waves-effect waves-light btn-large" title='Generate a PDF instruction page'>Pdf</a>
</div>
</div>
<div class="row">
<div class="col s2">
<a class="waves-effect waves-light btn" onclick='showUI()' id='btnShowUI' style='display:none;'><i class="material-icons right">reorder</i>Show</a>
</div>
<div class="col s10"></div>
<div class="col s12">
<div class="valign-wrapper">
<div class="valign center" style="width: 100%;" id='arcode-container' ></div>
</div>
</div>
</div>
</div>
<!--Import jQuery before materialize.js-->
<script type="text/javascript" src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.98.1/js/materialize.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
Materialize.updateTextFields();
});
</script>
<script>
function hideUI(){
document.querySelector('#hideUiEnabled').checked = true
updateARCode()
}
function showUI(){
document.querySelector('#hideUiEnabled').checked = false
updateARCode()
}
document.querySelector('#urlQrCode').addEventListener('change', updateARCode)
document.querySelector('#saveInUrl').addEventListener('change', updateARCode)
//////////////////////////////////////////////////////////////////////////////
// PDF Generation
//////////////////////////////////////////////////////////////////////////////
function generatePdf(){
console.log('generate PDF')
var urlQrCode = document.querySelector('#urlQrCode').value
var docDefinition = {
header: [
{
text: 'AR.js by @jerome_etienne - https://github.com/jeromeetienne/ar.js',
margin: [0, 0],
alignment: 'center',
},
],
content: [
{
image: canvas.toDataURL(),
width: 320,
alignment: 'center',
},
{
text: [
{
text: '\nAR-CODE ',
fontSize: 30,
bold: true,
},
{
text: 'for',
fontSize: 20,
bold: false,
}
],
alignment: 'center',
},
{
text: urlQrCode,
alignment: 'center',
margin: [0, 10],
},
{
text: 'How to Use an AR-Code ?',
fontSize: 24,
margin: [0, 20],
},
{
text: 'Step 1 - Get a Phone',
bold: true,
fontSize: 15,
},
{
text: [
'It works on any browser with WebGL and WebRTC. So android works. Windows mobile works. ',
'IOS doesn\'t work unfortunately. IOS safari doesn\'t support WebRTC at the moment.',
' Apple is currently working on it tho. Let\'s hope they join the party soon!'
],
margin: [0, 10, 0 , 30],
},
{
text: 'Step 2 - Scan the QR-Code',
bold: true,
fontSize: 15,
},
{
text: [
'It will open your web browser to an augmented reality page. ',
'It will open the url from the qr-code to some AR.js content. ',
'Then your phone will use the camera to find out the position ',
'of the marker and display 3d content on top of it. ',
],
margin: [0, 10, 0 , 30],
},
{
text: 'Step 3 - Point the Phone at the Hiro Marker',
bold: true,
fontSize: 15,
},
{
text: [
'You are ',
{ text: 'DONE', bold: true, },
'! Enjoy the Augmented Reality :)',
],
margin: [0, 10, 0 , 30],
},
],
}
pdfMake.createPdf(docDefinition).open();
// pdfMake.createPdf(docDefinition).download('optionalName.pdf');
}
//////////////////////////////////////////////////////////////////////////////
// build canvas
//////////////////////////////////////////////////////////////////////////////
var canvas = document.createElement('canvas');
document.querySelector('#arcode-container').appendChild(canvas)
canvas.width = 1024;
canvas.height = 1024;
canvas.style.width = '512px';
canvas.style.height = '512px';
var context = canvas.getContext('2d')
var hiroImage = new Image;
hiroImage.onload = function() {
console.log('hiro image loaded')
if( location.hash.substr(1) !== '' ){
var parameters = JSON.parse(decodeURIComponent(location.hash.substr(1)))
document.querySelector('#urlQrCode').value = parameters.urlQrCode
document.querySelector('#hideUiEnabled').checked = parameters.hideUiEnabled
document.querySelector('#saveInUrl').checked = true
// debugger;
}else{
document.querySelector('#urlQrCode').value = 'https://jeromeetienne.github.io/AR.js/three.js/examples/mobile-performance.html'
}
updateARCode()
}
hiroImage.src = '../../data/images/hiro.png';
function updateARCode(){
var urlQrCode = document.querySelector('#urlQrCode').value
var hideUiEnabled = document.querySelector('#hideUiEnabled').checked
var saveInUrl = document.querySelector('#saveInUrl').checked
var parameters = {
urlQrCode : urlQrCode,
hideUiEnabled: hideUiEnabled,
}
// generate the ar-code canvas
generateArCodeCanvas(canvas, urlQrCode, function onReady(){
console.log('ar-code generated for', urlQrCode)
})
if( hideUiEnabled === true ){
document.querySelector('#row-ui').style.display = 'none'
document.querySelector('#btnShowUI').style.display = 'block'
}else{
document.querySelector('#row-ui').style.display = 'block'
document.querySelector('#btnShowUI').style.display = 'none'
}
//////////////////////////////////////////////////////////////////////////////
// update location.hash if suitable
//////////////////////////////////////////////////////////////////////////////
if( saveInUrl === true ){
location.hash = '#' + encodeURIComponent(JSON.stringify(parameters))
}else{
// magic to remove the old location.hash
history.pushState("", document.title, location.pathname + location.search);
}
}
//////////////////////////////////////////////////////////////////////////////
// Code Separator
//////////////////////////////////////////////////////////////////////////////
/**
* Generate AR-Code
*/
function generateArCodeCanvas(canvas, text, onLoad){
var context = canvas.getContext('2d')
context.drawImage(hiroImage, 0, 0, canvas.width, canvas.height);
generateQrCodeImage(text, function onLoaded(qrCodeImage){
console.log('qrcode generated')
context.drawImage(qrCodeImage,canvas.width*0.50,canvas.height*0.30,canvas.width*0.20, canvas.height*0.20);
onLoad && onLoad()
})
}
/**
* Generate AR-Code
*/
function generateQrCodeImage(text, onLoaded){
var container = document.createElement('div')
var qrcode = new QRCode(container, {
text: text,
width: 256,
height: 256,
colorDark : '#000000',
colorLight : '#ffffff',
// correctLevel : QRCode.CorrectLevel.H
});
var qrCodeImage = container.querySelector('img')
qrCodeImage.addEventListener('load', function(){
onLoaded(qrCodeImage)
})
}
</script></body></html>