Palermo4 on MSDN

Technical Blog of J. Michael Palermo IV

October, 2012

Check it out!

Posts
  • Palermo4 on MSDN

    CSS3 for Windows Store Apps: Animations

    CSS3 has brought much goodness to the web developer community.  The feature that potentially steals the show is CSS3 Animations – which can also be used when developing applications for the Windows Store via JavaScript.

    If you are not familiar with animations, the name itself is practically self-explanatory.  Animations can deliver very appealing “eye-candy” in a variety of ways including object movement, resizing, transforming, fading, style changes, and much more.  Because animations have been around awhile, there is plenty of information out there to help you get started, such as the animations topic at the IE 10 Guide for Developers and Hands On: Animations on IE Test Drive.  Our focus here is more on how to use animations in the context of writing apps for the Windows Store.

    Animations Simplified

    A chronic worry when developing content for the web is how our creative work will render in multiple browsers.  For example, the following CSS rules define an animation for bringing an element in from above the screen:

    @-webkit-keyframes drop-in {
        0%   {top: -100px;}
        100% {top: 0px;}
    }
    @-moz-keyframes drop-in {
        0%   {top: -100px;}
        100% {top: 0px;}
    }
    @-o-keyframes drop-in {
        0%   {top: -100px;}
        100% {top: 0px;}
    }
    @keyframes drop-in {
        0%   {top: -100px;}
        100% {top: 0px;}
    }
    #message {
                 position: relative;
        -webkit-animation: drop-in 1s forwards;
           -moz-animation: drop-in 1s forwards;
             -o-animation: drop-in 1s forwards;
                animation: drop-in 1s forwards;
    }

    All of the rules above are reduced to the following when developing apps for the Windows Store:

    @keyframes drop-in {
        0%   {top: -100px;}
        100% {top: 0px;}
    }
    #message {
         position: relative;
        animation: drop-in 1s forwards;
    }

    Why so much reduction?  When writing Windows Store apps, we are not worried about supporting multiple browsers.  Whatever standards work for IE 10 are all we need.  Granted, if you used the previous rules with all the vendor prefixes while creating an app - no error would occur, they would just be ignored.

    Playing Animations

    Animations are triggered as soon as they are defined.  In the previous example, as soon as the page with the element with and ID of “message” was navigated to, the animation was triggered.  Animations can be triggered from code as well.  Consider the following example:

    @keyframes drop-in {
        0%   {top: -100px;}
        100% {top: 0px;}
    }
    .animate-drop-in {
        animation: drop-in 1s forwards;
    }
    #message {
         position: relative;
    }

    The rules above introduced a slight change from what we observed previously – the animation was moved to a class named “animate-drop-in”.  Assuming the element with an ID of “message” is not defined with the new class name, the animation will not occur.  However, we can trigger the animation in code as seen here:

    message.onclick = function () {
        message.classList.toggle("animate-drop-in");
    }

    The click event handler for the “message” element toggles the class name.  Once the app sees the element is assigned to an animation (which is what the new class does), it triggers the animation.  The toggle method of classList returns a boolean value based on whether it added (true) the class name or removed (false) it.

    If needed, you can respond to three events related to animations.  Here is the syntax for defining the handlers for each:

    // animation started
    message.addEventListener("animationstart", function (arg) { });
    
    // animation iteration completed
    // applies only when animation-iteration-count > 1
    message.addEventListener("animationiteration", function (arg) { });
    
    // animation ended
    message.addEventListener("animationend", function (arg) { });

    The above event handlers are handy and can allow a chain of complex animations.  For example, in the event handler for when an animation ends, you can add code to trigger a completely new animation.

    Animations vs. Transitions

    A close relative to animations is CSS3 Transitions, and those are covered in another post entitled CSS3 for Windows Store Apps: Transitions.  Since both of these technologies offer “animation” as the effect, you might wonder when to use one or the other.  With that in mind, take the following in consideration when trying to decide which is best:

    Animations
    • Effect is played as soon as animation is defined, or programmatically
    • Multiple stages are possible, allowing more complex animations
    Transitions
    • Effect is triggered when a targeted property value changes, usually due to a state change such as when a :hover is defined.  A targeted property value changed in code will also trigger a transition.
    • Syntax is much simpler, thus, effect is usually simpler than what can be done with animations.

    Another point to think about is what the intent of each technology is.  Transitions (as the name implies) are really intended to provide a nice visual effect when something is changing state.  Animations can be used for anything pertaining to the logic of the application.

    With Great Power…

    Animations offer a great way to enhance the user experience.  However, too much of a good thing can become an annoyance. So be judicious with your implementations of animations.

    It is an exciting time to be a developer with web skills.  Harness what you know and use it to great some great applications for the Windows Store.  Be sure to take advantage of all the resources available at Generation App!

  • Palermo4 on MSDN

    2012 Oct 30 The New Era - Hollywood

    • 0 Comments

    Please join me in Hollywood for an exciting launch event! Register!

  • Palermo4 on MSDN

    JavaScript for Windows Store Apps: View States

    Creating an application for the Windows Store requires forethought to a number of considerations – one of which is how will you want your app to appear when seen in different view states.

    When your application is running full screen in Windows 8, it will be in either landscape or portrait orientation.  When the orientation is landscape mode, two additional views are possible: snapped or fill.  A snapped view is docked either on the right or left side of the screen, with a width of 320px.  The fill view is the larger remaining portion of the screen when another app is in snapped view. 

    Note Snapped and fill options are only available when the screen resolution is a minimum of 1366 x 768 and supporting wide screen (16:9) aspect ratio.

    When developing apps using JavaScript, you have two main ways to manage the view states in your apps:  Programmatically via JavaScript, or declaratively with CSS3 Media Queries.  It is recommended that you use media queries to manage view state changes.  However, there are times when you will need to respond to a view state change in code, and that is going to be our focus in this post.

    What is Your Current View?

    The Windows.UI.ViewManagement namespace provides some classes and enumerations for managing an applications view.  The ApplicationView class can be used to determine the applications current view state by calling the value property.  The return type of value is numeric, and maps to the ApplicationViewState enumeration.  Armed with both, the following code shows a helper method for returning a string with the current view state name:

    var appView = Windows.UI.ViewManagement.ApplicationView;
    var viewStates = Windows.UI.ViewManagement.ApplicationViewState;
    
    function getViewStateName(viewState) {
        viewState = viewState || appView.value;
        var vs;
        switch (viewState) {
            case viewStates.fullScreenLandscape: // 0
                vs = "FullScreenLandscape";
                break;
            case viewStates.filled:              // 1
                vs = "Filled";
                break;
            case viewStates.snapped:             // 2
                vs = "Snapped";
                break;
            case viewStates.fullScreenPortrait:  // 3
                vs = "FullScreenPortrait";
                break;
            default:
                vs = "Unknown";
                break;
        }
        return vs;
    }

    The above code is useful for determining what the current view state is.  What about being notified when a view state change occurs?  One way to accomplish this is by responding to the resizing of the window as seen here:

    // manage view state in anonymous handler
    window.addEventListener("resize", function(){ }); 

    The above line of code would allow you to write code in the anonymous function to query the applications view state and respond accordingly.

    If you happen to be using Page Controls you are also likely using the PageControlNavigator, which is a class defined in a navigator.js file included in Visual Studio projects that support single-page navigation model.  Let’s look under the hood of PageControlNavigator, particularly when it responds to the application resizing:

    _resized: function (args) {
        // if a page control exists and has updateLayout defined
        if (this.pageControl && this.pageControl.updateLayout) {
            this.pageControl.updateLayout.call(
                this.pageControl,  // instance of pageControl
                this.pageElement,  // element containing page 
                appView.value,     // current view state
                this.lastViewstate // previous view state
            );
        }
        // capture view state
        this.lastViewstate = appView.value;
    },

    The above snippet of code shows what the PageControlNavigator does when it responds to resizing.  The code checks to see if the current page control has defined an updateLayout function, and if it has, it calls it with parameters useful for interrogating.  Now let’s look at an example of an updateLayout function inside a page control:

    updateLayout: function (element, viewState, lastViewState) {
        /// <param name="element" domElement="true" />
    
        // TODO: Respond to changes in viewState.
        pageTitle.innerText = getViewStateName(viewState);
    }

    In the example above, an element with the ID of “pageTitle” sets it’s text to equal the response of the helper method shown previously – which returns a string with the current view state name.

    Responding to a Different View

    Now that we have established where to write code when the view changes, it is now time to focus on what you might do in that response.  Suppose you have a ListView control on the page that displays items in full screen mode like so:

    SNAGHTML1e5e652d

    Now when going to snapped view, it looks like this:

    image

    Notice in snapped view the scroll bar is still moving horizontally.  What if we want the list to scroll vertically when in snapped view?  This is where a few lines of code can make the difference to improve the user experience.

    The ListView has a layout property that allows you to change from a horizontal grid to a vertical list.  When a view state change occurs, the following code conditionally switches the layout:

    // viewStates is Windows.UI.ViewManagement.ApplicationViewState;
    if (viewState == viewStates.snapped) {
        // vertical scrolling list of data
        list.layout = new WinJS.UI.ListLayout();
    } else {
        // horizontal scrolling grid of data
        list.layout = new WinJS.UI.GridLayout();
    }

    The code above (written inside the updateLayout function) changes a ListView control with an ID of “list” to switch to a different layout if the view state is snapped, otherwise it reverts to the original grid layout.  Doing this along with some tweaks to CSS via media queries (such as adjusting margins) results in the improved UI when in snapped view:

    image

    The content can now be scrolled vertically!

    Embrace a Different Point of View

    No doubt you will enjoy being a user of applications running on Windows 8.  One of the features that is sure to be used by multi-tasking enthusiasts is running apps in snapped & filled modes.  Make sure you when you develop apps for the Windows Store, you create a compelling UI for each view.  Also make sure to take advantage of all the resources available at Generation App!

  • Palermo4 on MSDN

    CSS3 for Windows Store Apps: Transitions

    CSS3-LogoWindows 8 brings in a new era of opportunity for the web developer/designer in that applications in the Windows Store can be written with “web” technologies such as HTML5, JavaScript, and CSS3. Creating a compelling user experience is an important aspect of a successful app, and CSS3 is a fantastic way to reach that objective.  This post will specifically address a great way to enable fast and fluid interactions in your app by using CSS3 Transitions.

    If you haven’t been using transitions or know what they even do, then let’s start with a quick description:  Transitions allow content on the screen to change from one visual state to another in a gradual, animated way.  To learn more about the basics of transitions, check out the Transitions topic at the IE 10 Guide for Developers, and play with the Hands On: transitions on IE Test Drive.  The focus here will assume you know the fundamentals of transitions and want to how to use them when developing apps for the Windows Store.

    Transitions from Web to App

    Will you need to define transitions differently for apps than for web pages?  Technically, the answer is no. But you may not need to work so hard. For example, consider the following CSS rule:

    .transit {
      -webkit-transition: all 0.4s;  
         -moz-transition: all 0.4s; 
           -o-transition: all 0.4s;  
              transition: all 0.4s; 
    }
    

    When developing for the web, we worry about web things – such as supporting multiple browsers. This is not a worry when developing an app for the Windows Store. This means you don’t have to riddle your CSS with all the vendor prefixes as seen above.  Therefore the above rule is reduced and far more simple to read and manage as seen here:

    .transit {
       transition: all 0.4s; 
    }
    

    When a transition is defined, it specifies what properties are targeted for animation and for how long.  In the example above, the all keyword means all animatable properties.  A transition is triggered when any targeted property value changes. A common scenario where values are changed is when using CSS pseudo classes such as :active, :checked, :disabled, :enabled, :focus, :hover, :invalid, and :valid are in play.  Hovering is one of the most common uses of transitions, as seen here:

    .box {
       background-color: #FFFFFF;
             transition: background-color 1s;
    }
    
    .box:hover {
       background-color: #FF0000;
    }
    

    The above example fades from white to red and smoothly back again when hovering.  Although hover effects are appealing, keep in mind that Windows 8 is an operating system designed to fully embrace touch screen devices.  Hovering works well with a mouse, but not with a finger.  One possible way to deal with this is seen here:

    .box {
       background-color: #FFFFFF;
             transition: background-color 1s;
    }
    
    .box:hover, .box:active {
       background-color: #FF0000;
    }
    


    The subtle change above is adding the :active pseudo class if the targeted element is pressed down.  This may not be practical in some circumstances, but it at least involves use touch devices when using you app.

    Transitions in View States

    Another way transitions are triggered is by use of media queries.  Windows 8 applications can run in multiple layouts, and media queries can respond to layout changes by responding to the view state.  Here is an example of a style sheet shell with media queries for each supported view state:

    /* shared css rules go here */
    
    /* media queries for adapting to layout changes */
    @media (-ms-view-state: filled) { }
    @media (-ms-view-state: fullscreen-landscape) { }
    @media (-ms-view-state: fullscreen-portrait) { }
    @media (-ms-view-state: snapped) { }
    

    It is useful to know that transitions can be triggered by layout changes because proper use of them can enhance the fluidity of the application.  For example, a switch to a snapped layout might require a change in font size.  With just a subtle transition, you can get the effect of a graceful move to snapped view and back.  This is demonstrated in the following CSS rules:

    .subheader {
         /* recommended subheader size */
         font-size : 20pt;
        font-family: 'Segoe UI Light';
              color: rgba(255,255,255,1.0);
    
         /* subtle shrink|grow effect */
         transition: font-size ease 0.3s;
    }
    
    @media (-ms-view-state: snapped) {
        .subheader {
            /* recommended subheader size for snapped */
            font-size: 11pt;
        }
    }

    When the rules are implemented above, a switch in the application to a snapped position will create a simple shrink effect.  When switching out of snapped view, the text quickly grows.  Careful use and experimentation can lead to a polished user experience.

    Transitions in Code

    While changes to targeted properties of a transition can be defined in the CSS rules, another way to make property changes programmatically through JavaScript.

    Transitions are always timed.  One way to interact with a transition in code is to respond to the transitionend event when it is complete.  To demonstrate, first look at the following CSS rule:

    .demo-transition-end {
        transition: all 4s;
    }
    
    .demo-transition-end:active {
            background-color: #FF0000;
                   transform: rotate(360deg);
    }

    Now here is an example of code that responds to an element on the page that is the target of a CSS transition rule above:

    var box = document.querySelector("#box");
    box.addEventListener("transitionend",
        function (args) {
            if (args.elapsedTime < 4) {
                box.innerText = "Keep Pressing!";
            }
        }
    );

    In the code above, an anonymous function is used as the event handler when the transition ends.  Note that the argument supplied to the function has an elapsedTime property, allowing you to determine how long the transition lasted.  Because the transition is triggered by the :active pseudo class, it will only reach 4 seconds if the user continues to press down on the element.  The code above detects the user let go too soon, and changes the text of the element to encourage the user to keep pressing.

    To continue with this example, a new class rule will be added:

    .vanish {
        transition-delay: 1s;
               transform: scale(0.01);
                 opacity: 0.01;
    }

    Now an else clause will be added to the event handler, so that the complete code now looks like this:

    var box = document.querySelector("#box");
    box.addEventListener("transitionend",
        function (args) {
            if (args.elapsedTime < 4) {
                box.innerText = "Keep Pressing!";
            } else {
                box.innerText = "Let Go...";
                box.classList.add("vanish");
            }
        }
    );

    The code now responds if the transition reached a full 4 seconds.  If that happens, it changes the text of the element to “Let Go…”.  It also calls on the element’s classList property, and adds a new class named vanish.  The addition of the class rule will invoke another transition that will make the element shrink and disappear in 1 second.

    The classList property allows you to easily change the the state of the class attribute, which will invoke a transition if any targeted property values change.  You can use classList for adding, removing, inspecting, or toggling class names in the attribute.  You can also simply change any of the elements style property values directly to also invoke a transition.

    Transitions Beyond

    Fans of CSS3 transitions will enjoy knowing that such powers are easily tapped when developing Windows Store apps with JavaScript.  You should also be aware that the WinJS.UI.Animation API contains methods for transitioning content as well.

    Have fun coding, and be sure to benefit from all the resources available to you at Generation App!

  • Palermo4 on MSDN

    Microsoft DevRadio: Developing for Device Features

    To play video, hover above and press play button.

    Abstract: win8genapp30
    Ever wonder how you can develop for device features you really like even though your personal device doesn’t support it? Well in this episode, Michael Palermo shows you the power of the Simulator in Visual Studio and how you can test features for various devices.

    Next Steps:
    Step #1 – Download Windows 8 and Windows 8 SDK
    Step #2 – Download Visual Studio Express for Windows 8
    Step #3 – Start building your own Apps for Windows 8

    Subscribe to our podcast via iTunes, Zune, or RSS

    If you're interested in learning more about the products or solutions discussed in this episode, click on any of the below links for free, in-depth information:

    Websites:

    Developing for Windows 8 in 1/2 the time!

  • Palermo4 on MSDN

    Microsoft DevRadio: Media Queries

    To play video, hover above and press play button.

    Abstract: win8genapp30
    In today’s episode Michael Palermo will show you how to manage your programs to work in different layouts via Media Queries. If you need to know how to control what needs to be done in various layouts – Portrait, landscape, snap-view, etc. this short how-to video is just for you.

    Next Steps:
    Step #1 – Download Windows 8 and Windows 8 SDK
    Step #2 – Download Visual Studio Express for Windows 8
    Step #3 – Start building your own Apps for Windows 8

    Subscribe to our podcast via iTunes, Zune, or RSS

    If you're interested in learning more about the products or solutions discussed in this episode, click on any of the below links for free, in-depth information:

    Websites:

    Developing for Windows 8 in 1/2 the time!

  • Palermo4 on MSDN

    Microsoft DevRadio: Using Event Handlers

    To play video, hover above and press play button.

    Abstract: win8genapp30
    In today's episode, Michael Palermo shows how easy it is to attach event handlers in JavaScript. Watch this short demo to see the various techniques to do it!

    Next Steps:
    Step #1 – Download Windows 8 and Windows 8 SDK
    Step #2 – Download Visual Studio Express for Windows 8
    Step #3 – Start building your own Apps for Windows 8

    Subscribe to our podcast via iTunes, Zune, or RSS

    If you're interested in learning more about the products or solutions discussed in this episode, click on any of the below links for free, in-depth information:

    Websites:

    Developing for Windows 8 in 1/2 the time!

  • Palermo4 on MSDN

    Microsoft DevRadio: CSS3 Animations

    To play video, hover above and press play button.

    Abstract: win8genapp30
    Michael Palermo is back for an episode and shows us how to implement CSS3 animations into Windows Store apps!

    Next Steps:
    Step #1 – Download Windows 8 and Windows 8 SDK
    Step #2 – Download Visual Studio Express for Windows 8
    Step #3 – Start building your own Apps for Windows 8

    Subscribe to our podcast via iTunes, Zune, or RSS

    If you're interested in learning more about the products or solutions discussed in this episode, click on any of the below links for free, in-depth information:

    Websites:

    Developing for Windows 8 in 1/2 the time!

Page 1 of 1 (8 items)