Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

util.js 2.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /* global $ */
  2. /**
  3. * Utility functions.
  4. */
  5. var Util = (function (my) {
  6. /**
  7. * Returns the text width for the given element.
  8. *
  9. * @param el the element
  10. */
  11. my.getTextWidth = function (el) {
  12. return (el.clientWidth + 1);
  13. };
  14. /**
  15. * Returns the text height for the given element.
  16. *
  17. * @param el the element
  18. */
  19. my.getTextHeight = function (el) {
  20. return (el.clientHeight + 1);
  21. };
  22. /**
  23. * Casts the given number to integer.
  24. *
  25. * @param number the number to cast
  26. */
  27. my.toInteger = function (number) {
  28. return Math.round(Number(number));
  29. };
  30. /**
  31. * Plays the sound given by id.
  32. *
  33. * @param id the identifier of the audio element.
  34. */
  35. my.playSoundNotification = function (id) {
  36. document.getElementById(id).play();
  37. };
  38. /**
  39. * Escapes the given text.
  40. */
  41. my.escapeHtml = function (unsafeText) {
  42. return $('<div/>').text(unsafeText).html();
  43. };
  44. /**
  45. * Returns the available video width.
  46. */
  47. my.getAvailableVideoWidth = function () {
  48. var chatspaceWidth
  49. = $('#chatspace').is(":visible") ? $('#chatspace').width() : 0;
  50. return window.innerWidth - chatspaceWidth;
  51. };
  52. my.imageToGrayScale = function (canvas) {
  53. var context = canvas.getContext('2d');
  54. var imgData = context.getImageData(0, 0, canvas.width, canvas.height);
  55. var pixels = imgData.data;
  56. for (var i = 0, n = pixels.length; i < n; i += 4) {
  57. var grayscale
  58. = pixels[i] * .3 + pixels[i+1] * .59 + pixels[i+2] * .11;
  59. pixels[i ] = grayscale; // red
  60. pixels[i+1] = grayscale; // green
  61. pixels[i+2] = grayscale; // blue
  62. // pixels[i+3] is alpha
  63. }
  64. // redraw the image in black & white
  65. context.putImageData(imgData, 0, 0);
  66. };
  67. my.setTooltip = function (element, tooltipText, position) {
  68. element.setAttribute("data-content", tooltipText);
  69. element.setAttribute("data-toggle", "popover");
  70. element.setAttribute("data-placement", position);
  71. element.setAttribute("data-html", true);
  72. };
  73. return my;
  74. }(Util || {}));