ImageBitmapLoader.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /**
  2. * @author thespite / http://clicktorelease.com/
  3. */
  4. THREE.ImageBitmapLoader = function (manager) {
  5. this.manager = manager !== undefined ? manager : THREE.DefaultLoadingManager;
  6. this.options = {};
  7. };
  8. THREE.ImageBitmapLoader.prototype = {
  9. constructor: THREE.ImageBitmapLoader,
  10. setOptions: function setOptions(options) {
  11. this.options = options;
  12. return this;
  13. },
  14. load: function load(url, onLoad, onProgress, onError) {
  15. if (url === undefined) url = '';
  16. if (this.path !== undefined) url = this.path + url;
  17. var scope = this;
  18. var cached = THREE.Cache.get(url);
  19. if (cached !== undefined) {
  20. scope.manager.itemStart(url);
  21. setTimeout(function () {
  22. if (onLoad) onLoad(cached);
  23. scope.manager.itemEnd(url);
  24. }, 0);
  25. return cached;
  26. }
  27. fetch(url).then(function (res) {
  28. return res.blob();
  29. }).then(function (res) {
  30. return createImageBitmap(res, scope.options);
  31. }).then(function (res) {
  32. THREE.Cache.add(url, res);
  33. if (onLoad) onLoad(res);
  34. scope.manager.itemEnd(url);
  35. }).catch(function (e) {
  36. if (onError) onError(e);
  37. scope.manager.itemEnd(url);
  38. scope.manager.itemError(url);
  39. });
  40. }
  41. };