deviceorientation.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. /*
  2. * Copyright 2018 Scytl Secure Electronic Voting SA
  3. *
  4. * All rights reserved
  5. *
  6. * See our extended copyright notice in *file 'Copyright.txt' which is part of this source code package
  7. */
  8. /* jshint node:true */
  9. 'use strict';
  10. var math = require('./math');
  11. module.exports = DeviceOrientationCollector;
  12. var deviceOrientationX_, deviceOrientationY_, deviceOrientationZ_;
  13. var collectEntropy_ = function() {};
  14. function DeviceOrientationCollector(collectEntropyCallback) {
  15. collectEntropy_ = collectEntropyCallback;
  16. }
  17. DeviceOrientationCollector.prototype = {
  18. handleEvent: function(ev) {
  19. ev = ev || window.event;
  20. var currentX =
  21. (math.entropyRound(ev.gamma) || math.entropyRound(ev.x) || 0);
  22. var currentY = (math.entropyRound(ev.beta) || math.entropyRound(ev.y) || 0);
  23. var currentZ =
  24. (math.entropyRound(ev.alpha) || math.entropyRound(ev.z) || 0);
  25. // The API detects and delivers accelerometer data 50 times per
  26. // second
  27. // even if there is any event related, so this is
  28. // a way to control if it really changed or not
  29. if ((deviceOrientationX_ === null) || (deviceOrientationY_ === null) ||
  30. (deviceOrientationZ_ === null) || (deviceOrientationX_ !== currentX) ||
  31. (deviceOrientationY_ !== currentY) ||
  32. (deviceOrientationZ_ !== currentZ)) {
  33. deviceOrientationX_ = currentX;
  34. deviceOrientationY_ = currentY;
  35. deviceOrientationZ_ = currentZ;
  36. collectEntropy_(
  37. currentX + currentY + currentZ + (+new Date()), 1);
  38. }
  39. }
  40. };