A pixel art painter geared specifically at NES pixel art. Includes export for .chr binary file as well as palette and namespace data.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

109 line
2.7KB

  1. function isElement(el){
  2. // Code based on...
  3. // https://stackoverflow.com/questions/384286/javascript-isdom-how-do-you-check-if-a-javascript-object-is-a-dom-object
  4. try {
  5. // Using W3 DOM2 (works for FF, Opera and Chrome)
  6. return el instanceof HTMLElement;
  7. } catch(e) {
  8. // Browsers not supporting W3 DOM2 don't have HTMLElement and
  9. // an exception is thrown and we end up here. Testing some
  10. // properties that all elements have (works on IE7)
  11. return (typeof(el) === "object") &&
  12. (el.nodeType === 1) &&
  13. (typeof(el.style) === "object") &&
  14. (typeof(el.ownerDocument) === "object");
  15. }
  16. }
  17. const utils = {
  18. isInt:function(v){
  19. if (isNaN(v)){
  20. return false;
  21. }
  22. var x = parseFloat(v);
  23. return (x | 0) === x;
  24. },
  25. isElement:isElement,
  26. addEventListeners(el, listeners){
  27. if (!isElement(el))
  28. throw new TypeError("Can only attach listeners to HTMLElement objects.");
  29. Objects.keys(listeners).forEach((key) => {
  30. el.addEventListener(key, listeners[key]);
  31. });
  32. },
  33. addListenerToEvents(el, enames, listener){
  34. if (!isElement(el))
  35. throw new TypeError("Can only attach listeners to HTMLElement objects.");
  36. if (!(enames instanceof Array))
  37. throw new TypeError("Expected an array of event name strings.");
  38. if (typeof(listener) !== 'function')
  39. throw new TypeError("Listener expected to be a function.");
  40. enames.forEach((name) => {
  41. el.addEventListener(name, listener);
  42. });
  43. },
  44. range:function(a, b, step){
  45. var arr = [];
  46. if (!isNaN(a) && !isNaN(b)){
  47. if (a == b || step < 0){
  48. arr.push(a);
  49. } else {
  50. if (a < b){
  51. for (var i=a; i <= b; i+=step)
  52. arr.push(i);
  53. } else {
  54. for (var i=a; i >= b; i-=step)
  55. arr.push(i);
  56. }
  57. }
  58. }
  59. return arr;
  60. },
  61. debounce:function(func, delay, scope){
  62. var timeout = null;
  63. return function(){
  64. //var context = this;
  65. var context = scope || this;
  66. var args = arguments;
  67. clearTimeout(timeout);
  68. timeout = setTimeout(function(){
  69. func.apply(context, args);
  70. }, delay);
  71. };
  72. },
  73. throttle:function(func, threshold, scope){
  74. threshold || (threshold = 250);
  75. var lst = 0;
  76. var timer;
  77. return function(){
  78. var context = scope || this;
  79. var args = arguments;
  80. var now = Date.now();
  81. if (now < lst + threshold){
  82. clearTimeout(timer);
  83. timer = setTimeout(function(){
  84. lst = now;
  85. func.apply(context, args);
  86. }, threshold);
  87. } else {
  88. lst = now;
  89. func.apply(context, args);
  90. }
  91. };
  92. }
  93. };
  94. Object.freeze(utils);
  95. export default utils;