OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 'use strict'; | 5 'use strict'; |
6 | 6 |
7 /** | 7 /** |
8 * Slide mode displays a single image and has a set of controls to navigate | 8 * Slide mode displays a single image and has a set of controls to navigate |
9 * between the images and to edit an image. | 9 * between the images and to edit an image. |
10 * | 10 * |
(...skipping 1367 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1378 * Zoom value just after last touch event. | 1378 * Zoom value just after last touch event. |
1379 * @type {number} | 1379 * @type {number} |
1380 * @private | 1380 * @private |
1381 */ | 1381 */ |
1382 this.lastZoom_ = 1.0; | 1382 this.lastZoom_ = 1.0; |
1383 | 1383 |
1384 targetElement.addEventListener('touchstart', this.onTouchStart_.bind(this)); | 1384 targetElement.addEventListener('touchstart', this.onTouchStart_.bind(this)); |
1385 var onTouchEventBound = this.onTouchEvent_.bind(this); | 1385 var onTouchEventBound = this.onTouchEvent_.bind(this); |
1386 targetElement.ownerDocument.addEventListener('touchmove', onTouchEventBound); | 1386 targetElement.ownerDocument.addEventListener('touchmove', onTouchEventBound); |
1387 targetElement.ownerDocument.addEventListener('touchend', onTouchEventBound); | 1387 targetElement.ownerDocument.addEventListener('touchend', onTouchEventBound); |
| 1388 |
| 1389 targetElement.addEventListener('mousewheel', this.onMouseWheel_.bind(this)); |
1388 } | 1390 } |
1389 | 1391 |
1390 /** | 1392 /** |
1391 * If the user touched the image and moved the finger more than SWIPE_THRESHOLD | 1393 * If the user touched the image and moved the finger more than SWIPE_THRESHOLD |
1392 * horizontally it's considered as a swipe gesture (change the current image). | 1394 * horizontally it's considered as a swipe gesture (change the current image). |
1393 * @type {number} | 1395 * @type {number} |
1394 * @const | 1396 * @const |
1395 */ | 1397 */ |
1396 TouchHandler.SWIPE_THRESHOLD = 100; | 1398 TouchHandler.SWIPE_THRESHOLD = 100; |
1397 | 1399 |
(...skipping 11 matching lines...) Expand all Loading... |
1409 * @return {boolean} Distance between touch[0] and touch[1]. | 1411 * @return {boolean} Distance between touch[0] and touch[1]. |
1410 */ | 1412 */ |
1411 TouchHandler.getDistance = function(event) { | 1413 TouchHandler.getDistance = function(event) { |
1412 var touch1 = event.touches[0]; | 1414 var touch1 = event.touches[0]; |
1413 var touch2 = event.touches[1]; | 1415 var touch2 = event.touches[1]; |
1414 var dx = touch1.clientX - touch2.clientX; | 1416 var dx = touch1.clientX - touch2.clientX; |
1415 var dy = touch1.clientY - touch2.clientY; | 1417 var dy = touch1.clientY - touch2.clientY; |
1416 return Math.sqrt(dx * dx + dy * dy); | 1418 return Math.sqrt(dx * dx + dy * dy); |
1417 }; | 1419 }; |
1418 | 1420 |
1419 TouchHandler.prototype = { | |
1420 /** | |
1421 * @param {boolean} flag New value. | |
1422 */ | |
1423 set enabled(flag) { | |
1424 this.enabled_ = flag; | |
1425 if (!this.enabled_) | |
1426 this.stopOperation(); | |
1427 } | |
1428 }; | |
1429 | |
1430 /** | 1421 /** |
1431 * Obtains the degrees of the pinch twist angle. | 1422 * Obtains the degrees of the pinch twist angle. |
1432 * @param {TouchEvent} event1 Start touch event. It should include more than two | 1423 * @param {TouchEvent} event1 Start touch event. It should include more than two |
1433 * touches. | 1424 * touches. |
1434 * @param {TouchEvent} event2 Current touch event. It should include more than | 1425 * @param {TouchEvent} event2 Current touch event. It should include more than |
1435 * two touches. | 1426 * two touches. |
1436 * @return {number} Degrees of the pinch twist angle. | 1427 * @return {number} Degrees of the pinch twist angle. |
1437 */ | 1428 */ |
1438 TouchHandler.getTwistAngle = function(event1, event2) { | 1429 TouchHandler.getTwistAngle = function(event1, event2) { |
1439 var dx1 = event1.touches[1].clientX - event1.touches[0].clientX; | 1430 var dx1 = event1.touches[1].clientX - event1.touches[0].clientX; |
1440 var dy1 = event1.touches[1].clientY - event1.touches[0].clientY; | 1431 var dy1 = event1.touches[1].clientY - event1.touches[0].clientY; |
1441 var dx2 = event2.touches[1].clientX - event2.touches[0].clientX; | 1432 var dx2 = event2.touches[1].clientX - event2.touches[0].clientX; |
1442 var dy2 = event2.touches[1].clientY - event2.touches[0].clientY; | 1433 var dy2 = event2.touches[1].clientY - event2.touches[0].clientY; |
1443 var innerProduct = dx1 * dx2 + dy1 * dy2; // |v1| * |v2| * cos(t) = x / r | 1434 var innerProduct = dx1 * dx2 + dy1 * dy2; // |v1| * |v2| * cos(t) = x / r |
1444 var outerProduct = dx1 * dy2 - dy1 * dx2; // |v1| * |v2| * sin(t) = y / r | 1435 var outerProduct = dx1 * dy2 - dy1 * dx2; // |v1| * |v2| * sin(t) = y / r |
1445 return Math.atan2(outerProduct, innerProduct) * 180 / Math.PI; // atan(y / x) | 1436 return Math.atan2(outerProduct, innerProduct) * 180 / Math.PI; // atan(y / x) |
1446 }; | 1437 }; |
1447 | 1438 |
| 1439 TouchHandler.prototype = { |
| 1440 /** |
| 1441 * @param {boolean} flag New value. |
| 1442 */ |
| 1443 set enabled(flag) { |
| 1444 this.enabled_ = flag; |
| 1445 if (!this.enabled_) |
| 1446 this.stopOperation(); |
| 1447 } |
| 1448 }; |
| 1449 |
1448 /** | 1450 /** |
1449 * Stops the current touch operation. | 1451 * Stops the current touch operation. |
1450 */ | 1452 */ |
1451 TouchHandler.prototype.stopOperation = function() { | 1453 TouchHandler.prototype.stopOperation = function() { |
1452 this.touchStarted_ = false; | 1454 this.touchStarted_ = false; |
1453 this.done_ = false; | 1455 this.done_ = false; |
1454 this.gestureStartEvent_ = null; | 1456 this.gestureStartEvent_ = null; |
1455 this.lastEvent_ = null; | 1457 this.lastEvent_ = null; |
1456 this.lastZoom_ = 1.0; | 1458 this.lastZoom_ = 1.0; |
1457 }; | 1459 }; |
1458 | 1460 |
| 1461 /** |
| 1462 * Handles touch start events. |
| 1463 * @param {TouchEvent} event Touch event. |
| 1464 * @private |
| 1465 */ |
1459 TouchHandler.prototype.onTouchStart_ = function(event) { | 1466 TouchHandler.prototype.onTouchStart_ = function(event) { |
1460 if (this.enabled_ && event.touches.length === 1) | 1467 if (this.enabled_ && event.touches.length === 1) |
1461 this.touchStarted_ = true; | 1468 this.touchStarted_ = true; |
1462 }; | 1469 }; |
1463 | 1470 |
1464 /** | 1471 /** |
1465 * @param {event} event Touch event. | 1472 * Handles touch move and touch end events. |
| 1473 * @param {TouchEvent} event Touch event. |
| 1474 * @private |
1466 */ | 1475 */ |
1467 TouchHandler.prototype.onTouchEvent_ = function(event) { | 1476 TouchHandler.prototype.onTouchEvent_ = function(event) { |
1468 // Check if the current touch operation started from the target element or | 1477 // Check if the current touch operation started from the target element or |
1469 // not. | 1478 // not. |
1470 if (!this.touchStarted_) | 1479 if (!this.touchStarted_) |
1471 return; | 1480 return; |
1472 | 1481 |
1473 // Check if the current touch operation ends with the event. | 1482 // Check if the current touch operation ends with the event. |
1474 if (event.touches.length === 0) { | 1483 if (event.touches.length === 0) { |
1475 this.stopOperation(); | 1484 this.stopOperation(); |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1540 else | 1549 else |
1541 viewport.setRotation(this.gestureStartRotation_); | 1550 viewport.setRotation(this.gestureStartRotation_); |
1542 this.slideMode_.applyViewportChange(); | 1551 this.slideMode_.applyViewportChange(); |
1543 break; | 1552 break; |
1544 } | 1553 } |
1545 | 1554 |
1546 // Update the last event. | 1555 // Update the last event. |
1547 this.lastEvent_ = event; | 1556 this.lastEvent_ = event; |
1548 this.lastZoom_ = viewport.getZoom(); | 1557 this.lastZoom_ = viewport.getZoom(); |
1549 }; | 1558 }; |
| 1559 |
| 1560 /** |
| 1561 * Handles mouse wheel events. |
| 1562 * @param {MouseEvent} event Wheel event. |
| 1563 * @private |
| 1564 */ |
| 1565 TouchHandler.prototype.onMouseWheel_ = function(event) { |
| 1566 var viewport = this.slideMode_.getViewport(); |
| 1567 if (!this.enabled_ || !viewport.isZoomed()) |
| 1568 return; |
| 1569 this.stopOperation(); |
| 1570 viewport.setOffset( |
| 1571 viewport.getOffsetX() + event.wheelDeltaX, |
| 1572 viewport.getOffsetY() + event.wheelDeltaY); |
| 1573 this.slideMode_.applyViewportChange(); |
| 1574 }; |
OLD | NEW |