Zooming Picture Size in Windows Store Apps

Introduction

Today we are going to learn how to create Windows Store Apps for zooming the Picture Size using JavaScript. If the picture aspect ratio of the video sample does not equal the aspect ratio of the video window of the video tag, then the "zoom" attribute allows the app to programmatically zoom in or zoom out to make the video fit the video window without having a pillar or letter boxed experience.

To start the creation of the apps, add two JavaScript pages by right-clicking on the js folder in the Solution Explorer and select Add > new item > JavaScript Page and then provide an appropriate name. In the same way, add one HTML page to your project.

zoom-in-Windows-store-apps.jpg

Write the following code in default.html:

<!DOCTYPE html>

<html>

<head>

    <meta charset="utf-8" />

    <title>App</title>

    <link rel="stylesheet" href="//Microsoft.WinJS.1.0/css/ui-light.css" />

    <script src="//Microsoft.WinJS.1.0/js/base.js"></script>

    <script src="//Microsoft.WinJS.1.0/js/ui.js"></script>   

    <link rel="stylesheet" href="/css/default.css" />

    <script src="/js/script1.js"></script>

    <script src="/js/default.js"></script>

</head>

<body role="application"style="background-color:lightskyblue">

   <center><div id="rootGrid">

         <div id="content">

            <h1 id="featureLabel"></h1>

            <div id="contentHost"></div>

        </div>

      </div></center>

</body>

</html>

Write the following code in default.js:
 

(function () {

    "use strict";

    var appTitle = "";

 

    var Pages = [

        { url: "page.html" }

    ];

    function activated(eventObject) {

        if (eventObject.detail.kind === Windows.ApplicationModel.Activation.ActivationKind.launch) {

            eventObject.setPromise(WinJS.UI.processAll().then(function () {

                var url = WinJS.Application.sessionState.lastUrl || Pages[0].url;

                return WinJS.Navigation.navigate(url);

            }));

        }

    }

    WinJS.Navigation.addEventListener("navigated"function (eventObject) {

        var url = eventObject.detail.location;

        var host = document.getElementById("contentHost");

        host.winControl && host.winControl.unload && host.winControl.unload();

        WinJS.Utilities.empty(host);

        eventObject.detail.setPromise(WinJS.UI.Pages.render(url, host, eventObject.detail.state).then(function () {

            WinJS.Application.sessionState.lastUrl = url;

        }));

    });

    WinJS.Namespace.define("App", {

        appTitle: appTitle,

        Pages: Pages,

        videoOnError: function (error) {

            switch (error.target.error.code) {

                case error.target.error.MEDIA_ERR_ABORTED:

                    App.displayError("You aborted the video playback.");

                    break;

                case error.target.error.MEDIA_ERR_NETWORK:

                    App.displayError("A network error caused the video download to fail part-way.");

                    break;

                case error.target.error.MEDIA_ERR_DECODE:

                    App.displayError("The video playback was aborted due to a corruption problem or because the video used features your browser did not support.");

                    break;

                case error.target.error.MEDIA_ERR_SRC_NOT_SUPPORTED:

                    App.displayError("The video could not be loaded, either because the server or network failed or because the format is not supported.");

                    break;

                default:

                    App.displayError("An unknown error occurred.");

                    break;

            }

        },

        displayStatus: function (msg) {

            WinJS.log && WinJS.log(msg, null"status");

        },

        displayError: function (msg) {

            WinJS.log && WinJS.log(msg, null"error");

        }

    });

    WinJS.Application.addEventListener("activated", activated, false);

    WinJS.Application.start();

})(); 


Write the following code in page.aspx:
 

<!DOCTYPE html>

<html>

<head>

    <title></title>

    <script src="/js/script.js"></script>

</head>

