assetManager.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import createTag from './html_elements';
  2. import createNS from './svg_elements';
  3. import featureSupport from '../featureSupport';
  4. var lumaLoader = (function () {
  5. var id = '__lottie_element_luma_buffer';
  6. var lumaBuffer = null;
  7. var lumaBufferCtx = null;
  8. var svg = null;
  9. // This alternate solution has a slight delay before the filter is applied, resulting in a flicker on the first frame.
  10. // Keeping this here for reference, and in the future, if offscreen canvas supports url filters, this can be used.
  11. // For now, neither of them work for offscreen canvas, so canvas workers can't support the luma track matte mask.
  12. // Naming it solution 2 to mark the extra comment lines.
  13. /*
  14. var svgString = [
  15. '<svg xmlns="http://www.w3.org/2000/svg">',
  16. '<filter id="' + id + '">',
  17. '<feColorMatrix type="matrix" color-interpolation-filters="sRGB" values="',
  18. '0.3, 0.3, 0.3, 0, 0, ',
  19. '0.3, 0.3, 0.3, 0, 0, ',
  20. '0.3, 0.3, 0.3, 0, 0, ',
  21. '0.3, 0.3, 0.3, 0, 0',
  22. '"/>',
  23. '</filter>',
  24. '</svg>',
  25. ].join('');
  26. var blob = new Blob([svgString], { type: 'image/svg+xml' });
  27. var url = URL.createObjectURL(blob);
  28. */
  29. function createLumaSvgFilter() {
  30. var _svg = createNS('svg');
  31. var fil = createNS('filter');
  32. var matrix = createNS('feColorMatrix');
  33. fil.setAttribute('id', id);
  34. matrix.setAttribute('type', 'matrix');
  35. matrix.setAttribute('color-interpolation-filters', 'sRGB');
  36. matrix.setAttribute('values', '0.3, 0.3, 0.3, 0, 0, 0.3, 0.3, 0.3, 0, 0, 0.3, 0.3, 0.3, 0, 0, 0.3, 0.3, 0.3, 0, 0');
  37. fil.appendChild(matrix);
  38. _svg.appendChild(fil);
  39. _svg.setAttribute('id', id + '_svg');
  40. if (featureSupport.svgLumaHidden) {
  41. _svg.style.display = 'none';
  42. }
  43. return _svg;
  44. }
  45. function loadLuma() {
  46. if (!lumaBuffer) {
  47. svg = createLumaSvgFilter();
  48. document.body.appendChild(svg);
  49. lumaBuffer = createTag('canvas');
  50. lumaBufferCtx = lumaBuffer.getContext('2d');
  51. // lumaBufferCtx.filter = `url('${url}#__lottie_element_luma_buffer')`; // part of solution 2
  52. lumaBufferCtx.filter = 'url(#' + id + ')';
  53. lumaBufferCtx.fillStyle = 'rgba(0,0,0,0)';
  54. lumaBufferCtx.fillRect(0, 0, 1, 1);
  55. }
  56. }
  57. function getLuma(canvas) {
  58. if (!lumaBuffer) {
  59. loadLuma();
  60. }
  61. lumaBuffer.width = canvas.width;
  62. lumaBuffer.height = canvas.height;
  63. // lumaBufferCtx.filter = `url('${url}#__lottie_element_luma_buffer')`; // part of solution 2
  64. lumaBufferCtx.filter = 'url(#' + id + ')';
  65. return lumaBuffer;
  66. }
  67. return {
  68. load: loadLuma,
  69. get: getLuma,
  70. };
  71. });
  72. function createCanvas(width, height) {
  73. if (featureSupport.offscreenCanvas) {
  74. return new OffscreenCanvas(width, height);
  75. }
  76. var canvas = createTag('canvas');
  77. canvas.width = width;
  78. canvas.height = height;
  79. return canvas;
  80. }
  81. const assetLoader = (function () {
  82. return {
  83. loadLumaCanvas: lumaLoader.load,
  84. getLumaCanvas: lumaLoader.get,
  85. createCanvas: createCanvas,
  86. };
  87. }());
  88. export default assetLoader;