Path: blob/trunk/third_party/closure/goog/graphics/graphics.js
2868 views
// Copyright 2007 The Closure Library Authors. All Rights Reserved.1//2// Licensed under the Apache License, Version 2.0 (the "License");3// you may not use this file except in compliance with the License.4// You may obtain a copy of the License at5//6// http://www.apache.org/licenses/LICENSE-2.07//8// Unless required by applicable law or agreed to in writing, software9// distributed under the License is distributed on an "AS-IS" BASIS,10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.11// See the License for the specific language governing permissions and12// limitations under the License.131415/**16* @fileoverview Graphics utility functions and factory methods.17* @author [email protected] (Erik Arvidsson)18* @see ../demos/graphics/advancedcoordinates.html19* @see ../demos/graphics/advancedcoordinates2.html20* @see ../demos/graphics/basicelements.html21* @see ../demos/graphics/events.html22* @see ../demos/graphics/modifyelements.html23* @see ../demos/graphics/tiger.html24*/252627goog.provide('goog.graphics');2829goog.require('goog.dom');30goog.require('goog.graphics.CanvasGraphics');31goog.require('goog.graphics.SvgGraphics');32goog.require('goog.graphics.VmlGraphics');33goog.require('goog.userAgent');343536/**37* Returns an instance of goog.graphics.AbstractGraphics that knows how to draw38* for the current platform (A factory for the proper Graphics implementation)39* @param {string|number} width The width in pixels. Strings40* expressing percentages of parent with (e.g. '80%') are also accepted.41* @param {string|number} height The height in pixels. Strings42* expressing percentages of parent with (e.g. '80%') are also accepted.43* @param {?number=} opt_coordWidth The optional coordinate width - if44* omitted or null, defaults to same as width.45* @param {?number=} opt_coordHeight The optional coordinate height - if46* omitted or null, defaults to same as height.47* @param {goog.dom.DomHelper=} opt_domHelper The DOM helper object for the48* document we want to render in.49* @return {!goog.graphics.AbstractGraphics} The created instance.50* @deprecated goog.graphics is deprecated. It existed to abstract over browser51* differences before the canvas tag was widely supported. See52* http://en.wikipedia.org/wiki/Canvas_element for details.53*/54goog.graphics.createGraphics = function(55width, height, opt_coordWidth, opt_coordHeight, opt_domHelper) {56var graphics;57// On IE9 and above, SVG is available, except in compatibility mode.58// We check createElementNS on document object that is not exist in59// compatibility mode.60if (goog.userAgent.IE && (!goog.userAgent.isVersionOrHigher('9') ||61!(opt_domHelper || goog.dom.getDomHelper())62.getDocument()63.createElementNS)) {64graphics = new goog.graphics.VmlGraphics(65width, height, opt_coordWidth, opt_coordHeight, opt_domHelper);66} else if (67goog.userAgent.WEBKIT &&68(!goog.userAgent.isVersionOrHigher('420') || goog.userAgent.MOBILE)) {69graphics = new goog.graphics.CanvasGraphics(70width, height, opt_coordWidth, opt_coordHeight, opt_domHelper);71} else {72graphics = new goog.graphics.SvgGraphics(73width, height, opt_coordWidth, opt_coordHeight, opt_domHelper);74}7576// Create the dom now, because all drawing methods require that the77// main dom element (the canvas) has been already created.78graphics.createDom();7980return graphics;81};828384/**85* Returns an instance of goog.graphics.AbstractGraphics that knows how to draw86* for the current platform (A factory for the proper Graphics implementation)87* @param {string|number} width The width in pixels. Strings88* expressing percentages of parent with (e.g. '80%') are also accepted.89* @param {string|number} height The height in pixels. Strings90* expressing percentages of parent with (e.g. '80%') are also accepted.91* @param {?number=} opt_coordWidth The optional coordinate width, defaults to92* same as width.93* @param {?number=} opt_coordHeight The optional coordinate height, defaults to94* same as height.95* @param {goog.dom.DomHelper=} opt_domHelper The DOM helper object for the96* document we want to render in.97* @return {!goog.graphics.AbstractGraphics} The created instance.98* @deprecated goog.graphics is deprecated. It existed to abstract over browser99* differences before the canvas tag was widely supported. See100* http://en.wikipedia.org/wiki/Canvas_element for details.101*/102goog.graphics.createSimpleGraphics = function(103width, height, opt_coordWidth, opt_coordHeight, opt_domHelper) {104if (goog.userAgent.MAC && goog.userAgent.GECKO &&105!goog.userAgent.isVersionOrHigher('1.9a')) {106// Canvas is 6x faster than SVG on Mac FF 2.0107var graphics = new goog.graphics.CanvasGraphics(108width, height, opt_coordWidth, opt_coordHeight, opt_domHelper);109graphics.createDom();110return graphics;111}112113// Otherwise, defer to normal graphics object creation.114return goog.graphics.createGraphics(115width, height, opt_coordWidth, opt_coordHeight, opt_domHelper);116};117118119/**120* Static function to check if the current browser has Graphics support.121* @return {boolean} True if the current browser has Graphics support.122* @deprecated goog.graphics is deprecated. It existed to abstract over browser123* differences before the canvas tag was widely supported. See124* http://en.wikipedia.org/wiki/Canvas_element for details.125*/126goog.graphics.isBrowserSupported = function() {127if (goog.userAgent.IE) {128return goog.userAgent.isVersionOrHigher('5.5');129}130if (goog.userAgent.GECKO) {131return goog.userAgent.isVersionOrHigher('1.8');132}133if (goog.userAgent.OPERA) {134return goog.userAgent.isVersionOrHigher('9.0');135}136if (goog.userAgent.WEBKIT) {137return goog.userAgent.isVersionOrHigher('412');138}139return false;140};141142143