ImageBitmapLoader.js 1.2 KB

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