ScalingUtil.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. 'use strict';
  2. /**
  3. * Licensed Materials - Property of IBM
  4. * IBM Cognos Products: BI Cloud (C) Copyright IBM Corp. 2014
  5. * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  6. */
  7. define(['jquery'], function ($) {
  8. var ScalingUtil = {
  9. /**
  10. * @param params An object with the following members:
  11. * * width and height - the user inputed width and height to scale/
  12. * verify
  13. * * image - a Javascript Image object whose width and height are to
  14. * be scaled - either width and height, or image must be provided
  15. * * defaultDim - (optional) default dimensions to replace missing
  16. * or invalid width/height dimensions
  17. * * scaleWidth - (optional) indicates the width should be replaced
  18. * with a version scaled to match the height. This member should
  19. * be an object with width and height members indicating the
  20. * correct aspect ratio.
  21. * * scaleHeight - (optional) the equivalent of scaleWidth to set
  22. * the height. Either scaleWidth or scaleHeight should be
  23. * specified, not both.
  24. * * min - (optional) indicates minimum output dimensions. This
  25. * member should be an object with width and height members
  26. * indicating their respective minimum dimensions.
  27. * * max - (optional) the equivalent of min with the maximum
  28. * dimensions. If both min and max are specified, and it is not
  29. * possible to satisfy both, max has priority.
  30. * * format - (optional) set to "css" for the outputs to be strings
  31. * with "px"; otherwise they will be integers.
  32. * @return an object with width and height members
  33. */
  34. scale: function scale(params) {
  35. var dim = {
  36. width: params.width,
  37. height: params.height
  38. };
  39. var aspectRatio;
  40. if (!dim.width && !dim.height && params.image) {
  41. dim.width = params.image.naturalWidth;
  42. dim.height = params.image.naturalHeight;
  43. }
  44. dim.width = this._sanitizeNumberInput(dim.width);
  45. dim.height = this._sanitizeNumberInput(dim.height);
  46. if (params.defaultDim) {
  47. if (dim.width < 1 && !params.scaleWidth) {
  48. dim.width = params.defaultDim.width;
  49. }
  50. if (dim.height < 1 && !params.scaleHeight) {
  51. dim.height = params.defaultDim.height;
  52. }
  53. }
  54. this._applyScale(params.scaleWidth || params.scaleHeight, !!params.scaleWidth, dim);
  55. aspectRatio = dim.width / dim.height;
  56. this._enforceMin(params.min, aspectRatio, dim);
  57. this._enforceMax(params.max, aspectRatio, dim);
  58. this._formatDimens(params.format, dim);
  59. return dim;
  60. },
  61. // returns input as an integer if it is numeric, otherwise return input
  62. _sanitizeNumberInput: function _sanitizeNumberInput(input) {
  63. var number = 0;
  64. if ($.type(input) === 'number') {
  65. number = input;
  66. } else if ($.isNumeric(input)) {
  67. number = parseInt(input, 10);
  68. }
  69. return number;
  70. },
  71. // applies scale to dim
  72. // if dim doesn't have a value, overwrites it with scale
  73. _applyScale: function _applyScale(scale, scaleWidth, dim) {
  74. if (scale) {
  75. var wScale = scale.width;
  76. var hScale = scale.height;
  77. var bothSet = dim.width > 0 && dim.height > 0;
  78. if (dim.width < 1 && dim.height < 1) {
  79. dim.width = wScale;
  80. dim.height = hScale;
  81. } else {
  82. this._scaleMaintainRatio(scale, scaleWidth, bothSet, dim);
  83. }
  84. }
  85. },
  86. // scales dim to have the same aspect ratio as scale
  87. // if force=true, will overwrite previous value, otherwise it will keep previous value
  88. // if scaleWidth=true, modifies width, otherwise modifies height
  89. // precondition: dim has at least one of width/height defined
  90. _scaleMaintainRatio: function _scaleMaintainRatio(scale, scaleWidth, force, dim) {
  91. if (scaleWidth && (dim.width < 1 || force)) {
  92. dim.width = scale.width * dim.height / scale.height;
  93. } else if (!scaleWidth && (dim.height < 1 || force)) {
  94. dim.height = scale.height * dim.width / scale.width;
  95. }
  96. },
  97. // enforces min dimensions while preserving aspect ratio
  98. _enforceMin: function _enforceMin(min, aspectRatio, dim) {
  99. if (min) {
  100. var wMin = min.width;
  101. var hMin = min.height;
  102. var wRatio = wMin ? dim.width / wMin : 1;
  103. var hRatio = hMin ? dim.height / hMin : 1;
  104. if (wRatio < 1 && wRatio < hRatio) {
  105. dim.width = wMin;
  106. dim.height = wMin / aspectRatio;
  107. } else if (hRatio < 1) {
  108. dim.height = hMin;
  109. dim.width = hMin * aspectRatio;
  110. }
  111. }
  112. },
  113. // enforces max dimensions while preserving aspect ratio
  114. _enforceMax: function _enforceMax(max, aspectRatio, dim) {
  115. if (max) {
  116. var wMax = max.width;
  117. var hMax = max.height;
  118. var wRatio = wMax ? dim.width / wMax : 0;
  119. var hRatio = hMax ? dim.height / hMax : 0;
  120. if (wRatio > 1 && wRatio > hRatio) {
  121. dim.width = wMax;
  122. dim.height = wMax / aspectRatio;
  123. } else if (hRatio > 1) {
  124. dim.height = hMax;
  125. dim.width = hMax * aspectRatio;
  126. }
  127. }
  128. },
  129. // formats dimensions by rounding them and changing to css styles if format=css
  130. _formatDimens: function _formatDimens(format, dim) {
  131. dim.width = Math.round(dim.width);
  132. dim.height = Math.round(dim.height);
  133. if (format === 'css') {
  134. dim.width = dim.width + 'px';
  135. dim.height = dim.height + 'px';
  136. }
  137. }
  138. };
  139. return ScalingUtil;
  140. });
  141. //# sourceMappingURL=ScalingUtil.js.map