DCSIMG
March 2012 - Posts - Shai Raiten's Blog

Shai Raiten's Blog

It's all about code...

March 2012 - Posts

Microsoft Test Manager 11 – Exploratory Testing

Window 8 JavaScript–Asynchronous Programming & App Storage

Microsoft Test Manager 11 – What’s New?

Windows 8 JavaScript – Pinch

The feature I’m going to show you now isn’t part of JavaScript Metro app and you need to write some code and use Metro Touch Event called – MSPointerMove.

The feature I’m going to simulate is Pinch -

Download Demo Project

In this post I’ve created a Pinch functionality to increase and decrease zoom on specific item.

Step 1: Create JavaScript Metro Project

In the default.html page I’ve added a image (for our demo) and div element called output to display the touch output.

<body>
    <div id="output"></div>
    <div style="text-align: center; vertical-align: middle">
        <img id="image" src="images/SelaLogo.png" style="zoom: 1;
width: 10%; height: 10%" />
    </div>
</
body
>

Step 2: Register to Pointer Move Event

In the application activated event we get the image object from the UI and register to MSPointerMove event, this event will allow to receive Multi-touch pointers for more than one finger on the screen.

var img;

app.onactivated = function (eventObject) {
    if (eventObject.detail.kind === Windows.ApplicationModel.Activation.
ActivationKind.launch) {
        WinJS.UI.processAll();

        img = document.querySelector("#image");
        document.addEventListener("MSPointerMove", pointerMoveListener,
false);
    }
};

Step 3: Distance Calculation Helpers

Now, before start handling the touch event I created a point class to obtain x, y, and the point id.

beside that a simple math calculation to get the distance between two points.

Distance Formula: Given the two points (x1, y1) and (x2, y2), the distance between these points is given by the formula:

image

function point(x, y, id) {
    this.id = id;
    this.x = x;
    this.y = y;
}

var distance;
var basePoint1, basePoint2;
var outID;

function calcDistance(point1, point2) {
    var x = Math.pow((point2.x - point1.x), 2);
    var y = Math.pow((point2.y - point1.y), 2);
    return Math.ceil(Math.sqrt(x + y));
}

Step 4: Calculate Distance Between Points

Now we ready to talk about the pointerMoveListener that gets the output from the MSPointerMove event, first we’ll use the getPointerList function to receive all recognize points on the screen, in my demo I want to handle only two points no less and no more.

If basePoint1,2 are undefined we define those objects for the current output and calculate the current distance between those points.

Once the event will raise again we call the cangeRatio method that will calculate the new distance between the points.

function pointerMoveListener(event) {
    var pointerList = event.getPointerList(event.pointerId);
    if (pointerList.length !== 2) return;

    clearTimeout(outID);
    outID = setTimeout(function () {
        basePoint1 = basePoint2 = distance = undefined;
    }, 100);

    var currentPoint1 = new point(pointerList[0].clientX,
pointerList[0].clientY, pointerList[0].pointerId);
    var currentPoint2 = new point(pointerList[1].clientX,
pointerList[1].clientY, pointerList[1].pointerId);

    if (basePoint1 === undefined || basePoint2 === undefined ||
distance === undefined) {
        basePoint1 = currentPoint1;
        basePoint2 = currentPoint2;
        distance = calcDistance(basePoint1, basePoint2);
    }
    else if (basePoint1.id === currentPoint1.id && basePoint2.id
=== currentPoint2.id ||
        basePoint1.id === currentPoint2.id && basePoint2.id
=== currentPoint1.id) {
        changeRatio(currentPoint1, currentPoint2);
    }
}

Step 5: Change Ratio

Finally we have two distances, we just need to subtract both to find the delta between the first and the seconds, using this value I’ll increase or decrease the zoom attribute of the picture.

function changeRatio(p1, p2) {
    var delta = 0;

    var Seconddistance = calcDistance(p1, p2);
    delta = (Seconddistance - distance) / 10;

    document.querySelector("#output").textContent = "Point 1 - x: " + p1.x
+ " y: " + p1.y + " - " + "Point 2 - x: " + p2.x + " y: "
+ p2.y + " Delta: " + delta;

    if (delta < 0) return;
    if (delta > 10)
        document.querySelector("#image").style.zoom = 10;
    else
        document.querySelector("#image").style.zoom = delta;
}

Download Demo Project

Windows 8 JavaScript–Splash Screen

Windows 8 JavaScript – Message Dialog

Windows 8 JavaScript – Settings