diff --git a/src/core/graphics/Graphics.js b/src/core/graphics/Graphics.js index 8c7249d..59012a9 100644 --- a/src/core/graphics/Graphics.js +++ b/src/core/graphics/Graphics.js @@ -1,8 +1,10 @@ var Container = require('../display/Container'), Texture = require('../textures/Texture'), + RenderTexture = require('../textures/RenderTexture'), CanvasBuffer = require('../renderers/canvas/utils/CanvasBuffer'), CanvasGraphics = require('../renderers/canvas/utils/CanvasGraphics'), GraphicsData = require('./GraphicsData'), + Sprite = require('../sprites/Sprite'), math = require('../math'), CONST = require('../const'), bezierCurveTo = require('./utils/bezierCurveTo'), @@ -146,6 +148,9 @@ */ this.cachedSpriteDirty = false; + + this._spriteRect = null; + /** * When cacheAsBitmap is set to true the graphics object will be rendered as if it was a sprite. * This is useful if your graphics element does not change often, as it will speed up the rendering @@ -160,6 +165,8 @@ */ } +Graphics._SPRITE_TEXTURE = null; + // constructor Graphics.prototype = Object.create(Container.prototype); Graphics.prototype.constructor = Graphics; @@ -697,6 +704,8 @@ return texture; }; +var texture = null; + /** * Renders the object using the WebGL renderer * @@ -713,13 +722,47 @@ this.glDirty = false; } - renderer.setObjectRenderer(renderer.plugins.graphics); - + if(this.graphicsData.length === 1 && this.graphicsData[0].shape.type === CONST.SHAPES.RECT) + { + this._renderSpriteRect(renderer); + } + else + { - renderer.plugins.graphics.render(this); + renderer.setObjectRenderer(renderer.plugins.graphics); + renderer.plugins.graphics.render(this); + } }; +Graphics.prototype._renderSpriteRect = function (renderer) +{ + var rect = this.graphicsData[0].shape; + if(!this._spriteRect) + { + if(!Graphics._SPRITE_TEXTURE) + { + Graphics._SPRITE_TEXTURE = RenderTexture.create(10, 10); + + var currentRenderTarget = renderer._activeRenderTarget + renderer.bindRenderTexture(Graphics._SPRITE_TEXTURE); + renderer.clear([1,1,1,1]); + renderer.bindRenderTarget(currentRenderTarget); + } + + this._spriteRect = new Sprite(Graphics._SPRITE_TEXTURE); + this._spriteRect.tint = this.graphicsData[0].fillColor; + } + + Graphics._SPRITE_TEXTURE.crop.width = rect.width; + Graphics._SPRITE_TEXTURE.crop.height = rect.height; + + this._spriteRect.anchor.x = -rect.x / rect.width; + this._spriteRect.anchor.y = -rect.y / rect.height; + + this._spriteRect._renderWebGL(renderer); +} + /** * Renders the object using the Canvas renderer * @@ -743,12 +786,14 @@ var transform = this.worldTransform; var compositeOperation = renderer.blendModes[this.blendMode]; + if (compositeOperation !== context.globalCompositeOperation) { context.globalCompositeOperation = compositeOperation; } var resolution = renderer.resolution; + context.setTransform( transform.a * resolution, transform.b * resolution,