1 /* ***************************************************************************
2 * Copyright (c) 2000 - 2013 Samsung Electronics Co., Ltd.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 * DEALINGS IN THE SOFTWARE.
21 * ***************************************************************************
23 * Author: Minkyu Kang <mk7.kang@samsung.com>
30 * pinchstart: triggered when start the touched two points
31 * pinch: triggered when move the touch point after pinchstart event occured
32 * pinchend: triggered when touchend event after pinchstart event occured
35 * point: touched points
36 * ratio: origin point-to-current point ratio for moving distance
38 * $("#pinch").bind("pinch", function (e, p) {
39 * console.log("point[0].x: " + p.point[0].x);
40 * console.log("point[0].y: " + p.point[0].y);
41 * console.log("point[1].x: " + p.point[1].x);
42 * console.log("point[1].y: " + p.point[1].y);
43 * console.log("ratio: " + p.ratio);
47 * $.mobile.pinch.enabled: true or false
48 * $.mobile.pinch.min: minimum value of ratio
49 * $.mobile.pinch.max: maximum value of ratio
50 * $.mobile.pinch.factor: scale factor of ratio
51 * $.mobile.pinch.threshold: move threshold of ratio
52 * $.mobile.pinch.interval: interval for pinch event
55 ( function( $, window, undefined ) {
59 var thisObject = this,
60 $this = $( thisObject );
62 if ( !$.mobile.support.touch ) {
66 function getDistance( point ) {
67 var x = point[0].x - point[1].x,
68 y = point[0].y - point[0].y;
70 return Math.sqrt( ( x * x ) + ( y * y ) );
73 function getParameter( point, ratio ) {
74 return { point: point, ratio: ratio };
77 $this.bind( "touchstart", function ( event ) {
78 var data = event.originalEvent.touches,
83 if ( !$.mobile.pinch.enabled ) {
87 if ( data.length != 2 ) {
92 { x: data[0].pageX, y: data[0].pageY },
93 { x: data[1].pageX, y: data[1].pageY }
96 $( event.target ).trigger( "pinchstart", getParameter( origin, undefined ) );
98 function pinchHandler( event ) {
99 var data = event.originalEvent.touches,
103 factor = $( window ).width() / $.mobile.pinch.factor;
114 { x: data[0].pageX, y: data[0].pageY },
115 { x: data[1].pageX, y: data[1].pageY }
118 delta = getDistance( current ) - getDistance( origin );
120 ratio = 1 + delta / factor;
122 if ( ratio < $.mobile.pinch.min ) {
123 ratio = $.mobile.pinch.min;
124 } else if ( ratio > $.mobile.pinch.max ) {
125 ratio = $.mobile.pinch.max;
128 if ( Math.abs( ratio - last_ratio ) < $.mobile.pinch.threshold ) {
132 $( event.target ).trigger( "pinch", getParameter( current, ratio ) );
136 if ( $.mobile.pinch.interval ) {
139 setTimeout( function () {
141 }, $.mobile.pinch.interval );
145 $this.bind( "touchmove", pinchHandler )
146 .one( "touchend", function ( event ) {
147 $this.unbind( "touchmove", pinchHandler );
148 $( event.target ).trigger( "pinchend",
149 getParameter( undefined, last_ratio ) );
160 $.event.special["pinch"] = pinch_event;