MSGesture object

Provides high level methods and properties that represent touch, mouse, or pen interaction with elements on a page.

Internet Explorer 10

Syntax


oMsg = new MSGesture();

DOM Information

Inheritance Hierarchy

The MSGesture does not inherit from any class or interface.

Members

The MSGesture object has these types of members:

Methods

The MSGesture object has these methods.

MethodDescription
addPointer

Adds a contact on an element to an MSGesture object.

stop

Stops and resets all ongoing gesture and inertia related events, and fires an onmsgestureend event on the target element.

 

Properties

The MSGesture object has these properties.

PropertyAccess typeDescription

target

Read/write

Get or set the element that you want to fire MSGestureEvents.

 

Remarks

This item described in this topic requires Windows 8. This object is undefined in Internet Explorer 10 on Windows 7.

The MSGesture object provides a way to get higher level gestures such as hold, pan, and tap easily without capturing every pointer event yourself. For example, when a user touches and removes their finger immediately, it fires an onmsgesturetap event. When the user touches the surface and keeps their finger there, it fires an onmsgesturehold event. When the user swipes their finger, onmsgesturestart, onmsgesturechange, and onmsgestureend events are fired. If the user swipes quickly, an onmsinertiastart event is fired. The onmsinertiastart event is followed by a series of onmsgesturechange events, depending on the speed of the swipe, before an onmsgestureend event is fired. The onmsinertiastart event can help your code differentiate between a slow move and a quick flick, and react accordingly, like continuing movement of an object even though the contact with the surface is broken.

Examples

The following is a complete sample that enables you to interact with the screen and see the specific gesture events that are fired. Try tapping and swiping your finger or mouse on the red or blue regions and view the events that are listed.


<!DOCTYPE html >
<html>
<head>
<title>Gesture Event Firing test</title>
<style type="text/css">
.display
{
  /* Block areas from being selected */
  -ms-user-select: none;
 }
 body
 {
   /* Block area from manipulation actions (zoom, pan) */
   touch-action: none;
 }

</style>
<script type="text/javascript">
    var messageId = 0;
    var redGesture, blueGesture;
    var redElement, blueElement;

    function printMessage(str)
       {
    var element = document.getElementById("output");
    messageId++;
    element.value += formatMessage(messageId + ":", 4) + str;
    element.scrollTop = element.scrollHeight;

    element = document.getElementById("eventCount");
    element.innerHTML = messageId;
}

function formatMessage(str, len)
{
    var formattedMessage = null;
    if (str != null)
    {
        var formattedMessage = str.toString();
        formattedMessage += " ";
        for (var idx = formattedMessage.length; idx < len; idx++)
        {
            formattedMessage += " ";
        }
    }
    return formattedMessage;
}

function onLoad() {

    //  Create gesture event listeners for each <div> element
    prepareTarget("Red", redListener);
    prepareTarget("Blue", blueListener);
    // Create MSGesture object 1 (red <div>)
    redGesture = new MSGesture();
    redElement = document.getElementById("Red");
    redGesture.target = redElement;
    // Create MSGesture object 2 (blue <div>)
    blueGesture = new MSGesture();
    blueElement = document.getElementById("Blue");
    blueGesture.target = blueElement;
    
}

//  Add gesture events to an element and point at a specific function
function prepareTarget(targetId, eventListener)
{
    var target = document.getElementById(targetId);
    target.addEventListener("MSGestureStart", eventListener, false);
    target.addEventListener("MSGestureEnd", eventListener, false);
    target.addEventListener("MSGestureChange", eventListener, false);
    target.addEventListener("MSInertiaStart", eventListener, false);
    target.addEventListener("MSGestureTap", eventListener, false);
    target.addEventListener("MSGestureHold", eventListener, false);
    target.addEventListener("pointerdown", eventListener, false);
}

function printEvent(evt)
{
    var str =
        formatMessage(evt.type, 16) + 
        formatMessage(evt.screenX, 6) +
        formatMessage(evt.screenY, 6)  +
        formatMessage(evt.clientX.toFixed(0), 6) +
        formatMessage(evt.clientY.toFixed(0), 6)  +
        formatMessage(evt.translationX.toFixed(2), 8) +
        formatMessage(evt.translationY.toFixed(2), 8)  +
        formatMessage(evt.scale.toFixed(2), 7)  +
        formatMessage(evt.rotation.toFixed(2), 7)  +
        formatMessage(evt.detail, 5) +
        formatMessage(evt.currentTarget.id, 10) +
        formatMessage(evt.srcElement.id, 10) +
        "\n";
    printMessage(str);
    evt.stopPropagation();
}

//  Reset the gesture object
function reset()
{
        printMessage("Reset gesture\n");
        redGesture.reset();
        blueGesture.reset();
}
//  Adds a pointer to the MSGesture object for the red square
function redListener(evt)
{
    if (evt.type == "pointerdown")
    {
        redGesture.addPointer(evt.pointerId);
        return;
    }
    printEvent(evt);
}
//  Adds a pointer to the MSGesture object for the blue square
function blueListener(evt)
{
    if (evt.type == "pointerdown")  // add pointer on pointerdown event
    {
        blueGesture.addPointer(evt.pointerId);
        return;
    }
    printEvent(evt);  //  Otherwise pass on event
}

function clearMessages(evt)
// Clear the list of gesture messages
{
    var results = document.getElementById("output");
    results.innerHTML = "";
    messageId = 0;
    results = document.getElementById("eventCount");
    results.innerHTML = messageId;
}
</script>
</head>

<body id = "page" onload="onLoad()">
    <!-- Elements to try out gestures -->
    <div class= "display" id="Red" style="position: relative; top: 0px; left: 0px; width: 300px; height: 250px; background-color: red"></div>
    <div class= "display" id="Blue" style="position: absolute; top: 10px; left: 350px; width: 300px; height: 250px; background-color: blue"></div>
    <div class= "display" id="control" style="position: absolute; top: 50px; left: 750px; width: 300px; height: 250px;">
        <button onclick=clearMessages()>Clear Messages</button>
        <br />
        <p>Gesture Event Count: <span id="eventCount"></span></p>
    </div>

    <strong><pre><font size="2" face="Courier">
        Type            ScrX  ScrY  CliX  CliY  TrnX    TrnY    Scal   Rotn   Det  Current  Source 
    <textarea id=output rows="18" cols="140" wrap="off" readonly="readonly" ></textarea>
    </font></pre></strong>
</body>
</html>


See also

msTouchAction
Gesture flags

 

 

Show:
© 2014 Microsoft