#

API Documentation for: 0.8.1
Show: Inherited Protected Private Deprecated

# File:Touch.js

    /*
    * Touch
    * Visit http://createjs.com/ for documentation, updates and examples.
    *
    * Copyright (c) 2010 gskinner.com, inc.
    *
    * Permission is hereby granted, free of charge, to any person
    * obtaining a copy of this software and associated documentation
    * files (the "Software"), to deal in the Software without
    * restriction, including without limitation the rights to use,
    * copy, modify, merge, publish, distribute, sublicense, and/or sell
    * copies of the Software, and to permit persons to whom the
    * Software is furnished to do so, subject to the following
    * conditions:
    *
    * The above copyright notice and this permission notice shall be
    * included in all copies or substantial portions of the Software.
    *
    * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
    * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
    * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
    * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
    * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
    * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
    * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
    * OTHER DEALINGS IN THE SOFTWARE.
    */

    /**
     * @module EaselJS
     */

    // namespace:
    //this.createjs = this.createjs||{};

    (function() {
    	"use strict";

    // constructor:
    	/**
     * Global utility for working with multi-touch enabled devices in EaselJS. Currently supports W3C Touch API (iOS and
     * modern Android browser) and the Pointer API (IE), including ms-prefixed events in IE10, and unprefixed in IE11.
     *
     * Ensure that you {{#crossLink "Touch/disable"}}{{/crossLink}} touch when cleaning up your application. You do not have
     * to check if touch is supported to enable it, as it will fail gracefully if it is not supported.
     *
     * <h4>Example</h4>
     *
     *      var stage = new createjs.Stage("canvasId");
     *      createjs.Touch.enable(stage);
     *
     * <strong>Note:</strong> It is important to disable Touch on a stage that you are no longer using:
     *
     *      createjs.Touch.disable(stage);
     *
     * @class Touch
     * @static
     **/
    	function Touch() {
    		throw "Touch cannot be instantiated";
    	}

    // public static methods:
    	/**
    	 * Returns `true` if touch is supported in the current browser.
    	 * @method isSupported
    	 * @return {Boolean} Indicates whether touch is supported in the current browser.
    	 * @static
    	 **/
    	Touch.isSupported = function() {
    		return	!!('ontouchstart' in window); // iOS & Android; // IE11+
    	};

    	Touch.isMove=false;

        Touch.currentX=0;
        Touch.currentY=0;

    	/**
    	 * Enables touch interaction for the specified EaselJS {{#crossLink "Stage"}}{{/crossLink}}. Currently supports iOS
    	 * (and compatible browsers, such as modern Android browsers), and IE10/11. Supports both single touch and
    	 * multi-touch modes. Extends the EaselJS {{#crossLink "MouseEvent"}}{{/crossLink}} model, but without support for
    	 * double click or over/out events. See the MouseEvent {{#crossLink "MouseEvent/pointerId:property"}}{{/crossLink}}
    	 * for more information.
    	 * @method enable
    	 * @param {Stage} stage The {{#crossLink "Stage"}}{{/crossLink}} to enable touch on.
    	 * @param {Boolean} [singleTouch=false] If `true`, only a single touch will be active at a time.
    	 * @param {Boolean} [allowDefault=false] If `true`, then default gesture actions (ex. scrolling, zooming) will be
    	 * allowed when the user is interacting with the target canvas.
    	 * @return {Boolean} Returns `true` if touch was successfully enabled on the target stage.
    	 * @static
    	 **/
    	Touch.enable = function(stage, singleTouch, allowDefault) {
    		if (!stage || !stage.canvas || !Touch.isSupported()) { return false; }
    		if (stage.__touch) { return true; }

    		// inject required properties on stage:
    		stage.__touch = {pointers:{}, multitouch:!singleTouch, preventDefault:!allowDefault, count:0};

    		// note that in the future we may need to disable the standard mouse event model before adding
    		// these to prevent duplicate calls. It doesn't seem to be an issue with iOS devices though.
    		if ('ontouchstart' in window) { Touch._IOS_enable(stage); }
    		return true;
    	};

    	/**
    	 * Removes all listeners that were set up when calling `Touch.enable()` on a stage.
    	 * @method disable
    	 * @param {Stage} stage The {{#crossLink "Stage"}}{{/crossLink}} to disable touch on.
    	 * @static
    	 **/
    	Touch.disable = function(stage) {
    		if (!stage) { return; }
    		if ('ontouchstart' in window) { Touch._IOS_disable(stage); }

    		delete stage.__touch;
    	};

    // Private static methods:
    	/**
    	 * @method _IOS_enable
    	 * @protected
    	 * @param {Stage} stage
    	 * @static
    	 **/
    	Touch._IOS_enable = function(stage) {
    		var canvas = stage.canvas;
    		var f = stage.__touch.f = function(e) { Touch._IOS_handleEvent(stage,e); };
    		canvas.addEventListener("touchstart", f, false);
    		canvas.addEventListener("touchmove", f, false);
    		canvas.addEventListener("touchend", f, false);
    		canvas.addEventListener("touchcancel", f, false);
    	};

    	/**
    	 * @method _IOS_disable
    	 * @protected
    	 * @param {Stage} stage
    	 * @static
    	 **/
    	Touch._IOS_disable = function(stage) {
    		var canvas = stage.canvas;
    		if (!canvas) { return; }
    		var f = stage.__touch.f;
    		canvas.removeEventListener("touchstart", f, false);
    		canvas.removeEventListener("touchmove", f, false);
    		canvas.removeEventListener("touchend", f, false);
    		canvas.removeEventListener("touchcancel", f, false);
    	};

    	/**
    	 * @method _IOS_handleEvent
    	 * @param {Stage} stage
    	 * @param {Object} e The event to handle
    	 * @protected
    	 * @static
    	 **/
    	Touch._IOS_handleEvent = function(stage, e) {
    		if (!stage) { return; }
    		//if (stage.__touch.preventDefault) { e.preventDefault&&e.preventDefault(); }
    		var touches = e.changedTouches;
    		var type = e.type;

    		for (var i= 0,l=touches.length; i<l; i++) {
    			var touch = touches[i];
    			var id = touch.identifier;
    			if (touch.target != stage.canvas) { continue; }
    			if (type == "touchstart") {
    				this._handleStart(stage, id, e, touch.pageX, touch.pageY);
    			} else if (type == "touchmove") {
    				this._handleMove(stage, id, e, touch.pageX, touch.pageY);
    			} else if (type == "touchend" || type == "touchcancel") {
    				this._handleEnd(stage, id, e, touch.pageX, touch.pageY);
    			}
    		}
    	};

    	/**
    	 * @method _handleStart
    	 * @param {Stage} stage
    	 * @param {String|Number} id
    	 * @param {Object} e
    	 * @param {Number} x
    	 * @param {Number} y
    	 * @protected
    	 **/
    	Touch._handleStart = function(stage, id, e, x, y) {
            Touch.currentX=x;
            Touch.currentY=y;
    		this.isMove=false;
    	};

    	/**
    	 * @method _handleMove
    	 * @param {Stage} stage
    	 * @param {String|Number} id
    	 * @param {Object} e
    	 * @param {Number} x
    	 * @param {Number} y
    	 * @protected
    	 **/
    	Touch._handleMove = function(stage, id, e, x, y) {
    		this.isMove=true;
    	};

    	/**
    	 * @method _handleEnd
    	 * @param {Stage} stage
    	 * @param {String|Number} id
    	 * @param {Object} e
    	 * @protected
    	 **/
    	Touch._handleEnd = function(stage, id, e,x,y) {
    		if( Touch.currentX-5<x && x<Touch.currentX+5  &&  Touch.currentY-5<y && y<Touch.currentY+5){
    			var _this=stage;
    			if (_this._primaryPointerID == null || id === -1) { _this._primaryPointerID = id; } // mouse always takes over.
    			if (y != null) { _this._updatePointerPosition(id, e, Touch.currentX, Touch.currentY); }
    			var target = null, o = _this._getPointerData(id);
    			target = o.target = _this._getObjectsUnderPoint(o.x, o.y, null, true);
    			if (target) { _this._dispatchMouseEvent(target, "click", true, id, o, e); }
    			_this=null;
    			target=null;
    			o=null;
    		}
    	};

    	createjs.Touch = Touch;
    }());