123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685 |
- goog.provide('goog.graphics.CanvasGraphics');
- goog.require('goog.dom.TagName');
- goog.require('goog.events.EventType');
- goog.require('goog.graphics.AbstractGraphics');
- goog.require('goog.graphics.CanvasEllipseElement');
- goog.require('goog.graphics.CanvasGroupElement');
- goog.require('goog.graphics.CanvasImageElement');
- goog.require('goog.graphics.CanvasPathElement');
- goog.require('goog.graphics.CanvasRectElement');
- goog.require('goog.graphics.CanvasTextElement');
- goog.require('goog.graphics.Font');
- goog.require('goog.graphics.SolidFill');
- goog.require('goog.math.Size');
- goog.require('goog.style');
- goog.graphics.CanvasGraphics = function(
- width, height, opt_coordWidth, opt_coordHeight, opt_domHelper) {
- goog.graphics.AbstractGraphics.call(
- this, width, height, opt_coordWidth, opt_coordHeight, opt_domHelper);
- };
- goog.inherits(goog.graphics.CanvasGraphics, goog.graphics.AbstractGraphics);
- goog.graphics.CanvasGraphics.prototype.setElementFill = function(
- element, fill) {
- this.redraw();
- };
- goog.graphics.CanvasGraphics.prototype.setElementStroke = function(
- element, stroke) {
- this.redraw();
- };
- goog.graphics.CanvasGraphics.prototype.setElementTransform = function(
- element, x, y, angle, centerX, centerY) {
- this.redraw();
- };
- goog.graphics.CanvasGraphics.prototype.setElementAffineTransform = function(
- element, affineTransform) {
- this.redraw();
- };
- goog.graphics.CanvasGraphics.prototype.pushElementTransform = function(
- element) {
- var ctx = this.getContext();
- ctx.save();
- var transform = element.getTransform();
-
- var tx = transform.getTranslateX();
- var ty = transform.getTranslateY();
- if (tx || ty) {
- ctx.translate(tx, ty);
- }
- var sinTheta = transform.getShearY();
- if (sinTheta) {
- ctx.rotate(Math.asin(sinTheta));
- }
- };
- goog.graphics.CanvasGraphics.prototype.popElementTransform = function() {
- this.getContext().restore();
- };
- goog.graphics.CanvasGraphics.prototype.createDom = function() {
- var element = this.dom_.createDom(
- goog.dom.TagName.DIV, {'style': 'position:relative;overflow:hidden'});
- this.setElementInternal(element);
- this.canvas_ = this.dom_.createDom(goog.dom.TagName.CANVAS);
- element.appendChild(this.canvas_);
-
- this.canvasElement = new goog.graphics.CanvasGroupElement(this);
- this.lastGroup_ = this.canvasElement;
- this.redrawTimeout_ = 0;
- this.updateSize();
- };
- goog.graphics.CanvasGraphics.prototype.clearContext_ = function() {
- this.context_ = null;
- };
- goog.graphics.CanvasGraphics.prototype.getContext = function() {
- if (!this.getElement()) {
- this.createDom();
- }
- if (!this.context_) {
- this.context_ = this.canvas_.getContext('2d');
- this.context_.save();
- }
- return this.context_;
- };
- goog.graphics.CanvasGraphics.prototype.setCoordOrigin = function(left, top) {
- this.coordLeft = left;
- this.coordTop = top;
- this.redraw();
- };
- goog.graphics.CanvasGraphics.prototype.setCoordSize = function(
- coordWidth, coordHeight) {
- goog.graphics.CanvasGraphics.superClass_.setCoordSize.apply(this, arguments);
- this.redraw();
- };
- goog.graphics.CanvasGraphics.prototype.setSize = function(
- pixelWidth, pixelHeight) {
- this.width = pixelWidth;
- this.height = pixelHeight;
- this.updateSize();
- this.redraw();
- };
- goog.graphics.CanvasGraphics.prototype.getPixelSize = function() {
-
-
- var width = this.width;
- var height = this.height;
- var computeWidth = goog.isString(width) && width.indexOf('%') != -1;
- var computeHeight = goog.isString(height) && height.indexOf('%') != -1;
- if (!this.isInDocument() && (computeWidth || computeHeight)) {
- return null;
- }
- var parent;
- var parentSize;
- if (computeWidth) {
- parent = (this.getElement().parentNode);
- parentSize = goog.style.getSize(parent);
- width = parseFloat( (width)) * parentSize.width / 100;
- }
- if (computeHeight) {
- parent = parent || (this.getElement().parentNode);
- parentSize = parentSize || goog.style.getSize(parent);
- height =
- parseFloat( (height)) * parentSize.height / 100;
- }
- return new goog.math.Size(
- (width),
- (height));
- };
- goog.graphics.CanvasGraphics.prototype.updateSize = function() {
- goog.style.setSize(this.getElement(), this.width, this.height);
- var pixels = this.getPixelSize();
- if (pixels) {
- goog.style.setSize(
- this.canvas_,
- (pixels.width),
- (pixels.height));
- this.canvas_.width = pixels.width;
- this.canvas_.height = pixels.height;
- this.clearContext_();
- }
- };
- goog.graphics.CanvasGraphics.prototype.reset = function() {
- var ctx = this.getContext();
- ctx.restore();
- var size = this.getPixelSize();
- if (size.width && size.height) {
- ctx.clearRect(0, 0, size.width, size.height);
- }
- ctx.save();
- };
- goog.graphics.CanvasGraphics.prototype.clear = function() {
- this.reset();
- this.canvasElement.clear();
- var el = this.getElement();
-
- while (el.childNodes.length > 1) {
- el.removeChild(el.lastChild);
- }
- };
- goog.graphics.CanvasGraphics.prototype.redraw = function() {
- if (this.preventRedraw_) {
- this.needsRedraw_ = true;
- return;
- }
- if (this.isInDocument()) {
- this.reset();
- if (this.coordWidth) {
- var pixels = this.getPixelSize();
- this.getContext().scale(
- pixels.width / this.coordWidth, pixels.height / this.coordHeight);
- }
- if (this.coordLeft || this.coordTop) {
- this.getContext().translate(-this.coordLeft, -this.coordTop);
- }
- this.pushElementTransform(this.canvasElement);
- this.canvasElement.draw(this.context_);
- this.popElementTransform();
- }
- };
- goog.graphics.CanvasGraphics.prototype.drawElement = function(element) {
- if (element instanceof goog.graphics.CanvasTextElement) {
-
- return;
- }
- var ctx = this.getContext();
- this.pushElementTransform(element);
- if (!element.getFill || !element.getStroke) {
-
- element.draw(ctx);
- this.popElementTransform();
- return;
- }
- var fill = element.getFill();
- if (fill) {
- if (fill instanceof goog.graphics.SolidFill) {
- if (fill.getOpacity() != 0) {
- ctx.globalAlpha = fill.getOpacity();
- ctx.fillStyle = fill.getColor();
- element.draw(ctx);
- ctx.fill();
- ctx.globalAlpha = 1;
- }
- } else {
- var linearGradient = ctx.createLinearGradient(
- fill.getX1(), fill.getY1(), fill.getX2(), fill.getY2());
- linearGradient.addColorStop(0.0, fill.getColor1());
- linearGradient.addColorStop(1.0, fill.getColor2());
- ctx.fillStyle = linearGradient;
- element.draw(ctx);
- ctx.fill();
- }
- }
- var stroke = element.getStroke();
- if (stroke) {
- element.draw(ctx);
- ctx.strokeStyle = stroke.getColor();
- var width = stroke.getWidth();
- if (goog.isString(width) && width.indexOf('px') != -1) {
- width = parseFloat(width) / this.getPixelScaleX();
- }
- ctx.lineWidth = width;
- ctx.stroke();
- }
- this.popElementTransform();
- };
- goog.graphics.CanvasGraphics.prototype.append = function(element, group) {
- group = group || this.canvasElement;
- group.appendChild(element);
- if (this.isDrawable(group)) {
- this.drawElement(element);
- }
- };
- goog.graphics.CanvasGraphics.prototype.drawEllipse = function(
- cx, cy, rx, ry, stroke, fill, opt_group) {
- var element = new goog.graphics.CanvasEllipseElement(
- null, this, cx, cy, rx, ry, stroke, fill);
- this.append(element, opt_group);
- return element;
- };
- goog.graphics.CanvasGraphics.prototype.drawRect = function(
- x, y, width, height, stroke, fill, opt_group) {
- var element = new goog.graphics.CanvasRectElement(
- null, this, x, y, width, height, stroke, fill);
- this.append(element, opt_group);
- return element;
- };
- goog.graphics.CanvasGraphics.prototype.drawImage = function(
- x, y, width, height, src, opt_group) {
- var element = new goog.graphics.CanvasImageElement(
- null, this, x, y, width, height, src);
- this.append(element, opt_group);
- return element;
- };
- goog.graphics.CanvasGraphics.prototype.drawTextOnLine = function(
- text, x1, y1, x2, y2, align, font, stroke, fill, opt_group) {
- var element = new goog.graphics.CanvasTextElement(
- this, text, x1, y1, x2, y2, align,
- (font), stroke, fill);
- this.append(element, opt_group);
- return element;
- };
- goog.graphics.CanvasGraphics.prototype.drawPath = function(
- path, stroke, fill, opt_group) {
- var element =
- new goog.graphics.CanvasPathElement(null, this, path, stroke, fill);
- this.append(element, opt_group);
- return element;
- };
- goog.graphics.CanvasGraphics.prototype.isDrawable = function(group) {
- return this.isInDocument() && !this.redrawTimeout_ &&
- !this.isRedrawRequired(group);
- };
- goog.graphics.CanvasGraphics.prototype.isRedrawRequired = function(group) {
-
- return group != this.canvasElement && group != this.lastGroup_;
- };
- goog.graphics.CanvasGraphics.prototype.createGroup = function(opt_group) {
- var group = new goog.graphics.CanvasGroupElement(this);
- opt_group = opt_group || this.canvasElement;
-
- if (opt_group == this.canvasElement || opt_group == this.lastGroup_) {
- this.lastGroup_ = group;
- }
- this.append(group, opt_group);
- return group;
- };
- goog.graphics.CanvasGraphics.prototype.getTextWidth = goog.abstractMethod;
- goog.graphics.CanvasGraphics.prototype.disposeInternal = function() {
- this.context_ = null;
- goog.graphics.CanvasGraphics.superClass_.disposeInternal.call(this);
- };
- goog.graphics.CanvasGraphics.prototype.enterDocument = function() {
- var oldPixelSize = this.getPixelSize();
- goog.graphics.CanvasGraphics.superClass_.enterDocument.call(this);
- if (!oldPixelSize) {
- this.updateSize();
- this.dispatchEvent(goog.events.EventType.RESIZE);
- }
- this.redraw();
- };
- goog.graphics.CanvasGraphics.prototype.suspend = function() {
- this.preventRedraw_ = true;
- };
- goog.graphics.CanvasGraphics.prototype.resume = function() {
- this.preventRedraw_ = false;
- if (this.needsRedraw_) {
- this.redraw();
- this.needsRedraw_ = false;
- }
- };
- goog.graphics.CanvasGraphics.prototype.removeElement = function(elem) {
- if (!elem) {
- return;
- }
- this.canvasElement.removeElement(elem);
- this.redraw();
- };
|