graphics.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // Copyright 2007 The Closure Library Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /**
  15. * @fileoverview Graphics utility functions and factory methods.
  16. * @author arv@google.com (Erik Arvidsson)
  17. * @see ../demos/graphics/advancedcoordinates.html
  18. * @see ../demos/graphics/advancedcoordinates2.html
  19. * @see ../demos/graphics/basicelements.html
  20. * @see ../demos/graphics/events.html
  21. * @see ../demos/graphics/modifyelements.html
  22. * @see ../demos/graphics/tiger.html
  23. */
  24. goog.provide('goog.graphics');
  25. goog.require('goog.dom');
  26. goog.require('goog.graphics.CanvasGraphics');
  27. goog.require('goog.graphics.SvgGraphics');
  28. goog.require('goog.graphics.VmlGraphics');
  29. goog.require('goog.userAgent');
  30. /**
  31. * Returns an instance of goog.graphics.AbstractGraphics that knows how to draw
  32. * for the current platform (A factory for the proper Graphics implementation)
  33. * @param {string|number} width The width in pixels. Strings
  34. * expressing percentages of parent with (e.g. '80%') are also accepted.
  35. * @param {string|number} height The height in pixels. Strings
  36. * expressing percentages of parent with (e.g. '80%') are also accepted.
  37. * @param {?number=} opt_coordWidth The optional coordinate width - if
  38. * omitted or null, defaults to same as width.
  39. * @param {?number=} opt_coordHeight The optional coordinate height - if
  40. * omitted or null, defaults to same as height.
  41. * @param {goog.dom.DomHelper=} opt_domHelper The DOM helper object for the
  42. * document we want to render in.
  43. * @return {!goog.graphics.AbstractGraphics} The created instance.
  44. * @deprecated goog.graphics is deprecated. It existed to abstract over browser
  45. * differences before the canvas tag was widely supported. See
  46. * http://en.wikipedia.org/wiki/Canvas_element for details.
  47. */
  48. goog.graphics.createGraphics = function(
  49. width, height, opt_coordWidth, opt_coordHeight, opt_domHelper) {
  50. var graphics;
  51. // On IE9 and above, SVG is available, except in compatibility mode.
  52. // We check createElementNS on document object that is not exist in
  53. // compatibility mode.
  54. if (goog.userAgent.IE && (!goog.userAgent.isVersionOrHigher('9') ||
  55. !(opt_domHelper || goog.dom.getDomHelper())
  56. .getDocument()
  57. .createElementNS)) {
  58. graphics = new goog.graphics.VmlGraphics(
  59. width, height, opt_coordWidth, opt_coordHeight, opt_domHelper);
  60. } else if (
  61. goog.userAgent.WEBKIT &&
  62. (!goog.userAgent.isVersionOrHigher('420') || goog.userAgent.MOBILE)) {
  63. graphics = new goog.graphics.CanvasGraphics(
  64. width, height, opt_coordWidth, opt_coordHeight, opt_domHelper);
  65. } else {
  66. graphics = new goog.graphics.SvgGraphics(
  67. width, height, opt_coordWidth, opt_coordHeight, opt_domHelper);
  68. }
  69. // Create the dom now, because all drawing methods require that the
  70. // main dom element (the canvas) has been already created.
  71. graphics.createDom();
  72. return graphics;
  73. };
  74. /**
  75. * Returns an instance of goog.graphics.AbstractGraphics that knows how to draw
  76. * for the current platform (A factory for the proper Graphics implementation)
  77. * @param {string|number} width The width in pixels. Strings
  78. * expressing percentages of parent with (e.g. '80%') are also accepted.
  79. * @param {string|number} height The height in pixels. Strings
  80. * expressing percentages of parent with (e.g. '80%') are also accepted.
  81. * @param {?number=} opt_coordWidth The optional coordinate width, defaults to
  82. * same as width.
  83. * @param {?number=} opt_coordHeight The optional coordinate height, defaults to
  84. * same as height.
  85. * @param {goog.dom.DomHelper=} opt_domHelper The DOM helper object for the
  86. * document we want to render in.
  87. * @return {!goog.graphics.AbstractGraphics} The created instance.
  88. * @deprecated goog.graphics is deprecated. It existed to abstract over browser
  89. * differences before the canvas tag was widely supported. See
  90. * http://en.wikipedia.org/wiki/Canvas_element for details.
  91. */
  92. goog.graphics.createSimpleGraphics = function(
  93. width, height, opt_coordWidth, opt_coordHeight, opt_domHelper) {
  94. if (goog.userAgent.MAC && goog.userAgent.GECKO &&
  95. !goog.userAgent.isVersionOrHigher('1.9a')) {
  96. // Canvas is 6x faster than SVG on Mac FF 2.0
  97. var graphics = new goog.graphics.CanvasGraphics(
  98. width, height, opt_coordWidth, opt_coordHeight, opt_domHelper);
  99. graphics.createDom();
  100. return graphics;
  101. }
  102. // Otherwise, defer to normal graphics object creation.
  103. return goog.graphics.createGraphics(
  104. width, height, opt_coordWidth, opt_coordHeight, opt_domHelper);
  105. };
  106. /**
  107. * Static function to check if the current browser has Graphics support.
  108. * @return {boolean} True if the current browser has Graphics support.
  109. * @deprecated goog.graphics is deprecated. It existed to abstract over browser
  110. * differences before the canvas tag was widely supported. See
  111. * http://en.wikipedia.org/wiki/Canvas_element for details.
  112. */
  113. goog.graphics.isBrowserSupported = function() {
  114. if (goog.userAgent.IE) {
  115. return goog.userAgent.isVersionOrHigher('5.5');
  116. }
  117. if (goog.userAgent.GECKO) {
  118. return goog.userAgent.isVersionOrHigher('1.8');
  119. }
  120. if (goog.userAgent.OPERA) {
  121. return goog.userAgent.isVersionOrHigher('9.0');
  122. }
  123. if (goog.userAgent.WEBKIT) {
  124. return goog.userAgent.isVersionOrHigher('412');
  125. }
  126. return false;
  127. };