diff --git a/src/pixi/primitives/Graphics.js b/src/pixi/primitives/Graphics.js index fcfdbb9..90a8b16 100644 --- a/src/pixi/primitives/Graphics.js +++ b/src/pixi/primitives/Graphics.js @@ -4,7 +4,7 @@ /** * The Graphics class contains methods used to draw primitive shapes such as lines, circles and rectangles to the display, and color and fill them. - * + * * @class Graphics * @extends DisplayObjectContainer * @constructor @@ -66,7 +66,7 @@ * @default PIXI.blendModes.NORMAL; */ this.blendMode = PIXI.blendModes.NORMAL; - + /** * Current path * @@ -75,7 +75,7 @@ * @private */ this.currentPath = null; - + /** * Array containing some WebGL-related properties used by the WebGL renderer. * @@ -105,7 +105,7 @@ /** * Used to detect if the graphics object has changed. If this is set to true then the graphics object will be recalculated. - * + * * @property dirty * @type Boolean * @private @@ -114,7 +114,7 @@ /** * Used to detect if the webgl graphics object has changed. If this is set to true then the graphics object will be recalculated. - * + * * @property webGLDirty * @type Boolean * @private @@ -123,7 +123,7 @@ /** * Used to detect if the cached sprite object needs to be updated. - * + * * @property cachedSpriteDirty * @type Boolean * @private @@ -181,7 +181,7 @@ { this.lineWidth = lineWidth || 0; this.lineColor = color || 0; - this.lineAlpha = (arguments.length < 3) ? 1 : alpha; + this.lineAlpha = (alpha === undefined) ? 1 : alpha; if(this.currentPath) { @@ -196,7 +196,7 @@ this.currentPath.lineWidth = this.lineWidth; this.currentPath.lineColor = this.lineColor; this.currentPath.lineAlpha = this.lineAlpha; - + } return this; @@ -261,7 +261,7 @@ n = 20, points = this.currentPath.shape.points; if(points.length === 0)this.moveTo(0, 0); - + var fromX = points[points.length-2]; var fromY = points[points.length-1]; @@ -317,7 +317,7 @@ var fromX = points[points.length-2]; var fromY = points[points.length-1]; - + var j = 0; for (var i=1; i<=n; i++) @@ -330,11 +330,11 @@ t2 = j * j; t3 = t2 * j; - + points.push( dt3 * fromX + 3 * dt2 * j * cpX + 3 * dt * t2 * cpX2 + t3 * toX, dt3 * fromY + 3 * dt2 * j * cpY + 3 * dt * t2 * cpY2 + t3 * toY); } - + this.dirty = true; return this; @@ -342,7 +342,7 @@ /* * The arcTo() method creates an arc/curve between two tangents on the canvas. - * + * * "borrowed" from https://code.google.com/p/fxcanvas/ - thanks google! * * @method arcTo @@ -425,30 +425,12 @@ */ PIXI.Graphics.prototype.arc = function(cx, cy, radius, startAngle, endAngle, anticlockwise) { - var startX = cx + Math.cos(startAngle) * radius; - var startY = cy + Math.sin(startAngle) * radius; - var points; + anticlockwise = anticlockwise || false; - if( this.currentPath ) + if (startAngle === endAngle) { - points = this.currentPath.shape.points; - - if(points.length === 0) - { - points.push(startX, startY); - } - else if( points[points.length-2] !== startX || points[points.length-1] !== startY) - { - points.push(startX, startY); - } + return this; } - else - { - this.moveTo(startX, startY); - points = this.currentPath.shape.points; - } - - if (startAngle === endAngle)return this; if( !anticlockwise && endAngle <= startAngle ) { @@ -459,17 +441,35 @@ startAngle += Math.PI * 2; } - var sweep = anticlockwise ? (startAngle - endAngle) *-1 : (endAngle - startAngle); - var segs = ( Math.abs(sweep)/ (Math.PI * 2) ) * 40; + var sweep = anticlockwise ? (startAngle - endAngle) * -1 : (endAngle - startAngle); + var segs = ( Math.abs(sweep) / (Math.PI * 2) ) * 40; - if( sweep === 0 ) return this; + if(sweep === 0) + { + return this; + } + + var startX = cx + Math.cos(startAngle) * radius; + var startY = cy + Math.sin(startAngle) * radius; + + if (anticlockwise && this.filling) + { + this.moveTo(cx, cy); + } + else + { + this.moveTo(startX, startY); + } + + // currentPath will always exist after calling a moveTo + var points = this.currentPath.shape.points; var theta = sweep/(segs*2); var theta2 = theta*2; var cTheta = Math.cos(theta); var sTheta = Math.sin(theta); - + var segMinus = segs - 1; var remainder = ( segMinus % 1 ) / segMinus; @@ -478,7 +478,7 @@ { var real = i + remainder * i; - + var angle = ((theta) + startAngle + (theta2 * real)); var c = Math.cos(angle); @@ -537,7 +537,7 @@ /** * Draws a rectangle. - * + * * @method drawRect * * @param x {Number} The X coord of the top-left of the rectangle @@ -555,7 +555,7 @@ /** * Draws a rounded rectangle. - * + * * @method drawRoundedRect * * @param x {Number} The X coord of the top-left of the rectangle @@ -651,16 +651,16 @@ resolution = resolution || 1; var bounds = this.getBounds(); - + var canvasBuffer = new PIXI.CanvasBuffer(bounds.width * resolution, bounds.height * resolution); - + var texture = PIXI.Texture.fromCanvas(canvasBuffer.canvas, scaleMode); texture.baseTexture.resolution = resolution; canvasBuffer.context.scale(resolution, resolution); canvasBuffer.context.translate(-bounds.x,-bounds.y); - + PIXI.CanvasGraphics.renderGraphics(this, canvasBuffer.context); return texture; @@ -670,7 +670,7 @@ * Renders the object using the WebGL renderer * * @method _renderWebGL -* @param renderSession {RenderSession} +* @param renderSession {RenderSession} * @private */ PIXI.Graphics.prototype._renderWebGL = function(renderSession) @@ -685,7 +685,7 @@ { this._generateCachedSprite(); - + // we will also need to update the texture on the gpu too! this.updateCachedSpriteTexture(); @@ -705,7 +705,7 @@ if(this._mask)renderSession.maskManager.pushMask(this._mask, renderSession); if(this._filters)renderSession.filterManager.pushFilter(this._filterBlock); - + // check blend mode if(this.blendMode !== renderSession.spriteBatch.currentBlendMode) { @@ -713,16 +713,16 @@ var blendModeWebGL = PIXI.blendModesWebGL[renderSession.spriteBatch.currentBlendMode]; renderSession.spriteBatch.gl.blendFunc(blendModeWebGL[0], blendModeWebGL[1]); } - + // check if the webgl graphic needs to be updated if(this.webGLDirty) { this.dirty = true; this.webGLDirty = false; } - + PIXI.WebGLGraphics.renderGraphics(this, renderSession); - + // only render if it has children! if(this.children.length) { @@ -739,7 +739,7 @@ if(this._filters)renderSession.filterManager.popFilter(); if(this._mask)renderSession.maskManager.popMask(this.mask, renderSession); - + renderSession.drawCount++; renderSession.spriteBatch.start(); @@ -750,20 +750,20 @@ * Renders the object using the Canvas renderer * * @method _renderCanvas -* @param renderSession {RenderSession} +* @param renderSession {RenderSession} * @private */ PIXI.Graphics.prototype._renderCanvas = function(renderSession) { // if the sprite is not visible or the alpha is 0 then no need to render this element if(this.visible === false || this.alpha === 0 || this.isMask === true)return; - + if(this._cacheAsBitmap) { if(this.dirty || this.cachedSpriteDirty) { this._generateCachedSprite(); - + // we will also need to update the texture this.updateCachedSpriteTexture(); @@ -780,7 +780,7 @@ { var context = renderSession.context; var transform = this.worldTransform; - + if(this.blendMode !== renderSession.currentBlendMode) { renderSession.currentBlendMode = this.blendMode; @@ -916,7 +916,7 @@ var type = data.type; var lineWidth = data.lineWidth; shape = data.shape; - + if(type === PIXI.Graphics.RECT || type === PIXI.Graphics.RREC) { @@ -961,7 +961,7 @@ { // POLY points = shape.points; - + for (var j = 0; j < points.length; j+=2) { @@ -985,7 +985,7 @@ } var padding = this.boundsPadding; - + this._localBounds.x = minX - padding; this._localBounds.width = (maxX - minX) + padding * 2; @@ -1007,7 +1007,7 @@ { var canvasBuffer = new PIXI.CanvasBuffer(bounds.width, bounds.height); var texture = PIXI.Texture.fromCanvas(canvasBuffer.canvas); - + this._cachedSprite = new PIXI.Sprite(texture); this._cachedSprite.buffer = canvasBuffer; @@ -1024,8 +1024,8 @@ // this._cachedSprite.buffer.context.save(); this._cachedSprite.buffer.context.translate(-bounds.x,-bounds.y); - - // make sure we set the alpha of the graphics to 1 for the render.. + + // make sure we set the alpha of the graphics to 1 for the render.. this.worldAlpha = 1; // now render the graphic.. @@ -1089,9 +1089,9 @@ this.currentPath = null; var data = new PIXI.GraphicsData(this.lineWidth, this.lineColor, this.lineAlpha, this.fillColor, this.fillAlpha, this.filling, shape); - + this.graphicsData.push(data); - + if(data.type === PIXI.Graphics.POLY) { data.shape.closed = this.filling; @@ -1105,7 +1105,7 @@ /** * A GraphicsData object. - * + * * @class GraphicsData * @constructor */