Source: animate/ShapesCache.js

animate/ShapesCache.js

  1. import utils from './utils';
  2. /**
  3. * Contains the collection of graphics data
  4. * @memberof PIXI.animate
  5. * @class ShapesCache
  6. */
  7. const ShapesCache = {};
  8. /**
  9. * Add an item or itesm to the cache
  10. * @method PIXI.animate.ShapesCache.add
  11. * @static
  12. * @param {String} prop The id of graphic or the map of graphics to add
  13. * @param {String|Array<Array>} items Collection of draw commands
  14. */
  15. Object.defineProperty(ShapesCache, 'add', {
  16. enumerable: false,
  17. value: function(prop, items) {
  18. // Decode string to map of files
  19. if (typeof items === "string") {
  20. items = utils.deserializeShapes(items);
  21. }
  22. // Convert all hex string colors (animate) to int (pixi.js)
  23. for (let i = 0; i < items.length; i++) {
  24. let item = items[i];
  25. for (let j = 0; j < item.length; j++) {
  26. let arg = item[j];
  27. if (typeof arg === 'string' && arg[0] === '#') {
  28. item[j] = utils.hexToUint(arg);
  29. }
  30. }
  31. }
  32. ShapesCache[prop] = items;
  33. }
  34. });
  35. /**
  36. * Get the graphic from cache
  37. * @method PIXI.animate.ShapesCache.fromCache
  38. * @static
  39. * @param {String} id The cache id
  40. * @return {Array} Series of graphic draw commands
  41. */
  42. Object.defineProperty(ShapesCache, 'fromCache', {
  43. enumerable: false,
  44. value: function(id) {
  45. return ShapesCache[id] || null;
  46. }
  47. });
  48. /**
  49. * Remove the graphic from cache
  50. * @method PIXI.animate.ShapesCache.remove
  51. * @static
  52. * @param {String|Object} id The cache id or map
  53. */
  54. Object.defineProperty(ShapesCache, 'remove', {
  55. enumerable: false,
  56. value: function(id) {
  57. if (typeof id === "object") {
  58. for (let name in id) {
  59. ShapesCache.remove(name);
  60. }
  61. return;
  62. }
  63. if (ShapesCache[id]) {
  64. ShapesCache[id].length = 0;
  65. delete ShapesCache[id];
  66. }
  67. }
  68. });
  69. /**
  70. * Remove all graphics from cache
  71. * @method PIXI.animate.ShapesCache.removeAll
  72. * @static
  73. */
  74. Object.defineProperty(ShapesCache, 'removeAll', {
  75. enumerable: false,
  76. value: function() {
  77. for (let id in ShapesCache) {
  78. ShapesCache.remove(id);
  79. }
  80. }
  81. });
  82. // Assign to namespace
  83. export default ShapesCache;