As the AR.js framework brings augmented reality to web; I wanted to spend my week of Innovation Time developing an AR web app to make the tech more accessible and bring digital objects off the screen and into the real world.
The project is open-source and with nearly 13,000 stars on the AR.js GitHub repository, it’s being used by developers of all kinds to create more innovative digital experiences.
The AR.js framework includes cross-browser compatibility and supports both WebGL and WebRTC, meaning it’s functional across Android and iPhone devices above iOS 11.
Wrapping a number of different AR frameworks including three.js, a-frame and ARToolKit, AR.js makes it incredibly simple and easy to bring AR to web apps.
With five days to prove the concept that augmented reality can be used across website and PWA (progressive web app) development, I decided to set my goals from the get-go.
This meant breaking up my week into achievable outputs and aiming to have a working prototype by the end of Friday, getting myself used to the software and creating a scalable framework that could be used for both Cube and our clients.
The prototype needed to be a simple AR platform that utilised the AR.js language, with a little Cube personalisation that would give me the knowledge and experience needed to implement augmented reality in websites and PWAs.
One of the limitations of using AR.js is that you have to use a marker image.
They work kinda like a QR code, you scan the marker image and the web application will display the 3D model on top of it. AR.js makes it possible to customise the marker image though by uploading an image of your choice to the marker training website.
My experience during this week is that AR.js is best to use simple, monochrome images such as logos or letters as it makes it easier to detect; in this instance, I used the 3 Sided Cube logo.
I’d recommend keeping this simple for your first attempt and stick to the <a-box> shown in the sample code. You can customise it by changing things such as the colour and opacity.
Once you have got to grips with implementing AR, you can come back and change this model. You could do this by either downloading a pre-made model from a website such as Sketchfab or if you are feeling creative, try creating your own 3D model.
It is surprisingly simple to get the example code up and running. You just to include the libraries and add the sample code.
Here are the scripts that you will need to include:
Here is an example code that will use the default marker image to display a pink cube:
<a-scene embedded arjs=’sourceType: webcam; debugUIEnabled: false;’>
<a-box position=’0 1 0′ material=’opacity: 0.5; color: #F70087;’></a-box>
If you have chosen to use a custom marker, you will need to edit the above code a bit so that it is using a custom preset rather than the hiro one, give it a type of pattern and add a URL with points to the marker .patt file created above, like so…
<a-marker preset=’custom’ type=’pattern’ url=’/PATH/TO/YOUR/MARKER.patt’>
One thing to bear in mind is that the <a-scene> code must be added straight after the body tag – it will not work if it is added inside any other elements such as a <div>.
This works best over https, so you will either need to use a service like ngrok to create a tunnel to test your local code, deploy your code somewhere or even use CodePen. When you look at the frontend, it should access your camera. If you point the camera at the marker image, it should display the cube.
The main limitation of using AR.js is that the framework functions around marker images; meaning that any image you want to bring into the real world with augmented reality needs to first be created and included in your code.
There are alternatives that look promising in this area, such as the markerless AR platform Argon.js, the limitation here being that it only works through the Argon browser (a much larger drawback).
The platform that caught my attention the most, however, is the WebXR Device API; which accommodates for markerless AR and even utilises a range of different hardware including sensors and head-mounted displays.
In my future weeks of Innovation Time, I’ll definitely be exploring this platform more and seeing the different kinds of AR platform I’m able to develop with it.
Published on May 2, 2019, last updated on May 19, 2023