simulator.html 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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="hit" class="bg4" style="padding: 30px; height: 300px; position: relative;">
  11. </div>
  12. <pre id="debug" style="overflow:hidden; background: #eee; padding: 15px;"></pre>
  13. <pre id="log" style="overflow:hidden;"></pre>
  14. </div>
  15. <script src="../../node_modules/hammer-simulator/index.js"></script>
  16. <script src="../../hammer.js"></script>
  17. <script>
  18. var program = [
  19. ['pan', ['deltaX','deltaY']],
  20. ['pinch', ['scale']],
  21. ['tap', ['pos']],
  22. ['swipe', ['deltaX','deltaY']],
  23. ['pinch', ['pos','scale']],
  24. ['pan', ['pos']],
  25. ['rotate', ['pos','rotation']],
  26. ['doubleTap', ['pos']],
  27. ['pinchRotate', ['pos','scale','rotation']],
  28. ];
  29. function randomRange(min, max) {
  30. return Math.random() * (max - min) + min;
  31. }
  32. function randomRangeInt(min, max) {
  33. return Math.round(randomRange(min, max));
  34. }
  35. function gestureOption(name) {
  36. var max = el.offsetWidth * .9;
  37. switch(name) {
  38. case 'deltaY':
  39. case 'deltaX':
  40. return randomRangeInt(10, max * .5);
  41. case 'pos':
  42. return [randomRangeInt(10, max), randomRangeInt(10, max)];
  43. case 'scale':
  44. return randomRange(.2, 2);
  45. case 'rotation':
  46. return randomRange(-180, 180);
  47. }
  48. }
  49. function walkProgram(done) {
  50. var clone = [].concat(program);
  51. (function next() {
  52. if(clone.length) {
  53. var entry = clone.shift();
  54. var options = {};
  55. for(var i=0; i<entry[1].length; i++) {
  56. options[entry[1][i]] = gestureOption(entry[1][i]);
  57. }
  58. setTimeout(function() {
  59. log.innerHTML = '';
  60. Simulator.gestures[entry[0]](el, options, next);
  61. }, 1000);
  62. } else {
  63. done();
  64. }
  65. }());
  66. }
  67. function startSimulator() {
  68. walkProgram(startSimulator);
  69. }
  70. var el = document.querySelector("#hit");
  71. var log = document.querySelector("#log");
  72. var debug = document.querySelector("#debug");
  73. var mc = new Hammer(el);
  74. mc.get('pinch').set({ enable: true, threshold:.1 });
  75. mc.get('rotate').set({ enable: true });
  76. mc.on("swipe pan multipan press pinch rotate tap doubletap", logGesture);
  77. function logGesture(ev) {
  78. log.textContent = ev.toDirString();
  79. }
  80. Object.prototype.toDirString = function() {
  81. var output = [];
  82. for(var key in this) {
  83. if(this.hasOwnProperty(key)) {
  84. var value = this[key];
  85. if(Array.isArray(value)) {
  86. value = "Array("+ value.length +"):"+ value;
  87. } else if(value instanceof HTMLElement) {
  88. value = value +" ("+ value.outerHTML.substring(0, 50) +"...)";
  89. }
  90. output.push(key +": "+ value);
  91. }
  92. }
  93. return output.join("\n")
  94. };
  95. startSimulator();
  96. </script>
  97. </body>
  98. </html>