simulator-googlemaps.html 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <!DOCTYPE html>
  2. <html>
  3. <head lang="en">
  4. <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1, maximum-scale=1">
  5. <link rel="stylesheet" href="assets/style.css">
  6. <title>Hammer.js</title>
  7. </head>
  8. <body>
  9. <div class="container">
  10. <div id="maps" style="height: 500px; margin-bottom: 20px;"></div>
  11. <h1>Gestures simulator</h1>
  12. <p>Used for unit-testing Hammer.js. To test it on the Google Maps view, you should open your
  13. <a href="https://developer.chrome.com/devtools/docs/mobile-emulation#emulate-touch-events">
  14. Inspector and emulate a touch-screen.</a>
  15. Or just open it on your touch-device.</p>
  16. <p>Currently, it only triggers touchEvents.</p>
  17. </div>
  18. <script src="../../node_modules/hammer-simulator/index.js"></script>
  19. <script src="https://maps.googleapis.com/maps/api/js?v=3.exp"></script>
  20. <script>
  21. Simulator.events.touch.fakeSupport();
  22. var el, map;
  23. var container = document.getElementById('maps');
  24. var program = [
  25. ['pan', ['deltaX','deltaY']],
  26. ['pinch', ['scale']],
  27. ['tap', ['pos']],
  28. ['swipe', ['deltaX','deltaY']],
  29. ['pinch', ['pos','scale']],
  30. ['pan', ['pos']],
  31. ['rotate', ['pos','rotation']],
  32. ['doubleTap', ['pos']],
  33. ['pinchRotate', ['pos','scale','rotation']],
  34. ];
  35. function randomRange(min, max) {
  36. return Math.random() * (max - min) + min;
  37. }
  38. function randomRangeInt(min, max) {
  39. return Math.round(randomRange(min, max));
  40. }
  41. function gestureOption(name) {
  42. var max = map.getDiv().offsetWidth * .9;
  43. switch(name) {
  44. case 'deltaY':
  45. case 'deltaX':
  46. return randomRangeInt(10, max * .5);
  47. case 'pos':
  48. return [randomRangeInt(10, max), randomRangeInt(10, max)];
  49. case 'scale':
  50. return randomRange(.2, 2);
  51. case 'rotation':
  52. return randomRange(-180, 180);
  53. }
  54. }
  55. function walkProgram(done) {
  56. var clone = [].concat(program);
  57. (function next() {
  58. if(clone.length) {
  59. var entry = clone.shift();
  60. var options = {};
  61. for(var i=0; i<entry[1].length; i++) {
  62. options[entry[1][i]] = gestureOption(entry[1][i]);
  63. }
  64. Simulator.gestures[entry[0]](el, options, next);
  65. } else {
  66. done();
  67. }
  68. }());
  69. }
  70. function startSimulator() {
  71. walkProgram(startSimulator);
  72. }
  73. (function setupGoogleMaps() {
  74. map = new google.maps.Map(container, {
  75. zoom: 14,
  76. center: new google.maps.LatLng(51.98, 5.91)
  77. });
  78. // collect the target element
  79. google.maps.event.addListenerOnce(map, 'idle', function(){
  80. el = container.childNodes[0].childNodes[0];
  81. startSimulator();
  82. });
  83. })();
  84. </script>
  85. </body>
  86. </html>