aboutsummaryrefslogtreecommitdiff
path: root/assets
diff options
context:
space:
mode:
authorPushkar Joshi2012-06-06 15:01:10 -0700
committerPushkar Joshi2012-06-06 15:01:10 -0700
commit2dca8a0aa69981bc2a81c4a68f9061aef861f0ea (patch)
tree8ebc5a87d3b90569f4ac2688ac2a6d8c3c8713d0 /assets
parentcf3087b78d771ef89b0d5557430fd594f71cf063 (diff)
downloadninja-2dca8a0aa69981bc2a81c4a68f9061aef861f0ea.tar.gz
enable gradients for brush stroke (in authoring as well as runtime)
Diffstat (limited to 'assets')
-rw-r--r--assets/canvas-runtime.js62
1 files changed, 58 insertions, 4 deletions
diff --git a/assets/canvas-runtime.js b/assets/canvas-runtime.js
index 524cdfeb..76332bcb 100644
--- a/assets/canvas-runtime.js
+++ b/assets/canvas-runtime.js
@@ -2137,15 +2137,59 @@ NinjaCvsRt.RuntimeBrushStroke = Object.create(NinjaCvsRt.RuntimeGeomObj, {
2137 } 2137 }
2138 }, 2138 },
2139 2139
2140 //buildColor returns the fillStyle or strokeStyle for the Canvas 2D context
2141 buildColor: {
2142 value: function(ctx, //the 2D rendering context (for creating gradients if necessary)
2143 ipColor, //color string, also encodes whether there's a gradient and of what type
2144 w, //width of the region of color
2145 h, //height of the region of color
2146 lw) //linewidth (i.e. stroke width/size)
2147 {
2148 if (ipColor.gradientMode){
2149 var position, gradient, cs, inset; //vars used in gradient calculations
2150 inset = Math.ceil( lw ) - 0.5;
2151
2152 if(ipColor.gradientMode === "radial") {
2153 var ww = w - 2*lw, hh = h - 2*lw;
2154 gradient = ctx.createRadialGradient(w/2, h/2, 0, w/2, h/2, Math.max(ww, hh)/2);
2155 } else {
2156 gradient = ctx.createLinearGradient(inset, h/2, w-inset, h/2);
2157 }
2158 var colors = ipColor.color;
2159
2160 var len = colors.length;
2161 for(n=0; n<len; n++) {
2162 position = colors[n].position/100;
2163 cs = colors[n].value;
2164 gradient.addColorStop(position, "rgba(" + cs.r + "," + cs.g + "," + cs.b + "," + cs.a + ")");
2165 }
2166 return gradient;
2167 } else {
2168 var c = "rgba(" + 255*ipColor[0] + "," + 255*ipColor[1] + "," + 255*ipColor[2] + "," + ipColor[3] + ")";
2169 return c;
2170 }
2171 }
2172 },
2173
2140 render: { 2174 render: {
2141 value: function() { 2175 value: function() {
2176 //vars for gradient code
2177 var w,h;
2178
2142 // get the world 2179 // get the world
2143 var world = this.getWorld(); 2180 var world = this.getWorld();
2144 if (!world) { 2181 if (!world) {
2145 throw( "null world in brush stroke render" ); 2182 throw( "null world in brush stroke render" );
2146 return; 2183 return;
2147 } 2184 } else {
2148 2185
2186 if (this._strokeColor.gradientMode){
2187 useBuildColor = true;
2188 }
2189 //vars used for the gradient computation in buildColor
2190 w = world.getViewportWidth();
2191 h = world.getViewportHeight();
2192 }
2149 // get the context 2193 // get the context
2150 var ctx = world.get2DContext(); 2194 var ctx = world.get2DContext();
2151 if (!ctx) { 2195 if (!ctx) {
@@ -2169,7 +2213,7 @@ NinjaCvsRt.RuntimeBrushStroke = Object.create(NinjaCvsRt.RuntimeGeomObj, {
2169 2213
2170 //build an angled (calligraphic) brush stamp 2214 //build an angled (calligraphic) brush stamp
2171 var deltaDisplacement = [Math.cos(this._strokeAngle),Math.sin(this._strokeAngle)]; 2215 var deltaDisplacement = [Math.cos(this._strokeAngle),Math.sin(this._strokeAngle)];
2172 deltaDisplacement = VecUtils.vecNormalize(2, deltaDisplacement, 1); 2216 deltaDisplacement = this.vecNormalize(2, deltaDisplacement, 1);
2173 var startPos = [-halfNumTraces*deltaDisplacement[0],-halfNumTraces*deltaDisplacement[1]]; 2217 var startPos = [-halfNumTraces*deltaDisplacement[0],-halfNumTraces*deltaDisplacement[1]];
2174 2218
2175 var brushStamp = []; 2219 var brushStamp = [];
@@ -2198,7 +2242,11 @@ NinjaCvsRt.RuntimeBrushStroke = Object.create(NinjaCvsRt.RuntimeGeomObj, {
2198 } else { 2242 } else {
2199 ctx.lineWidth=2; 2243 ctx.lineWidth=2;
2200 } 2244 }
2201 ctx.strokeStyle="rgba("+parseInt(255*this._strokeColor[0])+","+parseInt(255*this._strokeColor[1])+","+parseInt(255*this._strokeColor[2])+","+alphaVal+")"; 2245 if (!useBuildColor){
2246 ctx.strokeStyle="rgba("+parseInt(255*this._strokeColor[0])+","+parseInt(255*this._strokeColor[1])+","+parseInt(255*this._strokeColor[2])+","+alphaVal+")";
2247 } else {
2248 ctx.strokeStyle = this.buildColor(ctx, this._strokeColor, w, h, this._strokeWidth, alphaVal);
2249 }
2202 ctx.translate(disp[0],disp[1]); 2250 ctx.translate(disp[0],disp[1]);
2203 ctx.beginPath(); 2251 ctx.beginPath();
2204 p = points[0]; 2252 p = points[0];
@@ -2218,7 +2266,13 @@ NinjaCvsRt.RuntimeBrushStroke = Object.create(NinjaCvsRt.RuntimeGeomObj, {
2218 var minStrokeWidth = (this._strokeHardness*this._strokeWidth)/100; //the hardness is the percentage of the stroke width that's fully opaque 2266 var minStrokeWidth = (this._strokeHardness*this._strokeWidth)/100; //the hardness is the percentage of the stroke width that's fully opaque
2219 var numlayers = 1 + Math.ceil((this._strokeWidth-minStrokeWidth)*0.5); 2267 var numlayers = 1 + Math.ceil((this._strokeWidth-minStrokeWidth)*0.5);
2220 var alphaVal = 1.0/(numlayers); //this way the alpha at the first path will be 1 2268 var alphaVal = 1.0/(numlayers); //this way the alpha at the first path will be 1
2221 ctx.strokeStyle="rgba("+parseInt(255*this._strokeColor[0])+","+parseInt(255*this._strokeColor[1])+","+parseInt(255*this._strokeColor[2])+","+alphaVal+")"; 2269
2270 if (!useBuildColor){
2271 ctx.strokeStyle="rgba("+parseInt(255*this._strokeColor[0])+","+parseInt(255*this._strokeColor[1])+","+parseInt(255*this._strokeColor[2])+","+alphaVal+")";
2272 } else {
2273 ctx.strokeStyle = this.buildColor(ctx, this._strokeColor, w,h, this._strokeWidth, alphaVal);
2274 }
2275
2222 for (var l=0;l<numlayers;l++){ 2276 for (var l=0;l<numlayers;l++){
2223 ctx.beginPath(); 2277 ctx.beginPath();
2224 p = points[0]; 2278 p = points[0];