123456789101112131415161718192021222324252627282930313233343536373839
  1. import { resources } from "./resources";
  2. export function preload(onProgress) {
  3. // 拿出所有图片
  4. const images = resources.reduce((list, item) => {
  5. if (item.image) {
  6. list.push(item.image)
  7. }
  8. if (item.thumb1) {
  9. list.push(item.thumb1)
  10. }
  11. if (item.thumb2) {
  12. list.push(item.thumb2)
  13. }
  14. return list;
  15. }, [])
  16. return new Promise((resolve, reject) => {
  17. const total = images.length;
  18. let cursor = 0;
  19. const callback = (e, isError) => {
  20. cursor += 1;
  21. onProgress(cursor / total);
  22. if (cursor >= total) {
  23. resolve(resources)
  24. }
  25. }
  26. for (let src of images) {
  27. const img = document.createElement('img');
  28. img.onload = e => callback(e, false);
  29. img.onerror = e => callback(e, true);
  30. img.src = src;
  31. }
  32. })
  33. }