diff --git a/examples/example 13 - Graphics/index.html b/examples/example 13 - Graphics/index.html index f91cd3f..8587309 100644 --- a/examples/example 13 - Graphics/index.html +++ b/examples/example 13 - Graphics/index.html @@ -24,7 +24,9 @@ // create an new instance of a pixi stage - var stage = new PIXI.Stage(0x000000); + var stage = new PIXI.Stage(0x000000, true); + + stage.setInteractive(true); // create a renderer instance //var renderer = new PIXI.CanvasRenderer(800, 600);//PIXI.autoDetectRenderer(800, 600); @@ -96,11 +98,20 @@ stage.addChild(sprite); stage.addChild(graphics); + stage.click = function() + { + graphics.clear(); + } + requestAnimFrame(animate); function animate() { renderer.render(stage); requestAnimFrame( animate ); + graphics.lineStyle(Math.random() * 30, Math.random() * 0xFFFFFF, 0.8); + + graphics.moveTo(Math.random() * 600,Math.random() * 600); + graphics.lineTo(Math.random() * 600,Math.random() * 600); } diff --git a/examples/example 13 - Graphics/index.html b/examples/example 13 - Graphics/index.html index f91cd3f..8587309 100644 --- a/examples/example 13 - Graphics/index.html +++ b/examples/example 13 - Graphics/index.html @@ -24,7 +24,9 @@ // create an new instance of a pixi stage - var stage = new PIXI.Stage(0x000000); + var stage = new PIXI.Stage(0x000000, true); + + stage.setInteractive(true); // create a renderer instance //var renderer = new PIXI.CanvasRenderer(800, 600);//PIXI.autoDetectRenderer(800, 600); @@ -96,11 +98,20 @@ stage.addChild(sprite); stage.addChild(graphics); + stage.click = function() + { + graphics.clear(); + } + requestAnimFrame(animate); function animate() { renderer.render(stage); requestAnimFrame( animate ); + graphics.lineStyle(Math.random() * 30, Math.random() * 0xFFFFFF, 0.8); + + graphics.moveTo(Math.random() * 600,Math.random() * 600); + graphics.lineTo(Math.random() * 600,Math.random() * 600); } diff --git a/src/pixi/primitives/Graphics.js b/src/pixi/primitives/Graphics.js index a97762c..1c82d2b 100644 --- a/src/pixi/primitives/Graphics.js +++ b/src/pixi/primitives/Graphics.js @@ -62,6 +62,7 @@ PIXI.Graphics.prototype.lineTo = function(x, y) { this.currentPath.points.push(x, y); + this.dirty = true; } PIXI.Graphics.prototype.beginFill = function(color, alpha) @@ -91,6 +92,7 @@ points:[x, y, width, height], type:PIXI.Graphics.RECT}; this.graphicsData.push(this.currentPath); + this.dirty = true; } PIXI.Graphics.prototype.drawCircle = function( x, y, radius) @@ -100,4 +102,12 @@ points:[x, y, radius], type:PIXI.Graphics.CIRC}; this.graphicsData.push(this.currentPath); + this.dirty = true; +} + +PIXI.Graphics.prototype.clear = function() +{ + this.dirty = true; + this.clearDirty = true; + this.graphicsData = []; } diff --git a/examples/example 13 - Graphics/index.html b/examples/example 13 - Graphics/index.html index f91cd3f..8587309 100644 --- a/examples/example 13 - Graphics/index.html +++ b/examples/example 13 - Graphics/index.html @@ -24,7 +24,9 @@ // create an new instance of a pixi stage - var stage = new PIXI.Stage(0x000000); + var stage = new PIXI.Stage(0x000000, true); + + stage.setInteractive(true); // create a renderer instance //var renderer = new PIXI.CanvasRenderer(800, 600);//PIXI.autoDetectRenderer(800, 600); @@ -96,11 +98,20 @@ stage.addChild(sprite); stage.addChild(graphics); + stage.click = function() + { + graphics.clear(); + } + requestAnimFrame(animate); function animate() { renderer.render(stage); requestAnimFrame( animate ); + graphics.lineStyle(Math.random() * 30, Math.random() * 0xFFFFFF, 0.8); + + graphics.moveTo(Math.random() * 600,Math.random() * 600); + graphics.lineTo(Math.random() * 600,Math.random() * 600); } diff --git a/src/pixi/primitives/Graphics.js b/src/pixi/primitives/Graphics.js index a97762c..1c82d2b 100644 --- a/src/pixi/primitives/Graphics.js +++ b/src/pixi/primitives/Graphics.js @@ -62,6 +62,7 @@ PIXI.Graphics.prototype.lineTo = function(x, y) { this.currentPath.points.push(x, y); + this.dirty = true; } PIXI.Graphics.prototype.beginFill = function(color, alpha) @@ -91,6 +92,7 @@ points:[x, y, width, height], type:PIXI.Graphics.RECT}; this.graphicsData.push(this.currentPath); + this.dirty = true; } PIXI.Graphics.prototype.drawCircle = function( x, y, radius) @@ -100,4 +102,12 @@ points:[x, y, radius], type:PIXI.Graphics.CIRC}; this.graphicsData.push(this.currentPath); + this.dirty = true; +} + +PIXI.Graphics.prototype.clear = function() +{ + this.dirty = true; + this.clearDirty = true; + this.graphicsData = []; } diff --git a/src/pixi/renderers/CanvasGraphics.js b/src/pixi/renderers/CanvasGraphics.js index 2396467..fa26d3f 100644 --- a/src/pixi/renderers/CanvasGraphics.js +++ b/src/pixi/renderers/CanvasGraphics.js @@ -24,8 +24,8 @@ var data = graphics.graphicsData[i]; var points = data.points; - context.strokeStyle = "#" + data.lineColor.toString(16); - + context.strokeStyle = color = '#' + ('00000' + ( data.lineColor | 0).toString(16)).substr(-6); + context.lineWidth = data.lineWidth; context.globalAlpha = data.lineAlpha; @@ -55,7 +55,8 @@ // TODO - need to be Undefined! if(data.fillColor) { - context.fillStyle = "#" + data.fillColor.toString(16); + context.fillStyle = color = '#' + ('00000' + ( data.fillColor | 0).toString(16)).substr(-6); + context.fillRect(points[0], points[1], points[2], points[3]); } @@ -73,7 +74,7 @@ if(data.fill) { - context.fillStyle = "#" + data.fillColor.toString(16); + context.fillStyle = color = '#' + ('00000' + ( data.fillColor | 0).toString(16)).substr(-6); context.fill(); } if(data.lineWidth) diff --git a/examples/example 13 - Graphics/index.html b/examples/example 13 - Graphics/index.html index f91cd3f..8587309 100644 --- a/examples/example 13 - Graphics/index.html +++ b/examples/example 13 - Graphics/index.html @@ -24,7 +24,9 @@ // create an new instance of a pixi stage - var stage = new PIXI.Stage(0x000000); + var stage = new PIXI.Stage(0x000000, true); + + stage.setInteractive(true); // create a renderer instance //var renderer = new PIXI.CanvasRenderer(800, 600);//PIXI.autoDetectRenderer(800, 600); @@ -96,11 +98,20 @@ stage.addChild(sprite); stage.addChild(graphics); + stage.click = function() + { + graphics.clear(); + } + requestAnimFrame(animate); function animate() { renderer.render(stage); requestAnimFrame( animate ); + graphics.lineStyle(Math.random() * 30, Math.random() * 0xFFFFFF, 0.8); + + graphics.moveTo(Math.random() * 600,Math.random() * 600); + graphics.lineTo(Math.random() * 600,Math.random() * 600); } diff --git a/src/pixi/primitives/Graphics.js b/src/pixi/primitives/Graphics.js index a97762c..1c82d2b 100644 --- a/src/pixi/primitives/Graphics.js +++ b/src/pixi/primitives/Graphics.js @@ -62,6 +62,7 @@ PIXI.Graphics.prototype.lineTo = function(x, y) { this.currentPath.points.push(x, y); + this.dirty = true; } PIXI.Graphics.prototype.beginFill = function(color, alpha) @@ -91,6 +92,7 @@ points:[x, y, width, height], type:PIXI.Graphics.RECT}; this.graphicsData.push(this.currentPath); + this.dirty = true; } PIXI.Graphics.prototype.drawCircle = function( x, y, radius) @@ -100,4 +102,12 @@ points:[x, y, radius], type:PIXI.Graphics.CIRC}; this.graphicsData.push(this.currentPath); + this.dirty = true; +} + +PIXI.Graphics.prototype.clear = function() +{ + this.dirty = true; + this.clearDirty = true; + this.graphicsData = []; } diff --git a/src/pixi/renderers/CanvasGraphics.js b/src/pixi/renderers/CanvasGraphics.js index 2396467..fa26d3f 100644 --- a/src/pixi/renderers/CanvasGraphics.js +++ b/src/pixi/renderers/CanvasGraphics.js @@ -24,8 +24,8 @@ var data = graphics.graphicsData[i]; var points = data.points; - context.strokeStyle = "#" + data.lineColor.toString(16); - + context.strokeStyle = color = '#' + ('00000' + ( data.lineColor | 0).toString(16)).substr(-6); + context.lineWidth = data.lineWidth; context.globalAlpha = data.lineAlpha; @@ -55,7 +55,8 @@ // TODO - need to be Undefined! if(data.fillColor) { - context.fillStyle = "#" + data.fillColor.toString(16); + context.fillStyle = color = '#' + ('00000' + ( data.fillColor | 0).toString(16)).substr(-6); + context.fillRect(points[0], points[1], points[2], points[3]); } @@ -73,7 +74,7 @@ if(data.fill) { - context.fillStyle = "#" + data.fillColor.toString(16); + context.fillStyle = color = '#' + ('00000' + ( data.fillColor | 0).toString(16)).substr(-6); context.fill(); } if(data.lineWidth) diff --git a/src/pixi/renderers/WebGLGraphics.js b/src/pixi/renderers/WebGLGraphics.js index f1fe1dd..949d283 100644 --- a/src/pixi/renderers/WebGLGraphics.js +++ b/src/pixi/renderers/WebGLGraphics.js @@ -25,7 +25,22 @@ // graphicsObject // a collection of "shapes" (mainly lines right now!) ///this.shape.draw(); - if(!graphics._webGL)PIXI.WebGLGraphics.initGraphics(graphics); + if(!graphics._webGL)graphics._webGL = {points:[], lastPosition:new PIXI.Point(), lastIndex:0, buffer:gl.createBuffer()}; + + if(graphics.dirty) + { + graphics.dirty = false; + + if(graphics.clearDirty) + { + graphics.clearDirty = false; + graphics._webGL.lastIndex = 0; + graphics._webGL.points = []; + + } + + PIXI.WebGLGraphics.initGraphics(graphics); + } gl.uniformMatrix4fv(PIXI.shaderProgram2.mvMatrixUniform, false, PIXI.projectionMatrix ); @@ -36,16 +51,14 @@ //shaderProgram.colorAttribute // ulong idx, long size, ulong type, bool norm, long stride, ulong offset ) - gl.drawArrays(gl.TRIANGLE_STRIP, 0, graphics._webGL.points.length/6); + gl.drawArrays(gl.TRIANGLE_STRIP, 0, graphics._webGL.glPoints.length/6); PIXI.activateDefaultShader(); } PIXI.WebGLGraphics.initGraphics = function(graphics) { - graphics._webGL = {points:[], lastPosition:new PIXI.Point()}; - - for (var i=0; i < graphics.graphicsData.length; i++) + for (var i=graphics._webGL.lastIndex; i < graphics.graphicsData.length; i++) { var data = graphics.graphicsData[i]; @@ -66,15 +79,15 @@ } }; + //console.log(graphics._webGL.lastIndex - graphics.graphicsData.length ) + graphics._webGL.lastIndex = graphics.graphicsData.length; + // convert to points - graphics._webGL.points = new Float32Array(graphics._webGL.points); + graphics._webGL.glPoints = new Float32Array(graphics._webGL.points); var gl = PIXI.gl; - - graphics._webGL.buffer = gl.createBuffer(); - gl.bindBuffer(gl.ARRAY_BUFFER, graphics._webGL.buffer); - gl.bufferData(gl.ARRAY_BUFFER, graphics._webGL.points, gl.STATIC_DRAW); + gl.bufferData(gl.ARRAY_BUFFER, graphics._webGL.glPoints, gl.STATIC_DRAW); } diff --git a/examples/example 13 - Graphics/index.html b/examples/example 13 - Graphics/index.html index f91cd3f..8587309 100644 --- a/examples/example 13 - Graphics/index.html +++ b/examples/example 13 - Graphics/index.html @@ -24,7 +24,9 @@ // create an new instance of a pixi stage - var stage = new PIXI.Stage(0x000000); + var stage = new PIXI.Stage(0x000000, true); + + stage.setInteractive(true); // create a renderer instance //var renderer = new PIXI.CanvasRenderer(800, 600);//PIXI.autoDetectRenderer(800, 600); @@ -96,11 +98,20 @@ stage.addChild(sprite); stage.addChild(graphics); + stage.click = function() + { + graphics.clear(); + } + requestAnimFrame(animate); function animate() { renderer.render(stage); requestAnimFrame( animate ); + graphics.lineStyle(Math.random() * 30, Math.random() * 0xFFFFFF, 0.8); + + graphics.moveTo(Math.random() * 600,Math.random() * 600); + graphics.lineTo(Math.random() * 600,Math.random() * 600); } diff --git a/src/pixi/primitives/Graphics.js b/src/pixi/primitives/Graphics.js index a97762c..1c82d2b 100644 --- a/src/pixi/primitives/Graphics.js +++ b/src/pixi/primitives/Graphics.js @@ -62,6 +62,7 @@ PIXI.Graphics.prototype.lineTo = function(x, y) { this.currentPath.points.push(x, y); + this.dirty = true; } PIXI.Graphics.prototype.beginFill = function(color, alpha) @@ -91,6 +92,7 @@ points:[x, y, width, height], type:PIXI.Graphics.RECT}; this.graphicsData.push(this.currentPath); + this.dirty = true; } PIXI.Graphics.prototype.drawCircle = function( x, y, radius) @@ -100,4 +102,12 @@ points:[x, y, radius], type:PIXI.Graphics.CIRC}; this.graphicsData.push(this.currentPath); + this.dirty = true; +} + +PIXI.Graphics.prototype.clear = function() +{ + this.dirty = true; + this.clearDirty = true; + this.graphicsData = []; } diff --git a/src/pixi/renderers/CanvasGraphics.js b/src/pixi/renderers/CanvasGraphics.js index 2396467..fa26d3f 100644 --- a/src/pixi/renderers/CanvasGraphics.js +++ b/src/pixi/renderers/CanvasGraphics.js @@ -24,8 +24,8 @@ var data = graphics.graphicsData[i]; var points = data.points; - context.strokeStyle = "#" + data.lineColor.toString(16); - + context.strokeStyle = color = '#' + ('00000' + ( data.lineColor | 0).toString(16)).substr(-6); + context.lineWidth = data.lineWidth; context.globalAlpha = data.lineAlpha; @@ -55,7 +55,8 @@ // TODO - need to be Undefined! if(data.fillColor) { - context.fillStyle = "#" + data.fillColor.toString(16); + context.fillStyle = color = '#' + ('00000' + ( data.fillColor | 0).toString(16)).substr(-6); + context.fillRect(points[0], points[1], points[2], points[3]); } @@ -73,7 +74,7 @@ if(data.fill) { - context.fillStyle = "#" + data.fillColor.toString(16); + context.fillStyle = color = '#' + ('00000' + ( data.fillColor | 0).toString(16)).substr(-6); context.fill(); } if(data.lineWidth) diff --git a/src/pixi/renderers/WebGLGraphics.js b/src/pixi/renderers/WebGLGraphics.js index f1fe1dd..949d283 100644 --- a/src/pixi/renderers/WebGLGraphics.js +++ b/src/pixi/renderers/WebGLGraphics.js @@ -25,7 +25,22 @@ // graphicsObject // a collection of "shapes" (mainly lines right now!) ///this.shape.draw(); - if(!graphics._webGL)PIXI.WebGLGraphics.initGraphics(graphics); + if(!graphics._webGL)graphics._webGL = {points:[], lastPosition:new PIXI.Point(), lastIndex:0, buffer:gl.createBuffer()}; + + if(graphics.dirty) + { + graphics.dirty = false; + + if(graphics.clearDirty) + { + graphics.clearDirty = false; + graphics._webGL.lastIndex = 0; + graphics._webGL.points = []; + + } + + PIXI.WebGLGraphics.initGraphics(graphics); + } gl.uniformMatrix4fv(PIXI.shaderProgram2.mvMatrixUniform, false, PIXI.projectionMatrix ); @@ -36,16 +51,14 @@ //shaderProgram.colorAttribute // ulong idx, long size, ulong type, bool norm, long stride, ulong offset ) - gl.drawArrays(gl.TRIANGLE_STRIP, 0, graphics._webGL.points.length/6); + gl.drawArrays(gl.TRIANGLE_STRIP, 0, graphics._webGL.glPoints.length/6); PIXI.activateDefaultShader(); } PIXI.WebGLGraphics.initGraphics = function(graphics) { - graphics._webGL = {points:[], lastPosition:new PIXI.Point()}; - - for (var i=0; i < graphics.graphicsData.length; i++) + for (var i=graphics._webGL.lastIndex; i < graphics.graphicsData.length; i++) { var data = graphics.graphicsData[i]; @@ -66,15 +79,15 @@ } }; + //console.log(graphics._webGL.lastIndex - graphics.graphicsData.length ) + graphics._webGL.lastIndex = graphics.graphicsData.length; + // convert to points - graphics._webGL.points = new Float32Array(graphics._webGL.points); + graphics._webGL.glPoints = new Float32Array(graphics._webGL.points); var gl = PIXI.gl; - - graphics._webGL.buffer = gl.createBuffer(); - gl.bindBuffer(gl.ARRAY_BUFFER, graphics._webGL.buffer); - gl.bufferData(gl.ARRAY_BUFFER, graphics._webGL.points, gl.STATIC_DRAW); + gl.bufferData(gl.ARRAY_BUFFER, graphics._webGL.glPoints, gl.STATIC_DRAW); } diff --git a/src/pixi/renderers/WebGLRenderGroup.js b/src/pixi/renderers/WebGLRenderGroup.js index 1d36d4d..a193406 100644 --- a/src/pixi/renderers/WebGLRenderGroup.js +++ b/src/pixi/renderers/WebGLRenderGroup.js @@ -190,7 +190,10 @@ { if(startBatch.visible) startBatch.renderWebGL(this, projectionMatrix); } - + else if(renderable instanceof PIXI.Graphics) + { + if(renderable.visible) PIXI.WebGLGraphics.renderGraphics(renderable);//, projectionMatrix); + } return; } @@ -215,7 +218,10 @@ { if(startBatch.visible) startBatch.renderWebGL(this, projectionMatrix); } - + else if(renderable instanceof PIXI.Graphics) + { + if(renderable.visible) PIXI.WebGLGraphics.renderGraphics(renderable);//, projectionMatrix); + } // DO the middle batchs.. for (var i=startBatchIndex+1; i < endBatchIndex; i++) { @@ -237,7 +243,10 @@ { if(renderable.visible) renderable.renderWebGL(this, projectionMatrix); } - + else if(renderable instanceof PIXI.Graphics) + { + if(renderable.visible) PIXI.WebGLGraphics.renderGraphics(renderable);//, projectionMatrix); + } } // DO the last batch.. @@ -257,6 +266,10 @@ { if(endBatch.visible) endBatch.renderWebGL(this, projectionMatrix); } + else if(renderable instanceof PIXI.Graphics) + { + if(renderable.visible) PIXI.WebGLGraphics.renderGraphics(renderable);//, projectionMatrix); + } } PIXI.WebGLRenderGroup.prototype.checkVisibility = function(displayObject, globalVisible)