From b6288e1ffe4ffe29a595bb1e146feb388503e2c4 Mon Sep 17 00:00:00 2001 From: Nivesh Rajbhandari Date: Wed, 7 Mar 2012 15:31:20 -0800 Subject: gradient support for canvas-2d shapes. Signed-off-by: Nivesh Rajbhandari --- js/controllers/elements/shapes-controller.js | 23 +++++++---- js/lib/geom/circle.js | 61 ++++++++++++++++++++++++---- js/lib/geom/line.js | 38 +++++++++++++++-- js/lib/geom/rectangle.js | 4 +- 4 files changed, 106 insertions(+), 20 deletions(-) diff --git a/js/controllers/elements/shapes-controller.js b/js/controllers/elements/shapes-controller.js index b6d97b14..d72d9c14 100755 --- a/js/controllers/elements/shapes-controller.js +++ b/js/controllers/elements/shapes-controller.js @@ -261,28 +261,37 @@ exports.ShapesController = Montage.create(CanvasController, { _setGradientMaterial: { value: function(el, gradientMode, isFill) { - var m = "LinearGradientMaterial"; + var m = "LinearGradientMaterial", + fm; if(gradientMode === "radial") { m = "RadialGradientMaterial"; } - if(el.elementModel.shapeModel.fillMaterial.getName() !== m) + if(isFill) { - var fm = Object.create(MaterialsModel.getMaterial(m)); - if(fm) + if(el.elementModel.shapeModel.fillMaterial.getName() !== m) { - if(isFill) + fm = Object.create(MaterialsModel.getMaterial(m)); + if(fm) { el.elementModel.shapeModel.GLGeomObj.setFillMaterial(fm); el.elementModel.shapeModel.fillMaterial = fm; + el.elementModel.shapeModel.GLGeomObj.buildBuffers(); } - else + } + } + else + { + if(el.elementModel.shapeModel.strokeMaterial.getName() !== m) + { + fm = Object.create(MaterialsModel.getMaterial(m)); + if(fm) { el.elementModel.shapeModel.GLGeomObj.setStrokeMaterial(fm); el.elementModel.shapeModel.strokeMaterial = fm; + el.elementModel.shapeModel.GLGeomObj.buildBuffers(); } - el.elementModel.shapeModel.GLGeomObj.buildBuffers(); } } } diff --git a/js/lib/geom/circle.js b/js/lib/geom/circle.js index dd82a4cc..70f608be 100755 --- a/js/lib/geom/circle.js +++ b/js/lib/geom/circle.js @@ -442,16 +442,43 @@ var Circle = function GLCircle() { var bezPts = MathUtils.circularArcToBezier( [0,0,0], [1,0,0], 2.0*Math.PI ); if (bezPts) { var n = bezPts.length; + var gradient, + colors, + len, + j, + position, + cs, + c; // set up the fill style ctx.beginPath(); ctx.lineWidth = 0; if (this._fillColor) { - var c = "rgba(" + 255*this._fillColor[0] + "," + 255*this._fillColor[1] + "," + 255*this._fillColor[2] + "," + this._fillColor[3] + ")"; - ctx.fillStyle = c; - + if(this._fillColor.gradientMode) { + if(this._fillColor.gradientMode === "radial") { + gradient = ctx.createRadialGradient(xCtr, yCtr, 0, + xCtr, yCtr, yScale); + } else { + gradient = ctx.createLinearGradient(0, this._height/2, this._width, this._height/2); + } + colors = this._fillColor.color; + + len = colors.length; + + for(j=0; j