canvasgraphics.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685
  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 CanvasGraphics sub class that uses the canvas tag for drawing.
  16. * @author robbyw@google.com (Robby Walker)
  17. */
  18. goog.provide('goog.graphics.CanvasGraphics');
  19. goog.require('goog.dom.TagName');
  20. goog.require('goog.events.EventType');
  21. goog.require('goog.graphics.AbstractGraphics');
  22. goog.require('goog.graphics.CanvasEllipseElement');
  23. goog.require('goog.graphics.CanvasGroupElement');
  24. goog.require('goog.graphics.CanvasImageElement');
  25. goog.require('goog.graphics.CanvasPathElement');
  26. goog.require('goog.graphics.CanvasRectElement');
  27. goog.require('goog.graphics.CanvasTextElement');
  28. goog.require('goog.graphics.Font');
  29. goog.require('goog.graphics.SolidFill');
  30. goog.require('goog.math.Size');
  31. goog.require('goog.style');
  32. /**
  33. * A Graphics implementation for drawing using canvas.
  34. * @param {string|number} width The (non-zero) width in pixels. Strings
  35. * expressing percentages of parent with (e.g. '80%') are also accepted.
  36. * @param {string|number} height The (non-zero) height in pixels. Strings
  37. * expressing percentages of parent with (e.g. '80%') are also accepted.
  38. * @param {?number=} opt_coordWidth The coordinate width - if
  39. * omitted or null, defaults to same as width.
  40. * @param {?number=} opt_coordHeight The coordinate height - if
  41. * omitted or null, defaults to same as height.
  42. * @param {goog.dom.DomHelper=} opt_domHelper The DOM helper object for the
  43. * document we want to render in.
  44. * @constructor
  45. * @extends {goog.graphics.AbstractGraphics}
  46. * @deprecated goog.graphics is deprecated. It existed to abstract over browser
  47. * differences before the canvas tag was widely supported. See
  48. * http://en.wikipedia.org/wiki/Canvas_element for details.
  49. */
  50. goog.graphics.CanvasGraphics = function(
  51. width, height, opt_coordWidth, opt_coordHeight, opt_domHelper) {
  52. goog.graphics.AbstractGraphics.call(
  53. this, width, height, opt_coordWidth, opt_coordHeight, opt_domHelper);
  54. };
  55. goog.inherits(goog.graphics.CanvasGraphics, goog.graphics.AbstractGraphics);
  56. /**
  57. * Sets the fill for the given element.
  58. * @param {goog.graphics.StrokeAndFillElement} element The element
  59. * wrapper.
  60. * @param {goog.graphics.Fill} fill The fill object.
  61. * @override
  62. */
  63. goog.graphics.CanvasGraphics.prototype.setElementFill = function(
  64. element, fill) {
  65. this.redraw();
  66. };
  67. /**
  68. * Sets the stroke for the given element.
  69. * @param {goog.graphics.StrokeAndFillElement} element The element
  70. * wrapper.
  71. * @param {goog.graphics.Stroke} stroke The stroke object.
  72. * @override
  73. */
  74. goog.graphics.CanvasGraphics.prototype.setElementStroke = function(
  75. element, stroke) {
  76. this.redraw();
  77. };
  78. /**
  79. * Set the translation and rotation of an element.
  80. *
  81. * If a more general affine transform is needed than this provides
  82. * (e.g. skew and scale) then use setElementAffineTransform.
  83. * @param {goog.graphics.Element} element The element wrapper.
  84. * @param {number} x The x coordinate of the translation transform.
  85. * @param {number} y The y coordinate of the translation transform.
  86. * @param {number} angle The angle of the rotation transform.
  87. * @param {number} centerX The horizontal center of the rotation transform.
  88. * @param {number} centerY The vertical center of the rotation transform.
  89. * @override
  90. */
  91. goog.graphics.CanvasGraphics.prototype.setElementTransform = function(
  92. element, x, y, angle, centerX, centerY) {
  93. this.redraw();
  94. };
  95. /**
  96. * Set the transformation of an element.
  97. *
  98. * Note that in this implementation this method just calls this.redraw()
  99. * and the affineTransform param is unused.
  100. * @param {!goog.graphics.Element} element The element wrapper.
  101. * @param {!goog.graphics.AffineTransform} affineTransform The
  102. * transformation applied to this element.
  103. * @override
  104. */
  105. goog.graphics.CanvasGraphics.prototype.setElementAffineTransform = function(
  106. element, affineTransform) {
  107. this.redraw();
  108. };
  109. /**
  110. * Push an element transform on to the transform stack.
  111. * @param {goog.graphics.Element} element The transformed element.
  112. */
  113. goog.graphics.CanvasGraphics.prototype.pushElementTransform = function(
  114. element) {
  115. var ctx = this.getContext();
  116. ctx.save();
  117. var transform = element.getTransform();
  118. // TODO(robbyw): Test for unsupported transforms i.e. skews.
  119. var tx = transform.getTranslateX();
  120. var ty = transform.getTranslateY();
  121. if (tx || ty) {
  122. ctx.translate(tx, ty);
  123. }
  124. var sinTheta = transform.getShearY();
  125. if (sinTheta) {
  126. ctx.rotate(Math.asin(sinTheta));
  127. }
  128. };
  129. /**
  130. * Pop an element transform off of the transform stack.
  131. */
  132. goog.graphics.CanvasGraphics.prototype.popElementTransform = function() {
  133. this.getContext().restore();
  134. };
  135. /**
  136. * Creates the DOM representation of the graphics area.
  137. * @override
  138. */
  139. goog.graphics.CanvasGraphics.prototype.createDom = function() {
  140. var element = this.dom_.createDom(
  141. goog.dom.TagName.DIV, {'style': 'position:relative;overflow:hidden'});
  142. this.setElementInternal(element);
  143. this.canvas_ = this.dom_.createDom(goog.dom.TagName.CANVAS);
  144. element.appendChild(this.canvas_);
  145. /**
  146. * The main canvas element.
  147. * @type {goog.graphics.CanvasGroupElement}
  148. */
  149. this.canvasElement = new goog.graphics.CanvasGroupElement(this);
  150. this.lastGroup_ = this.canvasElement;
  151. this.redrawTimeout_ = 0;
  152. this.updateSize();
  153. };
  154. /**
  155. * Clears the drawing context object in response to actions that make the old
  156. * context invalid - namely resize of the canvas element.
  157. * @private
  158. */
  159. goog.graphics.CanvasGraphics.prototype.clearContext_ = function() {
  160. this.context_ = null;
  161. };
  162. /**
  163. * Returns the drawing context.
  164. * @return {Object} The canvas element rendering context.
  165. */
  166. goog.graphics.CanvasGraphics.prototype.getContext = function() {
  167. if (!this.getElement()) {
  168. this.createDom();
  169. }
  170. if (!this.context_) {
  171. this.context_ = this.canvas_.getContext('2d');
  172. this.context_.save();
  173. }
  174. return this.context_;
  175. };
  176. /**
  177. * Changes the coordinate system position.
  178. * @param {number} left The coordinate system left bound.
  179. * @param {number} top The coordinate system top bound.
  180. * @override
  181. */
  182. goog.graphics.CanvasGraphics.prototype.setCoordOrigin = function(left, top) {
  183. this.coordLeft = left;
  184. this.coordTop = top;
  185. this.redraw();
  186. };
  187. /**
  188. * Changes the coordinate size.
  189. * @param {number} coordWidth The coordinate width.
  190. * @param {number} coordHeight The coordinate height.
  191. * @override
  192. */
  193. goog.graphics.CanvasGraphics.prototype.setCoordSize = function(
  194. coordWidth, coordHeight) {
  195. goog.graphics.CanvasGraphics.superClass_.setCoordSize.apply(this, arguments);
  196. this.redraw();
  197. };
  198. /**
  199. * Change the size of the canvas.
  200. * @param {number} pixelWidth The width in pixels.
  201. * @param {number} pixelHeight The height in pixels.
  202. * @override
  203. */
  204. goog.graphics.CanvasGraphics.prototype.setSize = function(
  205. pixelWidth, pixelHeight) {
  206. this.width = pixelWidth;
  207. this.height = pixelHeight;
  208. this.updateSize();
  209. this.redraw();
  210. };
  211. /** @override */
  212. goog.graphics.CanvasGraphics.prototype.getPixelSize = function() {
  213. // goog.style.getSize does not work for Canvas elements. We
  214. // have to compute the size manually if it is percentage based.
  215. var width = this.width;
  216. var height = this.height;
  217. var computeWidth = goog.isString(width) && width.indexOf('%') != -1;
  218. var computeHeight = goog.isString(height) && height.indexOf('%') != -1;
  219. if (!this.isInDocument() && (computeWidth || computeHeight)) {
  220. return null;
  221. }
  222. var parent;
  223. var parentSize;
  224. if (computeWidth) {
  225. parent = /** @type {Element} */ (this.getElement().parentNode);
  226. parentSize = goog.style.getSize(parent);
  227. width = parseFloat(/** @type {string} */ (width)) * parentSize.width / 100;
  228. }
  229. if (computeHeight) {
  230. parent = parent || /** @type {Element} */ (this.getElement().parentNode);
  231. parentSize = parentSize || goog.style.getSize(parent);
  232. height =
  233. parseFloat(/** @type {string} */ (height)) * parentSize.height / 100;
  234. }
  235. return new goog.math.Size(
  236. /** @type {number} */ (width),
  237. /** @type {number} */ (height));
  238. };
  239. /**
  240. * Update the size of the canvas.
  241. */
  242. goog.graphics.CanvasGraphics.prototype.updateSize = function() {
  243. goog.style.setSize(this.getElement(), this.width, this.height);
  244. var pixels = this.getPixelSize();
  245. if (pixels) {
  246. goog.style.setSize(
  247. this.canvas_,
  248. /** @type {number} */ (pixels.width),
  249. /** @type {number} */ (pixels.height));
  250. this.canvas_.width = pixels.width;
  251. this.canvas_.height = pixels.height;
  252. this.clearContext_();
  253. }
  254. };
  255. /**
  256. * Reset the canvas.
  257. */
  258. goog.graphics.CanvasGraphics.prototype.reset = function() {
  259. var ctx = this.getContext();
  260. ctx.restore();
  261. var size = this.getPixelSize();
  262. if (size.width && size.height) {
  263. ctx.clearRect(0, 0, size.width, size.height);
  264. }
  265. ctx.save();
  266. };
  267. /**
  268. * Remove all drawing elements from the graphics.
  269. * @override
  270. */
  271. goog.graphics.CanvasGraphics.prototype.clear = function() {
  272. this.reset();
  273. this.canvasElement.clear();
  274. var el = this.getElement();
  275. // Remove all children (text nodes) except the canvas (which is at index 0)
  276. while (el.childNodes.length > 1) {
  277. el.removeChild(el.lastChild);
  278. }
  279. };
  280. /**
  281. * Redraw the entire canvas.
  282. */
  283. goog.graphics.CanvasGraphics.prototype.redraw = function() {
  284. if (this.preventRedraw_) {
  285. this.needsRedraw_ = true;
  286. return;
  287. }
  288. if (this.isInDocument()) {
  289. this.reset();
  290. if (this.coordWidth) {
  291. var pixels = this.getPixelSize();
  292. this.getContext().scale(
  293. pixels.width / this.coordWidth, pixels.height / this.coordHeight);
  294. }
  295. if (this.coordLeft || this.coordTop) {
  296. this.getContext().translate(-this.coordLeft, -this.coordTop);
  297. }
  298. this.pushElementTransform(this.canvasElement);
  299. this.canvasElement.draw(this.context_);
  300. this.popElementTransform();
  301. }
  302. };
  303. /**
  304. * Draw an element, including any stroke or fill.
  305. * @param {goog.graphics.Element} element The element to draw.
  306. */
  307. goog.graphics.CanvasGraphics.prototype.drawElement = function(element) {
  308. if (element instanceof goog.graphics.CanvasTextElement) {
  309. // Don't draw text since that is not implemented using canvas.
  310. return;
  311. }
  312. var ctx = this.getContext();
  313. this.pushElementTransform(element);
  314. if (!element.getFill || !element.getStroke) {
  315. // Draw without stroke or fill (e.g. the element is an image or group).
  316. element.draw(ctx);
  317. this.popElementTransform();
  318. return;
  319. }
  320. var fill = element.getFill();
  321. if (fill) {
  322. if (fill instanceof goog.graphics.SolidFill) {
  323. if (fill.getOpacity() != 0) {
  324. ctx.globalAlpha = fill.getOpacity();
  325. ctx.fillStyle = fill.getColor();
  326. element.draw(ctx);
  327. ctx.fill();
  328. ctx.globalAlpha = 1;
  329. }
  330. } else { // (fill instanceof goog.graphics.LinearGradient)
  331. var linearGradient = ctx.createLinearGradient(
  332. fill.getX1(), fill.getY1(), fill.getX2(), fill.getY2());
  333. linearGradient.addColorStop(0.0, fill.getColor1());
  334. linearGradient.addColorStop(1.0, fill.getColor2());
  335. ctx.fillStyle = linearGradient;
  336. element.draw(ctx);
  337. ctx.fill();
  338. }
  339. }
  340. var stroke = element.getStroke();
  341. if (stroke) {
  342. element.draw(ctx);
  343. ctx.strokeStyle = stroke.getColor();
  344. var width = stroke.getWidth();
  345. if (goog.isString(width) && width.indexOf('px') != -1) {
  346. width = parseFloat(width) / this.getPixelScaleX();
  347. }
  348. ctx.lineWidth = width;
  349. ctx.stroke();
  350. }
  351. this.popElementTransform();
  352. };
  353. /**
  354. * Append an element.
  355. *
  356. * @param {goog.graphics.Element} element The element to draw.
  357. * @param {goog.graphics.GroupElement|undefined} group The group to draw
  358. * it in. If null or undefined, defaults to the root group.
  359. * @protected
  360. */
  361. goog.graphics.CanvasGraphics.prototype.append = function(element, group) {
  362. group = group || this.canvasElement;
  363. group.appendChild(element);
  364. if (this.isDrawable(group)) {
  365. this.drawElement(element);
  366. }
  367. };
  368. /**
  369. * Draw an ellipse.
  370. *
  371. * @param {number} cx Center X coordinate.
  372. * @param {number} cy Center Y coordinate.
  373. * @param {number} rx Radius length for the x-axis.
  374. * @param {number} ry Radius length for the y-axis.
  375. * @param {goog.graphics.Stroke} stroke Stroke object describing the
  376. * stroke.
  377. * @param {goog.graphics.Fill} fill Fill object describing the fill.
  378. * @param {goog.graphics.GroupElement=} opt_group The group wrapper
  379. * element to append to. If not specified, appends to the main canvas.
  380. *
  381. * @return {!goog.graphics.EllipseElement} The newly created element.
  382. * @override
  383. */
  384. goog.graphics.CanvasGraphics.prototype.drawEllipse = function(
  385. cx, cy, rx, ry, stroke, fill, opt_group) {
  386. var element = new goog.graphics.CanvasEllipseElement(
  387. null, this, cx, cy, rx, ry, stroke, fill);
  388. this.append(element, opt_group);
  389. return element;
  390. };
  391. /**
  392. * Draw a rectangle.
  393. *
  394. * @param {number} x X coordinate (left).
  395. * @param {number} y Y coordinate (top).
  396. * @param {number} width Width of rectangle.
  397. * @param {number} height Height of rectangle.
  398. * @param {goog.graphics.Stroke} stroke Stroke object describing the
  399. * stroke.
  400. * @param {goog.graphics.Fill} fill Fill object describing the fill.
  401. * @param {goog.graphics.GroupElement=} opt_group The group wrapper
  402. * element to append to. If not specified, appends to the main canvas.
  403. *
  404. * @return {!goog.graphics.RectElement} The newly created element.
  405. * @override
  406. */
  407. goog.graphics.CanvasGraphics.prototype.drawRect = function(
  408. x, y, width, height, stroke, fill, opt_group) {
  409. var element = new goog.graphics.CanvasRectElement(
  410. null, this, x, y, width, height, stroke, fill);
  411. this.append(element, opt_group);
  412. return element;
  413. };
  414. /**
  415. * Draw an image.
  416. *
  417. * @param {number} x X coordinate (left).
  418. * @param {number} y Y coordinate (top).
  419. * @param {number} width Width of image.
  420. * @param {number} height Height of image.
  421. * @param {string} src Source of the image.
  422. * @param {goog.graphics.GroupElement=} opt_group The group wrapper
  423. * element to append to. If not specified, appends to the main canvas.
  424. *
  425. * @return {!goog.graphics.ImageElement} The newly created element.
  426. */
  427. goog.graphics.CanvasGraphics.prototype.drawImage = function(
  428. x, y, width, height, src, opt_group) {
  429. var element = new goog.graphics.CanvasImageElement(
  430. null, this, x, y, width, height, src);
  431. this.append(element, opt_group);
  432. return element;
  433. };
  434. /**
  435. * Draw a text string vertically centered on a given line.
  436. *
  437. * @param {string} text The text to draw.
  438. * @param {number} x1 X coordinate of start of line.
  439. * @param {number} y1 Y coordinate of start of line.
  440. * @param {number} x2 X coordinate of end of line.
  441. * @param {number} y2 Y coordinate of end of line.
  442. * @param {?string} align Horizontal alignment: left (default), center, right.
  443. * @param {goog.graphics.Font} font Font describing the font properties.
  444. * @param {goog.graphics.Stroke} stroke Stroke object describing the stroke.
  445. * @param {goog.graphics.Fill} fill Fill object describing the fill.
  446. * @param {goog.graphics.GroupElement=} opt_group The group wrapper
  447. * element to append to. If not specified, appends to the main canvas.
  448. *
  449. * @return {!goog.graphics.TextElement} The newly created element.
  450. * @override
  451. */
  452. goog.graphics.CanvasGraphics.prototype.drawTextOnLine = function(
  453. text, x1, y1, x2, y2, align, font, stroke, fill, opt_group) {
  454. var element = new goog.graphics.CanvasTextElement(
  455. this, text, x1, y1, x2, y2, align,
  456. /** @type {!goog.graphics.Font} */ (font), stroke, fill);
  457. this.append(element, opt_group);
  458. return element;
  459. };
  460. /**
  461. * Draw a path.
  462. * @param {!goog.graphics.Path} path The path object to draw.
  463. * @param {goog.graphics.Stroke} stroke Stroke object describing the stroke.
  464. * @param {goog.graphics.Fill} fill Fill object describing the fill.
  465. * @param {goog.graphics.GroupElement=} opt_group The group wrapper
  466. * element to append to. If not specified, appends to the main canvas.
  467. *
  468. * @return {!goog.graphics.PathElement} The newly created element.
  469. * @override
  470. */
  471. goog.graphics.CanvasGraphics.prototype.drawPath = function(
  472. path, stroke, fill, opt_group) {
  473. var element =
  474. new goog.graphics.CanvasPathElement(null, this, path, stroke, fill);
  475. this.append(element, opt_group);
  476. return element;
  477. };
  478. /**
  479. * @param {goog.graphics.GroupElement} group The group to possibly
  480. * draw to.
  481. * @return {boolean} Whether drawing can occur now.
  482. */
  483. goog.graphics.CanvasGraphics.prototype.isDrawable = function(group) {
  484. return this.isInDocument() && !this.redrawTimeout_ &&
  485. !this.isRedrawRequired(group);
  486. };
  487. /**
  488. * Returns true if drawing to the given group means a redraw is required.
  489. * @param {goog.graphics.GroupElement} group The group to draw to.
  490. * @return {boolean} Whether drawing to this group should force a redraw.
  491. */
  492. goog.graphics.CanvasGraphics.prototype.isRedrawRequired = function(group) {
  493. // TODO(robbyw): Moving up to any parent of lastGroup should not force redraw.
  494. return group != this.canvasElement && group != this.lastGroup_;
  495. };
  496. /**
  497. * Create an empty group of drawing elements.
  498. *
  499. * @param {goog.graphics.GroupElement=} opt_group The group wrapper
  500. * element to append to. If not specified, appends to the main canvas.
  501. *
  502. * @return {!goog.graphics.CanvasGroupElement} The newly created group.
  503. * @override
  504. */
  505. goog.graphics.CanvasGraphics.prototype.createGroup = function(opt_group) {
  506. var group = new goog.graphics.CanvasGroupElement(this);
  507. opt_group = opt_group || this.canvasElement;
  508. // TODO(robbyw): Moving up to any parent group should not force redraw.
  509. if (opt_group == this.canvasElement || opt_group == this.lastGroup_) {
  510. this.lastGroup_ = group;
  511. }
  512. this.append(group, opt_group);
  513. return group;
  514. };
  515. /**
  516. * Measure and return the width (in pixels) of a given text string.
  517. * Text measurement is needed to make sure a text can fit in the allocated
  518. * area. The way text length is measured is by writing it into a div that is
  519. * after the visible area, measure the div width, and immediately erase the
  520. * written value.
  521. *
  522. * @param {string} text The text string to measure.
  523. * @param {goog.graphics.Font} font The font object describing the font style.
  524. * @override
  525. */
  526. goog.graphics.CanvasGraphics.prototype.getTextWidth = goog.abstractMethod;
  527. /**
  528. * Disposes of the component by removing event handlers, detacing DOM nodes from
  529. * the document body, and removing references to them.
  530. * @override
  531. * @protected
  532. */
  533. goog.graphics.CanvasGraphics.prototype.disposeInternal = function() {
  534. this.context_ = null;
  535. goog.graphics.CanvasGraphics.superClass_.disposeInternal.call(this);
  536. };
  537. /** @override */
  538. goog.graphics.CanvasGraphics.prototype.enterDocument = function() {
  539. var oldPixelSize = this.getPixelSize();
  540. goog.graphics.CanvasGraphics.superClass_.enterDocument.call(this);
  541. if (!oldPixelSize) {
  542. this.updateSize();
  543. this.dispatchEvent(goog.events.EventType.RESIZE);
  544. }
  545. this.redraw();
  546. };
  547. /**
  548. * Start preventing redraws - useful for chaining large numbers of changes
  549. * together. Not guaranteed to do anything - i.e. only use this for
  550. * optimization of a single code path.
  551. * @override
  552. */
  553. goog.graphics.CanvasGraphics.prototype.suspend = function() {
  554. this.preventRedraw_ = true;
  555. };
  556. /**
  557. * Stop preventing redraws. If any redraws had been prevented, a redraw will
  558. * be done now.
  559. * @override
  560. */
  561. goog.graphics.CanvasGraphics.prototype.resume = function() {
  562. this.preventRedraw_ = false;
  563. if (this.needsRedraw_) {
  564. this.redraw();
  565. this.needsRedraw_ = false;
  566. }
  567. };
  568. /**
  569. * Removes an element from the Canvas.
  570. * @param {goog.graphics.Element} elem the element to remove.
  571. * @override
  572. */
  573. goog.graphics.CanvasGraphics.prototype.removeElement = function(elem) {
  574. if (!elem) {
  575. return;
  576. }
  577. this.canvasElement.removeElement(elem);
  578. this.redraw();
  579. };