Let’s create a web camera app ready for foldable devices.
foldable devices are here! these smartphones allow different form factors with just one device, which can lead to new opportunities for innovation. as developers should be able to detect what is the current posture of the device, the physical position in which a device holds, samsung is putting an effort within the w3c on making a new web standard called device posture api. this api allows web applications to request and be notified of changes in the device posture. web apps can take advantage of the new form factors and improve users’ experience. we’ve created this codelab to show you how you can take advantage of this new feature. before we start, if you would like to learn more about it in a different format, here is a video that we’ve created with more information about this api and foldables. 👉 would you like to do this codelab with mentors and in person? if you are in london we will have a first meetup to try this with real foldable devices, feel free to register here! (ed. this event has already occurred, but we're leaving the link intact.) what is the w3c and why you should care in the meantime, as i’ve mentioned before, there is a current proposal about how to take advantage of these foldable devices called ‘device posture api’. its current state is ‘draft’, which means that is still open to changes and discussion and has been published for review by the community, including w3c members, the public (you!), and other technical organizations. we’ve created a demo to show you how to use this api, hear your thoughts and teach you some capabilities of the web. at the end of this codelab you can send us your feedback here. let’s start working… 🛠️ hands on 1. set up your environment you will need the following: samsung internet latest version samsung galaxy z fold2, z fold3, z flip, or z flip3 if a physical device is not available for testing, use a) remote test lab requirements: samsung account java runtime environment (jre) 7 or later with java web start b) device posture api polyfill for testing using a non-foldable smartphone, just implement a polyfill to the project. 2. start your project open a new browser tab, go to https://glitch.com, and log in click new project, then select import from github enter the url of this repository 3. add html video markup note: take a look and check the files of your project before starting to code. in index.html, you will have two main sections, one is dedicated to the video camera and the other one to the camera controls. you can identify the video section as it contains the class video-container. complete the html markup by adding the <video> element, width, height, and id attributes. <div class="video-container"> <video id="video" width="1280" height="200" > video stream not available. </video> </div> once the video markup is added, the html file is complete. besides this, you can find: a <canvas> element into which the captured frames are stored and kept hidden since users don’t need to see it. an <img> element where the pictures will be displayed. 4. enable front and back cameras with facingmode in index.js, you'll find a startup() function, here is where you will be doing the following: initialize most values grab element references to use later set up the camera and image start by setting up the camera. you will have access to both front and back cameras by using facingmode, a domstring that indicates which camera is being used to capture images. it's always a good practice to check if this function is available for use. add the following lines of code in startup(): `let supports = navigator.mediadevices.getsupportedconstraints(); if (supports["facingmode"] === true) { flip_button.disabled = false; }` 5. link the media stream to the video element if you are able to use facingmode, the camera web app will use this option in the capture function to detect which is the current camera used and later send it to the flip button. now, the camera should be activated, insert this block of code after retrieving defaultopts.video value using facingmode: `navigator.mediadevices .getusermedia(defaultsopts) .then(_stream => { stream = _stream; video.srcobject = stream; video.play(); }) .catch(error => console.error(error));` in order to get the media stream, you call navigator.mediadevices.getusermedia() and request a video stream that returns a promise. the success callback receives a stream object as input. it is the <video> element's source to the new stream. once the stream is linked to the <video> element, start it playing by calling video.play(). it's always a good practice to include the error callback too, just in case the camera is not available or the permissions are denied. 6. take a look at the implementations in index.js at this point, the functionality of the web app is complete. before moving to the next step, let’s review the rest of the javascript code: there is an event listener video for the canplay event that will check when the video playback begins. if it's the first time running, it will set up video attributes like width and height. for the snip button, there is an event listener for click that will capture the picture. the flip button will be waiting for a click event to assign the flag about which camera is being used, front or back camera, within the variable shouldfaceuser, and initialize the camera again. clearpicture() creates an image and converts it to a format that will be displayed in the <img> element. finally, takepicture() captures the currently displayed video frame, converts it into a png file, and displays it in the captured frame box. 7. use the device posture api at this point, you should have a working prototype of a camera web app. the video camera should be displayed and a picture may be taken using the snip button. in addition, it will show a preview of the picture taken through the small preview display. the flip button allows you also to switch between front and back cameras. now, it’s time to play around with the layout of the web app and take advantage of the features available on a foldable device. in order to do that, you will implement the device posture api that allows developers to detect what is the current posture of the phone. in order to change the layout when the device is partially folded, the device posture that you will look for is in a form of a book or laptop. in style.css, apply the following media query: @media (device-posture: folded) { body { display: flex; flex-flow: column nowrap; } .video-container .camera-controls { flex: 1 1 env(fold-bottom); } .msg { display:block; margin: 5em; } using modern css features like display:flex and grid, you can change the layout of the body element and the elements with the class video-container and camera-controls when the phone is flipped. 8. test your app whether you test on a real foldable phone or on a remote test lab device, you need to enable the device posture api in the latest version of samsung internet. to do this, open the browser and type internet://flags in the url bar and search for either device posture api or screen fold api, then select enable. test in a real device if you have a real physical device, you can test it directly on samsung internet using the url that glitch provides you by clicking on show in a new window menu. just partially fold your phone and you will see how the layout changes and even discover a hidden message! use remote test lab the other option, if you don’t have a physical device, is by using remote test lab. you can choose any galaxy foldable devices from the list and follow the same instructions as you would with a real device. just make sure to enable the device posture api and have the latest version of samsung internet. use the buttons provided by remote test lab to partially fold your remote galaxy device. implement polyfill polyfill allows you to emulate behavior on devices that do not have folding capabilities. it helps you visualize how the content responds to the different angle and posture configurations. just include sfold-polyfill.js directly into your code and use the polyfill settings (screenfold-settings.js) of the web component that will emulate the angle of the device and therefore, it will change its posture. moreover, add the following code in index.html <head> … <script type='module' defer src="screenfold-settings.js"></script> <script src="sfold-polyfill.js"></script> … </head> <body> <section> <screenfold-settings></screenfold-settings> </section> … </body> 🥳 you did it! you’ve created a web app that detects when a foldable device change its posture. please let us know how it went here, we would like to hear your thoughts around this api, future codelabs and more! this feedback will go directly to the w3c devices and sensors group, so you will be part of the conversation around this draft too! thanks a lot for reading.