From a0f939feceaf3d23a6cd08ed5d7f6b236a5a9bde Mon Sep 17 00:00:00 2001 From: Nivesh Rajbhandari Date: Thu, 5 Apr 2012 12:53:17 -0700 Subject: Updated canvas-runtime.js to support gradients for rectangle and oval in canvas2d shape mode. Also fixed some typos. Signed-off-by: Nivesh Rajbhandari --- assets/canvas-runtime.js | 377 +++++++++++++++++++++++++++++------------------ 1 file changed, 231 insertions(+), 146 deletions(-) (limited to 'assets') diff --git a/assets/canvas-runtime.js b/assets/canvas-runtime.js index 32829351..ee9f24a4 100644 --- a/assets/canvas-runtime.js +++ b/assets/canvas-runtime.js @@ -288,23 +288,23 @@ NinjaCvsRt.GLRuntime = function ( canvas, jObj, assetPath ) this.importObject = function( jObj, parent ) { - var type = jObj.type + var type = jObj.type; var obj; switch (type) { case 1: obj = new NinjaCvsRt.RuntimeRectangle(); - obj.importJSON( jObj, parent ); + obj.importJSON( jObj ); break; case 2: // circle obj = new NinjaCvsRt.RuntimeOval(); - obj.importJSON( jObj, parent ); + obj.importJSON( jObj ); break; case 3: // line obj = new NinjaCvsRt.RuntimeLine(); - obj.importJSON( jObj, parent ); + obj.importJSON( jObj ); break; default: @@ -513,7 +513,7 @@ NinjaCvsRt.RuntimeGeomObj = function () case "pulse": mat = new NinjaCvsRt.RuntimePulseMaterial(); break; default: - console.log( "material type: " + materialType + " is not supported" ); + console.log( "material type: " + shaderName + " is not supported" ); break; } @@ -741,7 +741,7 @@ NinjaCvsRt.RuntimeRectangle = function () ctx.quadraticCurveTo( width-inset, inset, width-inset-rad, inset ); // do the top of the rectangle - pt = [inset, inset] + pt = [inset, inset]; rad = tlRad - inset; if (rad < 0) rad = 0; pt[0] += rad; @@ -770,34 +770,81 @@ NinjaCvsRt.RuntimeRectangle = function () var w = world.getViewportWidth(), h = world.getViewportHeight(); - // render the fill - ctx.beginPath(); - var c = null, inset = 0; - if (this._fillColor) - { - c = "rgba(" + 255*this._fillColor[0] + "," + 255*this._fillColor[1] + "," + 255*this._fillColor[2] + "," + this._fillColor[3] + ")"; - ctx.fillStyle = c; - - ctx.lineWidth = lw; - inset = Math.ceil( lw ) + 0.5; - this.renderPath( inset, ctx ); - ctx.fill(); - ctx.closePath(); - } + var c, + inset, + gradient, + colors, + len, + n, + position, + cs; + // render the fill + ctx.beginPath(); + if (this._fillColor) { + inset = Math.ceil( lw ) + 0.5; + + if(this._fillColor.gradientMode) { + if(this._fillColor.gradientMode === "radial") { + gradient = ctx.createRadialGradient(w/2, h/2, 0, w/2, h/2, Math.max(w/2, h/2)-inset); + } else { + gradient = ctx.createLinearGradient(inset, h/2, w-2*inset, h/2); + } + colors = this._fillColor.color; - // render the stroke - ctx.beginPath(); - if (this._strokeColor) - { - c = "rgba(" + 255*this._strokeColor[0] + "," + 255*this._strokeColor[1] + "," + 255*this._strokeColor[2] + "," + this._strokeColor[3] + ")"; - ctx.strokeStyle = c; + len = colors.length; - ctx.lineWidth = lw; - inset = Math.ceil( 0.5*lw ) + 0.5; - this.renderPath( inset, ctx ); - ctx.stroke(); - ctx.closePath(); - } + for(n=0; n 0.001) - { - xScale = 0.5*innerRad*this._width; - yScale = 0.5*innerRad*this._height; - mat[0] = xScale; - mat[5] = yScale; - - // get the bezier points - var bezPtsInside = this.circularArcToBezier( [0,0,0], [1,0,0], -2.0*Math.PI ); - if (bezPtsInside) - { - n = bezPtsInside.length; - p = this.transformPoint( bezPtsInside[0], mat ); - ctx.moveTo( p[0], p[1] ); - index = 1; - while (index < n) - { - p0 = this.transformPoint( bezPtsInside[index], mat ); - p1 = this.transformPoint( bezPtsInside[index+1], mat ); + } else { + c = "rgba(" + 255*this._fillColor[0] + "," + 255*this._fillColor[1] + "," + 255*this._fillColor[2] + "," + this._fillColor[3] + ")"; + ctx.fillStyle = c; + } + // draw the fill +// ctx.beginPath(); + var p = this.transformPoint( bezPts[0], mat ); + ctx.moveTo( p[0], p[1] ); + var index = 1; + while (index < n) { + p0 = this.transformPoint( bezPts[index], mat ); + p1 = this.transformPoint( bezPts[index+1], mat ); + + x0 = p0[0]; y0 = p0[1]; + x1 = p1[0]; y1 = p1[1]; + ctx.quadraticCurveTo( x0, y0, x1, y1 ); + index += 2; + } - x0 = p0[0]; + if (innerRad > 0.001) { + xScale = 0.5*innerRad*this._width; + yScale = 0.5*innerRad*this._height; + mat[0] = xScale; + mat[5] = yScale; + + // get the bezier points + var bezPtsInside = this.circularArcToBezier( [0,0,0], [1,0,0], -2.0*Math.PI ); + if (bezPtsInside) { + n = bezPtsInside.length; + p = this.transformPoint( bezPtsInside[0], mat ); + ctx.moveTo( p[0], p[1] ); + index = 1; + while (index < n) { + p0 = this.transformPoint( bezPtsInside[index], mat ); + p1 = this.transformPoint( bezPtsInside[index+1], mat ); + + x0 = p0[0]; y0 = p0[1]; - x1 = p1[0]; + x1 = p1[0]; y1 = p1[1]; - ctx.quadraticCurveTo( x0, y0, x1, y1 ); - index += 2; - } - } - } + ctx.quadraticCurveTo( x0, y0, x1, y1 ); + index += 2; + } + } + } - // fill the path - ctx.fill(); - } + // fill the path + ctx.fill(); + } - // calculate the stroke matrix - xScale = 0.5*this._width - 0.5*lineWidth; - yScale = 0.5*this._height - 0.5*lineWidth; - mat[0] = xScale; - mat[5] = yScale; + // calculate the stroke matrix + xScale = 0.5*this._width - 0.5*lineWidth; + yScale = 0.5*this._height - 0.5*lineWidth; + mat[0] = xScale; + mat[5] = yScale; + + // set up the stroke style + ctx.beginPath(); + ctx.lineWidth = lineWidth; + if (this._strokeColor) { + if(this._strokeColor.gradientMode) { + if(this._strokeColor.gradientMode === "radial") { + gradient = ctx.createRadialGradient(xCtr, yCtr, Math.min(xScale, yScale), + xCtr, yCtr, 0.5*Math.max(this._height, this._width)); + } else { + gradient = ctx.createLinearGradient(0, this._height/2, this._width, this._height/2); + } + colors = this._strokeColor.color; + + len = colors.length; + + for(j=0; j 0.01) - { - // calculate the stroke matrix - xScale = 0.5*innerRad*this._width - 0.5*lineWidth; - yScale = 0.5*innerRad*this._height - 0.5*lineWidth; - mat[0] = xScale; - mat[5] = yScale; - - // draw the stroke - p = this.transformPoint( bezPts[0], mat ); - ctx.moveTo( p[0], p[1] ); - index = 1; - while (index < n) - { - p0 = this.transformPoint( bezPts[index], mat ); - p1 = this.transformPoint( bezPts[index+1], mat ); + ctx.quadraticCurveTo( x0, y0, x1, y1 ); + index += 2; + } - x0 = p0[0]; + if (innerRad > 0.001) { + // calculate the stroke matrix + xScale = 0.5*innerRad*this._width - 0.5*lineWidth; + yScale = 0.5*innerRad*this._height - 0.5*lineWidth; + mat[0] = xScale; + mat[5] = yScale; + + // draw the stroke + p = this.transformPoint( bezPts[0], mat ); + ctx.moveTo( p[0], p[1] ); + index = 1; + while (index < n) { + p0 = this.transformPoint( bezPts[index], mat ); + p1 = this.transformPoint( bezPts[index+1], mat ); + + x0 = p0[0]; y0 = p0[1]; - x1 = p1[0]; + x1 = p1[0]; y1 = p1[1]; - ctx.quadraticCurveTo( x0, y0, x1, y1 ); - index += 2; - } - } + ctx.quadraticCurveTo( x0, y0, x1, y1 ); + index += 2; + } + } - // render the stroke - ctx.stroke(); - } + // render the stroke + ctx.stroke(); + } } }; @@ -1321,13 +1405,13 @@ NinjaCvsRt.RuntimeRadialGradientMaterial = function () this.importJSON = function( jObj ) { - this._color1 = jObj.color1, - this._color2 = jObj.color2, - this._color3 = jObj.color3, - this._color4 = jObj.color4, - this._colorStop1 = jObj.colorStop1, - this._colorStop2 = jObj.colorStop2, - this._colorStop3 = jObj.colorStop3, + this._color1 = jObj.color1; + this._color2 = jObj.color2; + this._color3 = jObj.color3; + this._color4 = jObj.color4; + this._colorStop1 = jObj.colorStop1; + this._colorStop2 = jObj.colorStop2; + this._colorStop3 = jObj.colorStop3; this._colorStop4 = jObj.colorStop4; if (this._angle !== undefined) @@ -1443,6 +1527,7 @@ NinjaCvsRt.RuntimeUberMaterial = function () } else if(light.type == 'spot') { + var deg2Rad = Math.PI / 180; technique['u_light'+i+'Atten'].set(light.attenuation || [ 1,0,0 ]); technique['u_light'+i+'Pos'].set(light.position || [ 0, 0, 0 ]); technique['u_light'+i+'Spot'].set([ Math.cos( ( light.spotInnerCutoff || 45.0 ) * deg2Rad ), -- cgit v1.2.3