Debian Codegraph @

closure-library/closure/goog/graphics/graphics.js

raw
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
16 /**
17 * @fileoverview Graphics utility functions and factory methods.
18 * @author [email protected] (Erik Arvidsson)
19 * @see ../demos/graphics/advancedcoordinates.html
20 * @see ../demos/graphics/advancedcoordinates2.html
21 * @see ../demos/graphics/basicelements.html
22 * @see ../demos/graphics/events.html
23 * @see ../demos/graphics/modifyelements.html
24 * @see ../demos/graphics/tiger.html
25 */
26
27
28 goog.provide('goog.graphics');
29
30 goog.require('goog.dom');
31 goog.require('goog.graphics.CanvasGraphics');
32 goog.require('goog.graphics.SvgGraphics');
33 goog.require('goog.graphics.VmlGraphics');
34 goog.require('goog.userAgent');
35
36
37 /**
38 * Returns an instance of goog.graphics.AbstractGraphics that knows how to draw
39 * for the current platform (A factory for the proper Graphics implementation)
40 * @param {string|number} width The width in pixels. Strings
41 * expressing percentages of parent with (e.g. '80%') are also accepted.
42 * @param {string|number} height The height in pixels. Strings
43 * expressing percentages of parent with (e.g. '80%') are also accepted.
44 * @param {?number=} opt_coordWidth The optional coordinate width - if
45 * omitted or null, defaults to same as width.
46 * @param {?number=} opt_coordHeight The optional coordinate height - if
47 * omitted or null, defaults to same as height.
48 * @param {goog.dom.DomHelper=} opt_domHelper The DOM helper object for the
49 * document we want to render in.
50 * @return {!goog.graphics.AbstractGraphics} The created instance.
51 * @deprecated goog.graphics is deprecated. It existed to abstract over browser
52 * differences before the canvas tag was widely supported. See
53 * http://en.wikipedia.org/wiki/Canvas_element for details.
54 */
55 goog.graphics.createGraphics = function(
56 width, height, opt_coordWidth, opt_coordHeight, opt_domHelper) {
57 var graphics;
58 // On IE9 and above, SVG is available, except in compatibility mode.
59 // We check createElementNS on document object that is not exist in
60 // compatibility mode.
61 if (goog.userAgent.IE && (!goog.userAgent.isVersionOrHigher('9') ||
62 !(opt_domHelper || goog.dom.getDomHelper())
63 .getDocument()
64 .createElementNS)) {
65 graphics = new goog.graphics.VmlGraphics(
66 width, height, opt_coordWidth, opt_coordHeight, opt_domHelper);
67 } else if (
68 goog.userAgent.WEBKIT &&
69 (!goog.userAgent.isVersionOrHigher('420') || goog.userAgent.MOBILE)) {
70 graphics = new goog.graphics.CanvasGraphics(
71 width, height, opt_coordWidth, opt_coordHeight, opt_domHelper);
72 } else {
73 graphics = new goog.graphics.SvgGraphics(
74 width, height, opt_coordWidth, opt_coordHeight, opt_domHelper);
75 }
76
77 // Create the dom now, because all drawing methods require that the
78 // main dom element (the canvas) has been already created.
79 graphics.createDom();
80
81 return graphics;
82 };
83
84
85 /**
86 * Returns an instance of goog.graphics.AbstractGraphics that knows how to draw
87 * for the current platform (A factory for the proper Graphics implementation)
88 * @param {string|number} width The width in pixels. Strings
89 * expressing percentages of parent with (e.g. '80%') are also accepted.
90 * @param {string|number} height The height in pixels. Strings
91 * expressing percentages of parent with (e.g. '80%') are also accepted.
92 * @param {?number=} opt_coordWidth The optional coordinate width, defaults to
93 * same as width.
94 * @param {?number=} opt_coordHeight The optional coordinate height, defaults to
95 * same as height.
96 * @param {goog.dom.DomHelper=} opt_domHelper The DOM helper object for the
97 * document we want to render in.
98 * @return {!goog.graphics.AbstractGraphics} The created instance.
99 * @deprecated goog.graphics is deprecated. It existed to abstract over browser
100 * differences before the canvas tag was widely supported. See
101 * http://en.wikipedia.org/wiki/Canvas_element for details.
102 */
103 goog.graphics.createSimpleGraphics = function(
104 width, height, opt_coordWidth, opt_coordHeight, opt_domHelper) {
105 if (goog.userAgent.MAC && goog.userAgent.GECKO &&
106 !goog.userAgent.isVersionOrHigher('1.9a')) {
107 // Canvas is 6x faster than SVG on Mac FF 2.0
108 var graphics = new goog.graphics.CanvasGraphics(
109 width, height, opt_coordWidth, opt_coordHeight, opt_domHelper);
110 graphics.createDom();
111 return graphics;
112 }
113
114 // Otherwise, defer to normal graphics object creation.
115 return goog.graphics.createGraphics(
116 width, height, opt_coordWidth, opt_coordHeight, opt_domHelper);
117 };
118
119
120 /**
121 * Static function to check if the current browser has Graphics support.
122 * @return {boolean} True if the current browser has Graphics support.
123 * @deprecated goog.graphics is deprecated. It existed to abstract over browser
124 * differences before the canvas tag was widely supported. See
125 * http://en.wikipedia.org/wiki/Canvas_element for details.
126 */
127 goog.graphics.isBrowserSupported = function() {
128 if (goog.userAgent.IE) {
129 return goog.userAgent.isVersionOrHigher('5.5');
130 }
131 if (goog.userAgent.GECKO) {
132 return goog.userAgent.isVersionOrHigher('1.8');
133 }
134 if (goog.userAgent.OPERA) {
135 return goog.userAgent.isVersionOrHigher('9.0');
136 }
137 if (goog.userAgent.WEBKIT) {
138 return goog.userAgent.isVersionOrHigher('412');
139 }
140 return false;
141 };