Advertisement
Guest User

Untitled

a guest
Nov 19th, 2019
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.60 KB | None | 0 0
  1. import { environment } from '../../environments/environment';
  2. import { UrlTree } from '@angular/router';
  3.  
  4. export function getMultiDpiImageUrl(familyCode: string, index: string, height: number, width: number, webpSupport: boolean, dpi: string, type: string): string {
  5. let result: string = '';
  6.  
  7. result += getImageUrl(familyCode, index, height, width, webpSupport, dpi, type) + ' 1x, ';
  8. result += getImageUrl(familyCode, index, height, width, webpSupport, dpi, type) + ' 1.1x, ';
  9. result += getImageUrl(familyCode, index, height, width, webpSupport, dpi, type) + ' 1.5x, ';
  10. result += getImageUrl(familyCode, index, height, width, webpSupport, dpi, type) + ' 2x ';
  11.  
  12. return result;
  13. }
  14.  
  15. export function getImageUrl(familyCode: string, index: string, height: number, width: number, dpi: string, type: string): string {
  16.  
  17. let webpSupport = this.browserSupport.getWebpSupport();
  18.  
  19. let url = '?index=' + index;
  20.  
  21. if (height) {
  22. url += '&height=' + height;
  23. }
  24.  
  25. if (width) {
  26. url += '&width=' + width;
  27. }
  28.  
  29. if (!dpi) {
  30. dpi = '100';
  31. if (window.devicePixelRatio > 1.50) {
  32. dpi = '200';
  33. } else if (window.devicePixelRatio > 1.25) {
  34. dpi = '150';
  35. } else if (window.devicePixelRatio > 1) {
  36. dpi = '125';
  37. }
  38. }
  39.  
  40. url += '&dpi=' + dpi;
  41.  
  42. if (type) {
  43. url += '&type=' + type;
  44. }
  45.  
  46. url += '&format='.concat(webpSupport ? 'webp' : 'jpg');
  47.  
  48. return environment.mediaImageUrl + familyCode + url;
  49. }
  50.  
  51. export function getImageUrlStatic(imagePath:string, width: number, height: number, dpi: string): string {
  52.  
  53. let webpSupport = this.browserSupport.getWebpSupport();
  54.  
  55. let url = '';
  56.  
  57. if(imagePath.indexOf('?') > 0)
  58. {
  59. url = imagePath;
  60. }
  61. else
  62. {
  63. url = imagePath + '?';
  64. }
  65.  
  66. if (height) {
  67. url += '&height=' + height;
  68. }
  69.  
  70. if (width) {
  71. url += '&width=' + width;
  72. }
  73.  
  74. if (!dpi) {
  75. dpi = '100';
  76. if (window.devicePixelRatio > 1.50) {
  77. dpi = '200';
  78. } else if (window.devicePixelRatio > 1.25) {
  79. dpi = '150';
  80. } else if (window.devicePixelRatio > 1) {
  81. dpi = '125';
  82. }
  83. }
  84.  
  85. url += '&dpi=' + dpi;
  86. url += '&format='.concat(webpSupport ? 'webp' : 'jpg');
  87.  
  88. return url;
  89. }
  90.  
  91. export function getTextWidth(text, font): number {
  92. // if given, use cached canvas for better performance
  93. // else, create new canvas
  94. // @ts-ignore
  95. const canvas = getTextWidth.canvas || (getTextWidth.canvas = document.createElement('canvas'));
  96. const context = canvas.getContext('2d');
  97. context.font = font;
  98. const metrics = context.measureText(text);
  99. return metrics.width;
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement