faviconProgressbar.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. /*global WEB_UI*/
  2. const { platform } = require('../utils');
  3. const assets = require('../../common/assets');
  4. const size = 32;
  5. const loaderWidth = 5;
  6. const loaderColor = WEB_UI.COLORS.PRIMARY;
  7. function drawCircle(canvas, context, color, lineWidth, outerWidth, percent) {
  8. canvas.width = canvas.height = outerWidth;
  9. context.translate(outerWidth * 0.5, outerWidth * 0.5);
  10. context.rotate(-Math.PI * 0.5);
  11. const radius = (outerWidth - lineWidth) * 0.5;
  12. context.beginPath();
  13. context.arc(0, 0, radius, 0, Math.PI * 2 * percent, false);
  14. context.strokeStyle = color;
  15. context.lineCap = 'square';
  16. context.lineWidth = lineWidth;
  17. context.stroke();
  18. }
  19. function drawNewFavicon(progressRatio) {
  20. const canvas = document.createElement('canvas');
  21. const context = canvas.getContext('2d');
  22. drawCircle(canvas, context, '#efefef', loaderWidth, size, 1);
  23. drawCircle(canvas, context, loaderColor, loaderWidth, size, progressRatio);
  24. return canvas.toDataURL();
  25. }
  26. module.exports.updateFavicon = function(progressRatio) {
  27. if (platform() === 'web') {
  28. const link = document.querySelector("link[rel='icon'][sizes='32x32']");
  29. const progress = progressRatio * 100;
  30. if (progress === 0 || progress === 100) {
  31. link.type = 'image/png';
  32. link.href =
  33. WEB_UI.CUSTOM_ASSETS.favicon_32px !== ''
  34. ? WEB_UI.CUSTOM_ASSETS.favicon_32px
  35. : assets.get('favicon-32x32.png');
  36. return;
  37. }
  38. link.href = drawNewFavicon(progressRatio);
  39. }
  40. };