<body>

    <div data-win-control="App.PageInput">

        <button class="action" id="zoomIn">

            Zoom in</button>

        <button class="action" id="zoomOut">

            Zoom out</button>

        <br />

        <br />

    </div>

    <div data-win-control="App.PageOutput">

        <video id="zoomVideo1" style="positionrelativez-indexautoopacity1.0;" width="500"

            height="250" controls src="http://ie.microsoft.com/testdrive/Videos/BehindIE9AllAroundFast/Video.mp4"

            poster="images/sample-poster.jxr" loop>

        </video>

        <video id="zoomVideo2" style="positionrelativez-indexautoopacity1.0;" width="300"

            height="300" controls src="http://ie.microsoft.com/testdrive/Videos/BehindIE9AllAroundFast/Video.mp4"

            poster="images/sample-poster.jxr" loop muted>

        </video>

    </div>

</body>

</html>


Write the following code in script.js:

(function () {

    "use strict";

    var page = WinJS.UI.Pages.define("page.html", {

        ready: function (element, options) {

            WinJS.Utilities.query("#zoomIn").listen("click", zoomIn);

            WinJS.Utilities.query("#zoomOut").listen("click", zoomOut);

            WinJS.Utilities.query("#zoomVideo1").listen("error", App.videoOnError);

            WinJS.Utilities.query("#zoomVideo2").listen("error", App.videoOnError);

            WinJS.Utilities.query("#zoomVideo1")[0].play();

            WinJS.Utilities.query("#zoomVideo2")[0].play();

        },

        unload: function () {

            stopPlayback();

        }

    });

    function zoomIn() {

        WinJS.Utilities.query("#zoomVideo1")[0].msZoom = true;

        WinJS.Utilities.query("#zoomVideo2")[0].msZoom = true;

    }

    function zoomOut() {

        WinJS.Utilities.query("#zoomVideo1")[0].msZoom = false;

        WinJS.Utilities.query("#zoomVideo2")[0].msZoom = false;

    }

    function stopPlayback() {

        var vid1 = WinJS.Utilities.query("#zoomVideo1")[0],

            vid2 = WinJS.Utilities.query("#zoomVideo2")[0];

        if (!vid1.paused) {

            vid1.pause();

        }

        if (!vid2.paused) {

            vid2.pause();

        }

    }

})();


Write the following code in script1.js:

(
function () {

    var PageOutput = WinJS.Class.define(

        function (element, options) {

            element.winControl = this;

            this.element = element;

            new WinJS.Utilities.QueryCollection(element)

                        .setAttribute("role""region")

                        .setAttribute("aria-labelledby""outputLabel")

                        .setAttribute("aria-live""assertive");

            element.id = "output";

 

            this._addOutputLabel(element);

            this._addStatusOutput(element);

        }, {

            _addOutputLabel: function (element) {

                var label = document.createElement("h2");

                label.id = "outputLabel";

                label.textContent = "Output";

                element.parentNode.insertBefore(label, element);

            },

            _addStatusOutput: function (element) {

                var statusDiv = document.createElement("div");

                statusDiv.id = "statusMessage";

                element.insertBefore(statusDiv, element.childNodes[0]);

            }

        }

    );

    var currentPageUrl = null;

    WinJS.Navigation.addEventListener("navigating"function (evt) {

        currentPageUrl = evt.detail.location;

    });

    WinJS.log = function (message, tag, type) {

        var statusDiv = document.getElementById("statusMessage");

    };

    function activated(e) {

        WinJS.Utilities.query("#featureLabel")[0].textContent = App.appTitle;

    }

    WinJS.Application.addEventListener("activated", activated, false);

    WinJS.Namespace.define("App", {

        PageOutput: PageOutput

    });

})();



Output:

zoom-in-Windows-store-app.jpg

Summary


In this article I described how to create a Windows Store App for Zooming the picture size using JavaScript. I hope this article has helped you in understanding this topic. Please share it. If you know more about this, your feedback and constructive contributions are welcome.

Next Recommended Readings