• DaveDev

    Accessing the Camera in a Windows 8 Metro Style App using HTML and JavaScript

    • 2 Comments

    Overview

    I have recently been coding a Windows 8 Metro Style App using the new Windows 8 Release Preview bits and Visual Studio Express 2012 RC.  The app is going to be a retro shooter that takes advantage of HTML5 Canvas for the main game engine and then several Windows 8 Metro Style App Features.

    One of the things I wanted to do in the game was to keep Player Profiles.  These profiles track player high scores, player names and a player avatar.  Eventually these high scores could be used in a leaderboard running in the cloud. 

    Player Name and Avatar

     

    I thought it would be neat to let players take a picture of themselves for their Avatar by accessing the webcam.  Fortunately for us WinRT makes this super easy to use.  We can even pull up prebuilt camera capture UI by using the built-in Windows.Media.Capture API’s.  If you are an existing Windows Phone Developer this may remind you a lot of Windows Phone Development Tasks – something I absolutely loved. 

    Features like camera timer, settings, and  even picture cropping are all done for us via the Windows.Media.Capture.CameraCaptureUI.  For those looking for more control you also have the ability to call into Windows.Media.Capture.MediaCapture.  You won’t get a default UI with MediaCapture but you do get full control of the streams.  MediaCapture is also a way to record Audio streams in your app.

    Here is what the default Capture Capture UI looks like when using the cropping tool:

    Camera Capture Cropping Tool

    After cropping the photo to my liking and tapping the OK button my new Avatar is automatically set.  The new Avatar Image is automatically scaled correctly thanks to the predefined CSS styling we have created.

    New Avatar Picture

     

    Sweet!

     

    Declaration and Initialization

    One of the joys of doing Windows 8 Metro Style development with HTML and JavaScript is that it feels like Web Programming.  In fact my entire game header is done using normal <div> tags and CSS Styles.  Inside of my default.html file I defined the following header to display the player score, level, name and avatar picture:

    <div id="divGame">
            <div id="txtScore">Score: 0</div>    
            <div id="txtLevel">Level: 0</div>
            <div id="divPlayer">
                 <div id="txtPlayerName">Player1</div>
                 <img id="imgPlayer" src="/images/helmet.png" />
             </div>
    <div>  

     

    I then defined a CSS Style for the Avatar Picture using it’s imgPlayer id.

    #imgPlayer {
        max-width: 100px; 
        max-height: 50px;
        float:left;
        margin-right:10px;
     
    }

     

    The Windows Library for JavaScript (WinJS) gives us a way to create a Windows 8 AppBar easily by just a few lines of HTML (more on WinJS Controls later).  By opening the AppBar and tapping the cmdCamera button our players will get the option to take a new Avatar picture.

    <div id="AppBar" data-win-control="WinJS.UI.AppBar" data-win-options="">
           <button id="cmdHome" data-win-control="WinJS.UI.AppBarCommand" data-win-options="{id:'cmdHome',label:'Menu',icon:'home',section:'selection',tooltip:'Menu'}">
           </button>
          <button id="cmdCamera" data-win-control ="WinJS.UI.AppBarCommand" data-win-options="{id:'cmdCamera',label:'Cadet Photo',icon:'camera',section:'global',tooltip:'Change Cadet Photo'}">
           </button>
         <button id="cmdName" data-win-control ="WinJS.UI.AppBarCommand" data-win-options="{id:'cmdName',label:'Cadet Name',icon:'url(images/accept.png)',section:'global',tooltip:'Change Cadet Name',type:'flyout',flyout:'nameFlyout'}">
           </button>
    </div>

     

    All of the AppBar buttons tap events are handled using standard JavaScript EventListeners:

    //AppBar Commands
            document.getElementById("cmdCamera").addEventListener("click", capturePhoto, false);
            document.getElementById("cmdName").addEventListener("click", showCadetNameUpdate, false);
            document.getElementById("cmdHome").addEventListener("click", showMenu, false);
            document.getElementById("submitButton").addEventListener("click", updateCadetName, false);

    That’s it! We now have a working Game Header and AppBar for our game.  When a player clicks on the Camera Button we will invoke a call to the capturePhoto function.

     

    Opening the Camera Capture UI

    capturePhoto is where the magic happens.  The first thing we do is make a call to WinRT using the Windows.Media.Capture namespace.  This gives us a reference to CameraCaptureUI where we can set default properties.  For my game I am telling the camera to use a 16x9 aspect ratio and lock to the photo mode.  If I wanted the user to capture video I could easily do this as well by using Windows.Media.Capture.CameraCaptureUIMode.video.

    //WinRT Camera API
        function capturePhoto() {
            try {
                var dialog = new Windows.Media.Capture.CameraCaptureUI();
                var aspectRatio = { width: 16, height: 9 };
                dialog.photoSettings.croppedAspectRatio = aspectRatio;
                dialog.captureFileAsync(Windows.Media.Capture.CameraCaptureUIMode.photo).then(function (file) {
                    if (file) {
                        imgPlayer.src = URL.createObjectURL(file);
     
                    } else {
                        //No Photo captured
                    }
                }, function (err) {
                    displayError(err);
                });
            } catch (err) {
                displayError(err);
            }
        }

    This is where you will start to see all of the Async goodness inside of WinRT.  The Camera Capture UI  will run asynchronously while it awaits the user to take a picture.  I handle the callback by invoking a JavaScript Promise using the then keyword.  The promise will call a function when the user takes a photo and if there is an object it will set the source of my imgPlayer tag to be that file.  Those of you familiar with Web programming will recognize the URL.createObjectURL call so that my <img> tag knows where to reference the new source file.

     

    Conclusion

    Hopefully you have seen how easy it is to access the WebCam on Windows 8 now thanks to WinRT and the Windows.Media.Capture API’s.  As Always - if you are currently working on a Windows 8 app I would love to hear about it!

    You may also want to check out my previous Windows 8 Metro Style Development Tips:

  • DaveDev

    Defining Layout in a Windows 8 Metro Style App using CSS3, HTML and JavaScript

    • 2 Comments

    Overview

    I have been coding a Windows 8 Metro Style App using the new Windows 8 Release Preview bits and Visual Studio Express 2012 RC.  The app is going to be a retro shooter that takes advantage of HTML5 Canvas for the main game engine along with several Windows 8 Metro Style App Features.  This post is going to cover how to layout the elements of your game interface on the screen. 

    My game’s Layout is made up of two screens: a Main Menu (which is the default screen upon loading for first time) and then an actual Game Screen where gameplay takes place.  I used CSS3 (specifically floats) to align my Player Score, Name, Level and other screen elementals.  Let’s dive into how each screen is set up.


    Main Menu

    The Main Menu displays current game version number, game title and a button to begin game play.  It also includes an AppBar Menu (not shown in the screenshots below) that can be accessed at any time.  Here is what the Main Menu looks like:

     

    FullScreen 

    Filled

    Snapped

    You will notice that I am playing the game along side another application (IE 10) like some of my players will most likely do.  The screen can be rendered in three different states based which are named fullscreen, filled or snapped mode.  I want to point out that having your application support multiple screen sizes and Viewstates is a very important feature of all Metro Style Apps.  If you haven't already read it definitely check out the Scaling to Different Screens post on the official Windows 8 Blog.

    Windows8Devices

     

    Since this is such an important topic broke out this topic into two posts.  We will cover layout in this post and how to detect and scale for the different Viewstates here.  In the interest of simplicity this post will concentrate on just getting the elements lined up in the fullscreen mode (the default for all Metro Style Apps).  Let’s take a look at how we do that:  

    Inside of my default.html page we see that the title image, game version and play button are just standard html elements wrapped inside a <Div> tag I call divMenu.

    <div id="divMenu">
            <img id="imgMenu" src="/images/menutitle.png" />
            <div id="txtVersion"></div>
            <button id="btnStart">PLAY</button>
     <div>


    All of our custom layout occurs in a css file called space.css we reference in our default.html like so:

    <!-- App references -->
    <link href="/css/default.css" rel="stylesheet">
    <link href="/css/space.css" rel="stylesheet"/>
    <script src="/js/default.js"></script>
    <script src="/js/sound.js"></script>
    <script src="/js/ship.js"></script>
    <script src="/js/starField.js"></script>

     

    Thanks to CSS3 Web Fonts we can easily assign different type faces to our screen elements.  For my game I downloaded the Final Frontier Old Style font a royalty free font available off the web.  I then tell both the version text and button text to use this font.  Each of the elements I assign the maximum width and height I would like along with the text font size.  imgMenu is also told to be aligned 120px down from the top of the screen.

    @font-face {
        font-family: 'Final Frontier Old Style'; 
        src: url( '/fonts/finalold.ttf');
    }
     
    #imgMenu {
        position:absolute;
        max-width: 555px; 
        max-height: 118px; 
        top: 120px;
    }
     
    #txtVersion {
        position:absolute;
        bottom:0;
        right:0;
        font-family: 'Final Frontier Old Style'; 
        font-size: 20px;
        color: #ffd800;
    }
     
    #btnStart {
        position:absolute;
        width: 300px;
        height: 150px;
        font-family: 'Final Frontier Old Style'; 
        font-size: 30px;  
        z-index: 100;
    }
     

     

    Remembering that Windows 8 devices come in all different shapes and sizes I programmatically set the  menu elements up on the center of the screen at runtime.  This happens in the showMenu function inside my default.js file.  I calculate the exact center of the screen and then set the x and y coordinates of my elements like you see below.

    //Set Up Menu Screen UI Elements
    function showMenu(event) {
        menuEnabled = true;
     
        txtPlayerName.style.visibility = "hidden";
        txtScore.style.visibility = "hidden";
        imgPlayer.style.visibility = "hidden";
        imgMenu.style.visibility = "visible";
        btnStart.style.visibility = "visible";
        txtVersion.innerHTML = GAME_VERSION;
        txtVersion.style.visibility = "visible";
        txtLevel.style.visibility = "hidden";
        
     
        var menuX, btnX, btnY;
        menuX = (SCREEN_WIDTH - imgMenu.width) / 2;
        btnX = (SCREEN_WIDTH - btnStart.clientWidth) / 2;
        btnY = (SCREEN_HEIGHT - btnStart.clientHeight) / 2;
     
        imgMenu.style.posLeft = menuX;
        btnStart.style.posLeft = btnX;
        btnStart.style.posTop = btnY;
     
        musicGame.pause();
        musicMenu.play();
     
    }

     

    Game Screen

    The Game Screen  displays the player’s score, level, name and picture.  This is also where we are declaring our HTML5 Canvas tag.  Similarly to the Main Menu screen it can appear in all three Viewstates but we will focus on the Fullscreen mode for this post.

    Fullscreen

    Filled

    Snapped

     

    All of these elements (except for the player image) are standard <div> tags wrapped inside a main <div> tag we call divGame  inside of our space.css file.

    <div id="divGame">
            <div id="txtScore">Score: 0</div>    
            <div id="txtLevel">Level: 0</div>
            <div id="divPlayer">
                 <div id="txtPlayerName">Player1</div>
                 <img id="imgPlayer" src="/images/helmet.png" />
             </div>
    <div>  
     
    <div id="txtNewLevel"></div>
     
    <div id="divRoot" data-win-control="WinJS.UI.ViewBox">
        <canvas id="canvas" ></canvas>
    </div>
     

     

    Just like the Main Menu we assign the custom Font to our text elements and position them on screen using floats. 

    @font-face {
        font-family: 'Final Frontier Old Style'; 
        src: url( '/fonts/finalold.ttf');
    }
     
    #txtScore {
        font-family: 'Final Frontier Old Style';
        font-size: 50px;
        color: #ffd800;
        text-align: left;
        margin-left: 10px;
    }
     
    #txtPlayerName {
        font-family: 'Final Frontier Old Style'; 
        font-size: 50px;
        color: #ffd800;
        float:left;
        margin-right:20px;
    }
     
    #txtLevel {
        font-family: 'Final Frontier Old Style'; 
        font-size: 50px;
        color: #ffd800;
        position:absolute;
        top: 0;
    }
     
    #divPlayer {
        position: absolute; 
        top: 0; 
        right: 0; 
        text-align:right;
    }
     
    #imgPlayer {
        max-width: 100px; 
        max-height: 50px;
        float:left;
        margin-right:10px;
    }
     
    #txtNewLevel {
        position:absolute;
        font-family: 'Final Frontier Old Style'; 
        font-size: 100px;
        visibility: hidden;
    }

     

    Finally we set up all of our on screen elements through the startGame function once the player clicks on the Play button to begin.  

    //Set up Game Screen UI Elements
     function startGame(event) {
         txtPlayerName.style.visibility="visible";
         txtScore.style.visibility="visible";
         imgPlayer.style.visibility = "visible";
         imgMenu.style.visibility = "hidden";
         btnStart.style.visibility = "hidden";
         txtVersion.style.visibility = "hidden";
         txtLevel.style.visibility = "visible";
     
         var lvlX = (SCREEN_WIDTH - txtLevel.clientWidth) / 2;
         txtLevel.style.posLeft = lvlX;
     
         musicMenu.pause();
         musicGame.play();
     
         menuEnabled = false;
         
     }

     

    Game Coordinate System

    The last piece we need to set up is a coordinate system for our game.  This includes the size of each ship, the area of the screen they can be drawn on before appearing on the opposite side, and then the current width and height of the screen.  The screen width and height will determine how big our Canvas element needs to be rendered at as well.

    I use four different variables inside the default.js file for this with the ships width and height remaining constant (I created all of my ship images to be this size) and then the screen width and height to change based on the resolution of our current Windows 8 device.

    var SCREEN_WIDTH = 1366;
    var SCREEN_HEIGHT = 768;
    var SHIP_WIDTH = 190;
    var SHIP_HEIGHT = 100;

     

    We then calculate what the size of our canvas should be during our initialize function:

    //Init Canvas
    canvas = document.getElementById("canvas");
    ctx = canvas.getContext("2d");
     
    //Set up Coordinates for Screen Size
    //Adjust for different screen sizes
    canvas.width = window.innerWidth;
    canvas.height = window.innerHeight;
    SCREEN_HEIGHT = canvas.height;
    SCREEN_WIDTH = canvas.width;
     
    //Set boundries to be one ship size
    MAX_X = canvas.width - (SHIP_WIDTH + 20);
    MAX_Y = canvas.height - (SHIP_HEIGHT + 50);

     

    By setting up the coordinate system at run time we can support multiple resolutions and even dynamically scale to fit higher resolutions if the user changes them (for example docking a tablet and attaching a bigger monitor).  Here is an example of our game running on a 1366x768 tablet size screen as well as a 2560x1440 which you would typically see in a 27” inch all in one pc.

    1366x768

    2560x1440 

    All of the elements align directly center with the version showing up in the bottom right.  I also have the ability to scale up the image and button size when in a different resolution.  I am not doing that in the screen shot above but will show you how using CSS3 transforms in my next post covering Viewstates.

     

    Conclusion

    I hope this post has given you an idea of how easy it is to lay out interface elements in your Metro Style App using CSS3, HTML5 and JavaScript.  If you are currently working on a Windows 8 app and want to get into the Windows Store I would love to hear about it!

    You may also want to check out my previous Windows 8 Metro Style Development Tips:

  • DaveDev

    Windows Phone 7 Tip - How to automatically sync your OneNote documents to the Cloud

    • 12 Comments

    OneNote for Windows Phone 7

    OneNote is one of my favorite Microsoft Office applications and an amazing note taking tool.  I use it constantly to take screen shots of websites or applications, quick dictation, to-do lists, and even some sketching/doodles (via my Tablet’s pen).  One of the goals of the OneNote Team was to bring that note taking application to all three screens (PC, Cloud and Phone).  With the release of Windows Phone 7 that goal became a reality and we can now sync notes between all three screens.  Changes made on the phone will be available on our desktop and changes made on our desktop will show up in a web browser.  If you haven’t checked out the free Office Web Applications yet you owe it to yourself to take a peek.  Office Web Applications are part of SharePoint, are available when editing or viewing any Office document on your Skydrive as well as through other services like Docs.com and Office Live Workspace.

    Four Easy Steps to Cloud Sync

    What I don’t think we have done a good job of is actually exposing this functionality on Windows Phone 7 easily.  I had a conversation just last night with someone who was adamant about Windows Phone 7 not being able to cloud sync One Note Documents yet.  This prompted me to write this blog post in hopes I can make it easier for you as well. 

    Step 1 – Open Microsoft Office Hub

    When we first open up the Office Hub we are presented with a list of shortcuts to our most recently used OneNote Documents.  You can also tap and hold on these shortcuts to pin them to your start experience on the phone (I use this for quick to-do or shopping lists).  What is not obvious at first is you need to hit the All icon to get to the rest of your documents and open up the syncing option. In the below screenshot you will notice I have just created a quick OneNote document called DaveDev and I am now going to click on the All icon.

     

    Main Menu - Click ALL

    Step 2  - Click on Refresh icon on the Application Bar

    This will take you to a list of Pages in your current Notebook.  Windows Phone 7 only has one Notebook by default where all of your Pages will be stored.  When we enable syncing however we will start to pull down all Notebooks from the Cloud.  This means we can select these other Notebooks and create Pages in them right on the phone.  So while we cannot create Notebooks directly on the phone we can integrate with them existing ones.  If we bring up the full Application Bar we will notice a Refresh icon.  Go ahead and click that now.

    Document ListDocument List

     

    Step 3 – Confirm that you want to start syncing your documents with Skydrive

    You will be asked to confirm that you want to begin syncing OneNote with your Skydrive.  What Skydrive would that happen to be?  When you created/entered your Windows Live ID into the phone you were automatically given 25 gigabytes of free storage in the cloud (what a nice present eh?).  Office Documents on Windows Phone 7 are integrated with your Windows Live ID so it will take care of everything for you.  Exactly the same way the Zune Marketplace and X-Box Live integration works.  Go ahead and confirm that you want to enable Skydrive sync.

    SkyDrive Confirmation Prompt

    You will now be notified during the process of the sync status.  First it will connect up to Windows Live and then it will set up a default notebook (Unfiled Notes) if it does not already exist.

    Connecting to Windows Live!Creating Default Notebook

     

    Step 4 – Verify that your notebooks and pages have synced

    Now that we have enabled sync you will notice some changes.  First, all of your notes will now include the SkyDrive Folder (Personal) and OneNote Notebook (Unfiled Notes) they have been synced to.  You will also notice a Notebooks option that is now available.

    Notes-Synced!

    Swiping the screen to the left or clicking on Notebooks will take you to the new Notebooks screen.  This screen will display the status of all your Notebooks currently located in Skydrive as well as their Sync status.

    Synced Notebooks and Pages!

    Notice too that you now have the ability within each Note to sync immediately back to your Skydrive.  Clicking on the Refresh icon will kick off this process for you.

    DaveDev Note - Sync Button

     

    Viewing your One Note documents from the Cloud

    So now we have One Note running on our Windows Phone 7 device and we have One Note running on our Windows 7 PC.  But, let’s say we are out, maybe over at a friends house, and we need to pull up a quick One Note document.  I told you our stuff is in the cloud so we should be able to get to it from any Web Browser, right?  Of course! 

    Simply point your browser to Windows Phone Live and you will be presented with a bunch of functionality for your phone.  This is really where your cloud integration comes alive!  X-Box Live achievements, avatar customizations, contacts, calendar, phone tracking, photos as well as our newly synced OneNote documents are accessible here.  Simply click on the View All link under OneNote and you will be taken directly into the OneNote Web App.

     

    OneNote at http://WindowsPhone.live.com

    Like all of the Office Web Apps the One Note Web App is full featured and even includes the familiar Office Ribbon.  Notice along the top you can see the current Skydrive folder my Notebooks are synced under (Personal Web).  This is the same folder we saw displayed on our Windows Phone 7 device.  On the left pane you can now navigate between all of your notebooks and their included pages.  Opening up my newly synced document will display it in the editor on the write and any changes here will save back to the desktop and my phone.

    SkyDrive - Editing my OneNote Docs

    To the Cloud

    Hopefully this eliminated any confusion you might have had around enabled Cloud sync for Office One Note documents on your Windows Phone 7 Device.  Although we only do this for OneNote today we have already announced a free Windows Phone 7 update later this year that will sync your Excel, PowerPoint and Word documents as well.  These documents will be able to sync back to your Skydrive like we have shown here in addition to the already existing SharePoint functionality. 

  • DaveDev

    Announcing the MetroGameKit – a free, sample, starter project for Windows 8 Casual Games using HTML5 and JavaScript

    • 0 Comments

     

    spacecadet   Pew Pew! Fire Lasers!

    During the past couple of months I’ve been hacking around in the world of HTML5 Canvas and casual gaming.  The community that has formed up around HTML5 Gaming in particular is awesome and I am continually impressed by what I’ve seen.

    Not to mention I’m a big retro gaming fan – 8 bits anyone?

    With that in mind I wanted to contribute back to the community if I could.  I figured the best way I could do that was to take the experience I have writing Windows 8 Metro Style Apps and combine them with the world of HTML5 Canvas based gaming.

    Things were super easy to bring over from a code perspective.  All of my layout is done through CSS3 (including media queries for ViewState), the game loop is straight up HTML5 Canvas with requestAnimationFrame, the music uses HTML5 Audio Tags, and the sounds use third party JavaScript libraries like SoundJS.

     

    Get the Code

    You can grab the sourcecode from Codeplex here:

    CodePlex


    Or GitHub here:

    GitHub

     

    Summary

    I am replicating all the source back between GitHub into Codeplex so regardless of where you fork you will always have the latest source.

    If you decide to write a Windows 8 app and want to get into the Windows Store I would love to hear about it!  You may also want to check out my previous Windows 8 Metro Style Development Tips:

    Happing Coding!

    -Dave

Page 3 of 68 (272 items) 12345»