coordinates.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. "use strict";
  2. // Copyright 2012 United States Government, as represented by the Secretary of Defense, Under
  3. // Secretary of Defense (Personnel & Readiness).
  4. //
  5. // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
  6. // in compliance with the License. You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software distributed under the License
  11. // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
  12. // or implied. See the License for the specific language governing permissions and limitations under
  13. // the License.
  14. /// DOM element coordinate conversion functions.
  15. ///
  16. /// @module vwf/utility/coordinates
  17. define( function() {
  18. var exports = {
  19. /// Convert a coordinate from an element's content area to its internal coordinate system.
  20. /// The content box is the area within the element's padding.
  21. ///
  22. /// @param {HTMLElement} element
  23. /// The reference element.
  24. /// @param {Object} xyContent
  25. /// An x, y coordinate relative to the element's content box, expressed as:
  26. /// { x: value, y: value }.
  27. ///
  28. /// @returns {Object}
  29. /// An x, y coordinate relative to the element's internal coordinate system.
  30. canvasFromContent: function( element, xyContent ) {
  31. var computedStyle = getComputedStyle( element );
  32. var contentWidth = element.clientWidth -
  33. ( parseFloat( computedStyle.getPropertyValue( "padding-left" ) ) || 0 ) -
  34. ( parseFloat( computedStyle.getPropertyValue( "padding-right" ) ) || 0 );
  35. var contentHeight = element.clientHeight -
  36. ( parseFloat( computedStyle.getPropertyValue( "padding-top" ) ) || 0 ) -
  37. ( parseFloat( computedStyle.getPropertyValue( "padding-bottom" ) ) || 0 );
  38. return {
  39. x: ( xyContent && xyContent.x || 0 ) / contentWidth * ( element.width || contentWidth ),
  40. y: ( xyContent && xyContent.y || 0 ) / contentHeight * ( element.height || contentHeight ),
  41. };
  42. },
  43. /// Convert a coordinate from an element's padding area to its content area. The content box
  44. /// is the area within the element's padding. The padding box is the area within the
  45. /// element's border.
  46. ///
  47. /// @param {HTMLElement} element
  48. /// The reference element.
  49. /// @param {Object} xyPadding
  50. /// An x, y coordinate relative to the element's padding box, expressed as:
  51. /// { x: value, y: value }.
  52. ///
  53. /// @returns {Object}
  54. /// An x, y coordinate relative to the element's content box.
  55. contentFromPadding: function( element, xyPadding ) {
  56. var computedStyle = getComputedStyle( element );
  57. return {
  58. x: ( xyPadding && xyPadding.x || 0 ) - ( parseFloat( computedStyle.getPropertyValue( "padding-left" ) ) || 0 ) + element.scrollLeft,
  59. y: ( xyPadding && xyPadding.y || 0 ) - ( parseFloat( computedStyle.getPropertyValue( "padding-top" ) ) || 0 ) + element.scrollTop,
  60. };
  61. },
  62. /// Convert a coordinate from an element's border area to its padding area. The padding box
  63. /// is the area within the element's border. The border box encloses the overall element,
  64. /// including its content, padding and border.
  65. ///
  66. /// @param {HTMLElement} element
  67. /// The reference element.
  68. /// @param {Object} xyBorder
  69. /// An x, y coordinate relative to the element's border box, expressed as:
  70. /// { x: value, y: value }.
  71. ///
  72. /// @returns {Object}
  73. /// An x, y coordinate relative to the element's padding box.
  74. paddingFromBorder: function( element, xyBorder ) {
  75. var computedStyle = getComputedStyle( element );
  76. return {
  77. x: ( xyBorder && xyBorder.x || 0 ) - ( parseFloat( computedStyle.getPropertyValue( "border-left-width" ) ) || 0 ),
  78. y: ( xyBorder && xyBorder.y || 0 ) - ( parseFloat( computedStyle.getPropertyValue( "border-top-width" ) ) || 0 ),
  79. };
  80. },
  81. /// Convert a coordinate from an element's window coordinates to its border area. The border
  82. /// box encloses the overall element, including its content, padding and border. The window
  83. /// coordinates are the location of the border box in the browser window.
  84. ///
  85. /// @param {HTMLElement} element
  86. /// The reference element.
  87. /// @param {Object} xyWindow
  88. /// An x, y coordinate relative to the element's border box in the browser window,
  89. /// expressed as: { x: value, y: value }.
  90. ///
  91. /// @returns {Object}
  92. /// An x, y coordinate relative to the element's border box.
  93. borderFromWindow: function( element, xyWindow ) {
  94. var bounds = element.getBoundingClientRect(); // border box in window coordinates
  95. return {
  96. x: ( xyWindow && xyWindow.x || 0 ) - bounds.left,
  97. y: ( xyWindow && xyWindow.y || 0 ) - bounds.top,
  98. };
  99. },
  100. /// Convert a coordinate from an element's window coordinates to its content area.
  101. ///
  102. /// @param {HTMLElement} element
  103. /// The reference element.
  104. /// @param {Object} xyWindow
  105. /// An x, y coordinate relative to the element's border box in the browser window,
  106. /// expressed as: { x: value, y: value }.
  107. ///
  108. /// @returns {Object}
  109. /// An x, y coordinate relative to the element's content box.
  110. contentFromWindow: function( element, xyWindow ) {
  111. return this.contentFromPadding( element, this.paddingFromBorder( element,
  112. this.borderFromWindow( element, xyWindow ) ) );
  113. },
  114. };
  115. return exports;
  116. } );