Home Reference Source Repository

js/CoreGraphics/CGSize.js

  1. 'use strict'
  2.  
  3. /**
  4. * A structure that contains width and height values.
  5. * @access public
  6. * @see https://developer.apple.com/documentation/coregraphics/cgsize
  7. */
  8. export default class CGSize {
  9. /**
  10. * @access private
  11. * @param {Buffer} data -
  12. * @param {number} [offset = 0] -
  13. * @param {boolean} [bigEndian = false] -
  14. * @returns {CGSize} -
  15. */
  16. static _initWithData(data, offset = 0, bigEndian = false) {
  17. throw new Error('not implemented')
  18. }
  19.  
  20. // Initializers
  21.  
  22. /**
  23. * Creates a size with dimensions specified as floating-point values.
  24. * @access public
  25. * @constructor
  26. * @param {number} width -
  27. * @param {number} height -
  28. * @see https://developer.apple.com/documentation/coregraphics/cgsize/1454915-init
  29. */
  30. constructor(width, height) {
  31. // Geometric Properties
  32. this.width = width
  33. this.height = height
  34. }
  35.  
  36. // Geometric Properties
  37.  
  38. // Special Values
  39. /**
  40. * The size whose width and height are both zero.
  41. * @type {CGSize}
  42. * @desc
  43. * @see https://developer.apple.com/documentation/coregraphics/cgsize/1455512-zero
  44. */
  45. static get zero() {
  46. return new CGSize(0, 0)
  47. }
  48.  
  49. // Transforming Sizes
  50.  
  51. /**
  52. * Returns the height and width resulting from a transformation of an existing height and width.
  53. * @access public
  54. * @param {CGAffineTransform} t - The affine transform to apply.
  55. * @returns {CGSize} -
  56. * @see https://developer.apple.com/documentation/coregraphics/cgsize/1454806-applying
  57. */
  58. applying(t) {
  59. return null
  60. }
  61.  
  62. // Alternate Representations
  63.  
  64. /**
  65. * Creates a size from a canonical dictionary representation.
  66. * @access public
  67. * @param {Map} dict - A dictionary containing width and height values for the size to create, in the format used by the dictionaryRepresentation property.
  68. * @returns {void}
  69. * @see https://developer.apple.com/documentation/coregraphics/cgsize/2427155-init
  70. */
  71. static sizeWithDictionaryRepresentation(dict) {
  72. return new CGSize(dict.get('width'), dict.get('height'))
  73. }
  74.  
  75. /**
  76. * Returns a dictionary representation of the specified size.
  77. * @type {Map}
  78. * @desc
  79. * @see https://developer.apple.com/documentation/coregraphics/cgsize/1455274-dictionaryrepresentation
  80. */
  81. get dictionaryRepresentation() {
  82. const map = new Map()
  83. map.set('width', this.width)
  84. map.set('height', this.height)
  85. return map
  86. }
  87.  
  88. /**
  89. * A textual representation of the size's dimensions.
  90. * @type {string}
  91. * @desc
  92. * @see https://developer.apple.com/documentation/coregraphics/cgsize/1645822-debugdescription
  93. */
  94. get debugDescription() {
  95. return `{width: ${this.width}, height: ${this.height}}`
  96. }
  97.  
  98. /**
  99. * A representation of the size's structure and display style for use in debugging.
  100. * @type {Mirror}
  101. * @desc
  102. * @see https://developer.apple.com/documentation/coregraphics/cgsize/1645828-custommirror
  103. */
  104. get customMirror() {
  105. return null
  106. }
  107. /**
  108. * A representation of the size for use in Playgrounds.
  109. * @type {PlaygroundQuickLook}
  110. * @desc
  111. * @see https://developer.apple.com/documentation/coregraphics/cgsize/1645830-customplaygroundquicklook
  112. */
  113. get customPlaygroundQuickLook() {
  114. return null
  115. }
  116.  
  117. // Comparing Sizes
  118.  
  119. /**
  120. * Returns whether two sizes are equal.
  121. * @access public
  122. * @param {CGSize} size2 -
  123. * @returns {boolean} -
  124. * @see https://developer.apple.com/documentation/coregraphics/cgsize/1455176-equalto
  125. */
  126. equalTo(size2) {
  127. const epsilon = 0.00001
  128. return Math.abs(this.width - size2.width) < epsilon
  129. && Math.abs(this.height - size2.height) < epsilon
  130. }
  131.  
  132. zero() {
  133. return new CGSize(0, 0)
  134. }
  135.  
  136. add(size2) {
  137. return new CGSize(this.width + size2.width, this.height + size2.height)
  138. }
  139.  
  140. sub(size2) {
  141. return new CGSize(this.width - size2.width, this.height - size2.height)
  142. }
  143.  
  144. /**
  145. * @access public
  146. * @param {CGSize} s -
  147. * @param {number} rate -
  148. * @returns {CGSize} -
  149. */
  150. lerp(s, rate) {
  151. const w = this.width + rate * (s.width - this.width)
  152. const h = this.height + rate * (s.height - this.height)
  153. return new CGSize(w, h)
  154. }
  155.  
  156. copy() {
  157. return new CGSize(this.width, this.height)
  158. }
  159. }