Chromium Code Reviews| 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 1354 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1365 | 1365 |
| 1366 /** | 1366 /** |
| 1367 * Event on beginning of the current gesture. | 1367 * Event on beginning of the current gesture. |
| 1368 * The variable is updated when the number of touch finger changed. | 1368 * The variable is updated when the number of touch finger changed. |
| 1369 * @type {TouchEvent} | 1369 * @type {TouchEvent} |
| 1370 * @private | 1370 * @private |
| 1371 */ | 1371 */ |
| 1372 this.gestureStartEvent_ = null; | 1372 this.gestureStartEvent_ = null; |
| 1373 | 1373 |
| 1374 /** | 1374 /** |
| 1375 * Rotation value on beginning of the current gesture. | |
| 1376 * @type {number} | |
| 1377 * @private | |
| 1378 */ | |
| 1379 this.gestureStartRotation_ = 0; | |
| 1380 | |
| 1381 /** | |
| 1375 * Last touch event. | 1382 * Last touch event. |
| 1376 * @type {TouchEvent} | 1383 * @type {TouchEvent} |
| 1377 * @private | 1384 * @private |
| 1378 */ | 1385 */ |
| 1379 this.lastEvent_ = null; | 1386 this.lastEvent_ = null; |
| 1380 | 1387 |
| 1381 /** | 1388 /** |
| 1382 * Zoom value just after last touch event. | 1389 * Zoom value just after last touch event. |
| 1383 * @type {number} | 1390 * @type {number} |
| 1384 * @private | 1391 * @private |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1418 * @param {boolean} flag New value. | 1425 * @param {boolean} flag New value. |
| 1419 */ | 1426 */ |
| 1420 set enabled(flag) { | 1427 set enabled(flag) { |
| 1421 this.enabled_ = flag; | 1428 this.enabled_ = flag; |
| 1422 if (!this.enabled_) | 1429 if (!this.enabled_) |
| 1423 this.stopOperation(); | 1430 this.stopOperation(); |
| 1424 } | 1431 } |
| 1425 }; | 1432 }; |
| 1426 | 1433 |
| 1427 /** | 1434 /** |
| 1435 * Obtains the radian of the pinch twist angle. | |
| 1436 * @param {TouchEvent} event1 Start touch event. It should include more than two | |
| 1437 * touches. | |
| 1438 * @param {TouchEvent} event2 Current touch event. It should include more than | |
| 1439 * two touches. | |
| 1440 * @return {number} Radian of the pinch twist angle. | |
|
mtomasz
2014/07/24 09:59:25
Here we return radians, but in other places we wor
hirono
2014/07/24 10:13:08
Let me choose degrees that works fine with CSS tra
| |
| 1441 */ | |
| 1442 TouchHandler.getTwistRadian = function(event1, event2) { | |
| 1443 var dx1 = event1.touches[1].clientX - event1.touches[0].clientX; | |
| 1444 var dy1 = event1.touches[1].clientY - event1.touches[0].clientY; | |
| 1445 var dx2 = event2.touches[1].clientX - event2.touches[0].clientX; | |
| 1446 var dy2 = event2.touches[1].clientY - event2.touches[0].clientY; | |
| 1447 var innerProduce = dx1 * dx2 + dy1 * dy2; // |v1| * |v2| * cos(t) = x / r | |
|
mtomasz
2014/07/24 09:59:25
nit: Did you mean inner product?
hirono
2014/07/24 10:13:08
Oops, yes.
| |
| 1448 var outerProduce = dx1 * dy2 - dy1 * dx2; // |v1| * |v2| * sin(t) = y / r | |
| 1449 return Math.atan2(outerProduce, innerProduce); // atan(y / x) | |
| 1450 }; | |
| 1451 | |
| 1452 /** | |
| 1428 * Stops the current touch operation. | 1453 * Stops the current touch operation. |
| 1429 */ | 1454 */ |
| 1430 TouchHandler.prototype.stopOperation = function() { | 1455 TouchHandler.prototype.stopOperation = function() { |
| 1431 this.touchStarted_ = false; | 1456 this.touchStarted_ = false; |
| 1432 this.done_ = false; | 1457 this.done_ = false; |
| 1433 this.gestureStartEvent_ = null; | 1458 this.gestureStartEvent_ = null; |
| 1434 this.lastEvent_ = null; | 1459 this.lastEvent_ = null; |
| 1435 this.lastZoom_ = 1.0; | 1460 this.lastZoom_ = 1.0; |
| 1436 }; | 1461 }; |
| 1437 | 1462 |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 1449 if (!this.touchStarted_) | 1474 if (!this.touchStarted_) |
| 1450 return; | 1475 return; |
| 1451 | 1476 |
| 1452 // Check if the current touch operation ends with the event. | 1477 // Check if the current touch operation ends with the event. |
| 1453 if (event.touches.length === 0) { | 1478 if (event.touches.length === 0) { |
| 1454 this.stopOperation(); | 1479 this.stopOperation(); |
| 1455 return; | 1480 return; |
| 1456 } | 1481 } |
| 1457 | 1482 |
| 1458 // Check if a new gesture started or not. | 1483 // Check if a new gesture started or not. |
| 1484 var viewport = this.slideMode_.getViewport(); | |
| 1459 if (!this.lastEvent_ || | 1485 if (!this.lastEvent_ || |
| 1460 this.lastEvent_.touches.length !== event.touches.length) { | 1486 this.lastEvent_.touches.length !== event.touches.length) { |
| 1461 if (event.touches.length === 2 || | 1487 if (event.touches.length === 2 || |
| 1462 event.touches.length === 1) { | 1488 event.touches.length === 1) { |
| 1463 this.gestureStartEvent_ = event; | 1489 this.gestureStartEvent_ = event; |
| 1490 this.gestureStartRotation_ = viewport.getRotation(); | |
| 1464 this.lastEvent_ = event; | 1491 this.lastEvent_ = event; |
| 1465 this.lastZoom_ = this.slideMode_.getViewport().getZoom(); | 1492 this.lastZoom_ = viewport.getZoom(); |
| 1466 } else { | 1493 } else { |
| 1467 this.gestureStartEvent_ = null; | 1494 this.gestureStartEvent_ = null; |
| 1495 this.gestureStartRotation_ = 0; | |
| 1468 this.lastEvent_ = null; | 1496 this.lastEvent_ = null; |
| 1469 this.lastZoom_ = 1.0; | 1497 this.lastZoom_ = 1.0; |
| 1470 } | 1498 } |
| 1471 return; | 1499 return; |
| 1472 } | 1500 } |
| 1473 | 1501 |
| 1474 // Handle the gesture movement. | 1502 // Handle the gesture movement. |
| 1475 var viewport = this.slideMode_.getViewport(); | |
| 1476 switch (event.touches.length) { | 1503 switch (event.touches.length) { |
| 1477 case 1: | 1504 case 1: |
| 1478 if (viewport.isZoomed()) { | 1505 if (viewport.isZoomed()) { |
| 1479 // Scrolling an image by swipe. | 1506 // Scrolling an image by swipe. |
| 1480 var dx = event.touches[0].screenX - this.lastEvent_.touches[0].screenX; | 1507 var dx = event.touches[0].screenX - this.lastEvent_.touches[0].screenX; |
| 1481 var dy = event.touches[0].screenY - this.lastEvent_.touches[0].screenY; | 1508 var dy = event.touches[0].screenY - this.lastEvent_.touches[0].screenY; |
| 1482 viewport.setOffset( | 1509 viewport.setOffset( |
| 1483 viewport.getOffsetX() + dx, viewport.getOffsetY() + dy); | 1510 viewport.getOffsetX() + dx, viewport.getOffsetY() + dy); |
| 1484 this.slideMode_.applyViewportChange(); | 1511 this.slideMode_.applyViewportChange(); |
| 1485 } else { | 1512 } else { |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 1500 break; | 1527 break; |
| 1501 | 1528 |
| 1502 case 2: | 1529 case 2: |
| 1503 // Pinch zoom. | 1530 // Pinch zoom. |
| 1504 var distance1 = TouchHandler.getDistance(this.lastEvent_); | 1531 var distance1 = TouchHandler.getDistance(this.lastEvent_); |
| 1505 var distance2 = TouchHandler.getDistance(event); | 1532 var distance2 = TouchHandler.getDistance(event); |
| 1506 if (distance1 === 0) | 1533 if (distance1 === 0) |
| 1507 break; | 1534 break; |
| 1508 var zoom = distance2 / distance1 * this.lastZoom_; | 1535 var zoom = distance2 / distance1 * this.lastZoom_; |
| 1509 viewport.setZoom(zoom); | 1536 viewport.setZoom(zoom); |
| 1537 | |
| 1538 // Pinch rotation. | |
| 1539 var radian = TouchHandler.getTwistRadian(this.gestureStartEvent_, event); | |
| 1540 if (radian > Math.PI / 8) | |
| 1541 viewport.setRotation(this.gestureStartRotation_ + 1); | |
| 1542 else if (radian < -Math.PI / 8) | |
| 1543 viewport.setRotation(this.gestureStartRotation_ - 1); | |
| 1544 else | |
| 1545 viewport.setRotation(this.gestureStartRotation_); | |
| 1510 this.slideMode_.applyViewportChange(); | 1546 this.slideMode_.applyViewportChange(); |
| 1511 break; | 1547 break; |
| 1512 } | 1548 } |
| 1513 | 1549 |
| 1514 // Update the last event. | 1550 // Update the last event. |
| 1515 this.lastEvent_ = event; | 1551 this.lastEvent_ = event; |
| 1516 this.lastZoom_ = viewport.getZoom(); | 1552 this.lastZoom_ = viewport.getZoom(); |
| 1517 }; | 1553 }; |
| OLD | NEW |