aboutsummaryrefslogtreecommitdiff
path: root/js
diff options
context:
space:
mode:
Diffstat (limited to 'js')
-rwxr-xr-xjs/helper-classes/RDGE/GLBrushStroke.js108
-rw-r--r--[-rwxr-xr-x]js/tools/BrushTool.js176
2 files changed, 195 insertions, 89 deletions
diff --git a/js/helper-classes/RDGE/GLBrushStroke.js b/js/helper-classes/RDGE/GLBrushStroke.js
index fdf1595c..8fb6ab25 100755
--- a/js/helper-classes/RDGE/GLBrushStroke.js
+++ b/js/helper-classes/RDGE/GLBrushStroke.js
@@ -67,7 +67,23 @@ function GLBrushStroke() {
67 67
68 this.getNumPoints = function () { return this._Points.length; } 68 this.getNumPoints = function () { return this._Points.length; }
69 this.getPoint = function (index) { return this._Points[index]; } 69 this.getPoint = function (index) { return this._Points[index]; }
70 this.addPoint = function (anchorPt) { this._Points.push(anchorPt); this._dirty=true; } 70 this.addPoint = function (pt)
71 {
72 //add the point if it is some epsilon away from the previous point
73 var doAddPoint=true;
74 var numPoints = this._Points.length;
75 if (numPoints>0) {
76 var prevPt = this._Points[numPoints-1];
77 var diffPt = [prevPt[0]-pt[0], prevPt[1]-pt[1]];
78 var diffPtMag = diffPt[0]*diffPt[0] + diffPt[1]*diffPt[1];
79 if (diffPtMag<16)//TODO hook this up to the variable that measures flow/wetness of the paint brush...a smaller number-> more samples
80 doAddPoint=false;
81 }
82 if (doAddPoint) {
83 this._Points.push(pt);
84 this._dirty=true;
85 }
86 }
71 this.insertPoint = function(pt, index){ this._Points.splice(index, 0, pt); this._dirty=true;} 87 this.insertPoint = function(pt, index){ this._Points.splice(index, 0, pt); this._dirty=true;}
72 this.isDirty = function(){return this._dirty;} 88 this.isDirty = function(){return this._dirty;}
73 this.makeDirty = function(){this._dirty=true;} 89 this.makeDirty = function(){this._dirty=true;}
@@ -167,7 +183,8 @@ function GLBrushStroke() {
167 var bboxWidth = bboxMax[0] - bboxMin[0]; 183 var bboxWidth = bboxMax[0] - bboxMin[0];
168 var bboxHeight = bboxMax[1] - bboxMin[1]; 184 var bboxHeight = bboxMax[1] - bboxMin[1];
169 ctx.clearRect(0, 0, bboxWidth, bboxHeight); 185 ctx.clearRect(0, 0, bboxWidth, bboxHeight);
170/* 186
187 /*
171 ctx.lineWidth = this._strokeWidth; 188 ctx.lineWidth = this._strokeWidth;
172 ctx.strokeStyle = "black"; 189 ctx.strokeStyle = "black";
173 if (this._strokeColor) 190 if (this._strokeColor)
@@ -185,7 +202,91 @@ function GLBrushStroke() {
185 ctx.lineTo(pt[0]-bboxMin[0], pt[1]-bboxMin[1]); 202 ctx.lineTo(pt[0]-bboxMin[0], pt[1]-bboxMin[1]);
186 } 203 }
187 ctx.stroke(); 204 ctx.stroke();
188 */ 205 */
206
207 var isDebug = false;
208 var prevPt = this._Points[0];
209 var prevX = prevPt[0]-bboxMin[0];
210 var prevY = prevPt[1]-bboxMin[1];
211 prevPt = [prevX,prevY];
212 for (var i = 1; i < numPoints; i++) {
213 var pt = this._Points[i];
214 ctx.globalCompositeOperation = 'source-over';
215 var x = pt[0]-bboxMin[0];
216 var y = pt[1]-bboxMin[1];
217 pt = [x,y];
218
219 //vector from prev to current pt
220 var seg = VecUtils.vecSubtract(2, pt, prevPt);
221 var segDir = VecUtils.vecNormalize(2, seg, 1.0);
222
223 var segMidPt = VecUtils.vecInterpolate(2, pt, prevPt, 0.5);
224 var w2 = this._strokeWidth*0.5;
225 var segDirOrtho = [w2*segDir[1], -w2*segDir[0]];
226
227 //add half the strokewidth to the segMidPt
228 var lgStart = VecUtils.vecAdd(2, segMidPt, segDirOrtho);
229 var lgEnd = VecUtils.vecSubtract(2, segMidPt, segDirOrtho);
230
231 ctx.save();
232 ctx.beginPath();
233
234 if (isDebug) {
235 ctx.strokeStyle="black";
236 ctx.lineWidth = 1;
237
238 ctx.moveTo(lgStart[0], lgStart[1]);
239 ctx.lineTo(lgEnd[0], lgEnd[1]);
240 ctx.stroke();
241 }
242
243 var lg = ctx.createLinearGradient(lgStart[0], lgStart[1], lgEnd[0], lgEnd[1]);
244 lg.addColorStop(1, 'rgba(0,0,0,0.0)');
245 lg.addColorStop(0.5,'rgba(255,0,0,1.0)');
246 lg.addColorStop(0, 'rgba(0,0,0,0.0)');
247 ctx.fillStyle = lg;
248
249 if (isDebug){
250 ctx.strokeStyle="blue";
251 ctx.lineWidth=0.5;
252 }
253 ctx.moveTo(prevX-w2, prevY);
254 ctx.lineTo(prevX+w2, prevY);
255 ctx.lineTo(x+w2, y);
256 ctx.lineTo(x-w2, y);
257 ctx.lineTo(prevX-w2, prevY);
258 ctx.fill();
259 ctx.closePath();
260
261 ctx.restore();
262
263 prevPt = pt;
264 prevX = x;
265 prevY = y;
266 }
267
268
269 if (isDebug)
270 ctx.stroke();
271
272 if (isDebug){
273 //draw the skeleton of this stroke
274 ctx.lineWidth = 1;
275 ctx.strokeStyle = "black";
276 var pt = this._Points[0];
277 ctx.beginPath();
278 ctx.moveTo(pt[0]-bboxMin[0],pt[1]-bboxMin[1]);
279 for (var i = 1; i < numPoints; i++) {
280 pt = this._Points[i];
281 var x = pt[0]-bboxMin[0];
282 var y = pt[1]-bboxMin[1];
283 ctx.lineTo(x,y);
284 }
285 ctx.stroke();
286 }
287
288
289 /*
189 var R2 = this._strokeWidth; 290 var R2 = this._strokeWidth;
190 var R = R2*0.5; 291 var R = R2*0.5;
191 var hardness = 0.25; //for a pencil, this is always 1 //TODO get hardness parameter from user interface 292 var hardness = 0.25; //for a pencil, this is always 1 //TODO get hardness parameter from user interface
@@ -209,6 +310,7 @@ function GLBrushStroke() {
209 //ctx.globalCompositeOperation = 'source-in'; 310 //ctx.globalCompositeOperation = 'source-in';
210 //ctx.rect(x-R, y-R, R2, R2); 311 //ctx.rect(x-R, y-R, R2, R2);
211 } 312 }
313 */
212 ctx.restore(); 314 ctx.restore();
213 } //render() 315 } //render()
214 316
diff --git a/js/tools/BrushTool.js b/js/tools/BrushTool.js
index 4d44326f..9a0ad583 100755..100644
--- a/js/tools/BrushTool.js
+++ b/js/tools/BrushTool.js
@@ -65,12 +65,30 @@ exports.BrushTool = Montage.create(ShapeTool, {
65 if (this._selectedBrushStroke === null){ 65 if (this._selectedBrushStroke === null){
66 this._selectedBrushStroke = new GLBrushStroke(); 66 this._selectedBrushStroke = new GLBrushStroke();
67 } 67 }
68 console.log("BrushTool Start");
69 NJevent("enableStageMove");//stageManagerModule.stageManager.enableMouseMove(); 68 NJevent("enableStageMove");//stageManagerModule.stageManager.enableMouseMove();
70 } //value: function (event) { 69 } //value: function (event) {
71 }, //HandleLeftButtonDown 70 }, //HandleLeftButtonDown
72 71
72 _getUnsnappedPosition: {
73 value: function(x,y){
74 var elemSnap = snapManager.elementSnapEnabled();
75 var gridSnap = snapManager.gridSnapEnabled();
76 var alignSnap = snapManager.snapAlignEnabled();
73 77
78 snapManager.enableElementSnap(false);
79 snapManager.enableGridSnap(false);
80 snapManager.enableSnapAlign(false);
81
82 var point = webkitConvertPointFromPageToNode(this.application.ninja.stage.canvas, new WebKitPoint(x,y));
83 var unsnappedpos = DrawingToolBase.getHitRecPos(snapManager.snap(point.x, point.y, false));
84
85 snapManager.enableElementSnap(elemSnap);
86 snapManager.enableGridSnap(gridSnap);
87 snapManager.enableSnapAlign(alignSnap);
88
89 return unsnappedpos;
90 }
91 },
74 //need to override this function because the ShapeTool's definition contains a clearDrawingCanvas call - Pushkar 92 //need to override this function because the ShapeTool's definition contains a clearDrawingCanvas call - Pushkar
75 // might not need to override once we draw using OpenGL instead of SVG 93 // might not need to override once we draw using OpenGL instead of SVG
76 // Also took out all the snapping code for now...need to add that back 94 // Also took out all the snapping code for now...need to add that back
@@ -84,20 +102,10 @@ exports.BrushTool = Montage.create(ShapeTool, {
84 } 102 }
85 103
86 if (this._isDrawing) { 104 if (this._isDrawing) {
87 snapManager.enableElementSnap(false); 105 var currMousePos = this._getUnsnappedPosition(event.pageX, event.pageY);
88 snapManager.enableGridSnap(false);
89 snapManager.enableSnapAlign(false);
90 //this.doDraw(event);
91 //var currMousePos = this.getMouseUpPos();
92 //instead of doDraw call own DrawingTool
93 var point = webkitConvertPointFromPageToNode(this.application.ninja.stage.canvas, new WebKitPoint(event.pageX, event.pageY));
94 var hitRecSnapPoint = DrawingToolBase.getUpdatedSnapPointNoAppLevelEnabling(point.x, point.y, true, this.mouseDownHitRec);
95 var currMousePos = DrawingToolBase.getHitRecPos(hitRecSnapPoint);
96
97 if (this._selectedBrushStroke){ 106 if (this._selectedBrushStroke){
98 this._selectedBrushStroke.addPoint(currMousePos); 107 this._selectedBrushStroke.addPoint(currMousePos);
99 } 108 }
100
101 this.ShowCurrentBrushStrokeOnStage(); 109 this.ShowCurrentBrushStrokeOnStage();
102 } //if (this._isDrawing) { 110 } //if (this._isDrawing) {
103 111
@@ -123,7 +131,6 @@ exports.BrushTool = Montage.create(ShapeTool, {
123 131
124 this._isDrawing = false; 132 this._isDrawing = false;
125 this._hasDraw = false; 133 this._hasDraw = false;
126 console.log("BrushTool Stop");
127 134
128 //TODO get these values from the options 135 //TODO get these values from the options
129 if (this._selectedBrushStroke){ 136 if (this._selectedBrushStroke){
@@ -192,92 +199,89 @@ exports.BrushTool = Montage.create(ShapeTool, {
192 199
193 200
194 RenderShape: { 201 RenderShape: {
195 value: function (w, h, planeMat, midPt, canvas) { 202 value: function (w, h, planeMat, midPt, canvas) {
196 if ((Math.floor(w) === 0) || (Math.floor(h) === 0)) {