aboutsummaryrefslogtreecommitdiff
path: root/js
diff options
context:
space:
mode:
Diffstat (limited to 'js')
-rw-r--r--js/helper-classes/RDGE/GLBrushStroke.js54
-rw-r--r--js/tools/BrushTool.js170
2 files changed, 138 insertions, 86 deletions
diff --git a/js/helper-classes/RDGE/GLBrushStroke.js b/js/helper-classes/RDGE/GLBrushStroke.js
index fdf1595c..59017a0c 100644
--- 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<4)
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,9 @@ 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
188 /*
171 ctx.lineWidth = this._strokeWidth; 189 ctx.lineWidth = this._strokeWidth;
172 ctx.strokeStyle = "black"; 190 ctx.strokeStyle = "black";
173 if (this._strokeColor) 191 if (this._strokeColor)
@@ -185,7 +203,35 @@ function GLBrushStroke() {
185 ctx.lineTo(pt[0]-bboxMin[0], pt[1]-bboxMin[1]); 203 ctx.lineTo(pt[0]-bboxMin[0], pt[1]-bboxMin[1]);
186 } 204 }
187 ctx.stroke(); 205 ctx.stroke();
188 */ 206 */
207
208 var firstPt = this._Points[0];
209 var prevX = firstPt[0]-bboxMin[0];
210 var prevY = firstPt[1]-bboxMin[1];
211 for (var i = 1; i < numPoints; i++) {
212 var pt = this._Points[i];
213 ctx.globalCompositeOperation = 'source-over';
214 var x = pt[0]-bboxMin[0];
215 var y = pt[1]-bboxMin[1];
216 var r = ctx.createLinearGradient(prevX, prevY, x, y);
217 r.addColorStop(1, 'rgba(0,0,0,0.01)');
218 r.addColorStop(0.5,'rgba(255,0,0,1.0)');
219 r.addColorStop(0, 'rgba(0,0,0,0.01)');
220
221 ctx.fillStyle = r;
222 //ctx.fillStyle = "rgba(0,128,0,0.15)";
223 var w2 = this._strokeWidth*0.5;
224 ctx.moveTo(prevX-w2, prevY);
225 ctx.lineTo(prevX+w2, prevY);
226 ctx.lineTo(x+w2, y);
227 ctx.lineTo(x-w2, y);
228 ctx.fill();
229
230 prevX = x;
231 prevY = y;
232 }
233
234 /*
189 var R2 = this._strokeWidth; 235 var R2 = this._strokeWidth;
190 var R = R2*0.5; 236 var R = R2*0.5;
191 var hardness = 0.25; //for a pencil, this is always 1 //TODO get hardness parameter from user interface 237 var hardness = 0.25; //for a pencil, this is always 1 //TODO get hardness parameter from user interface
@@ -209,6 +255,8 @@ function GLBrushStroke() {
209 //ctx.globalCompositeOperation = 'source-in'; 255 //ctx.globalCompositeOperation = 'source-in';
210 //ctx.rect(x-R, y-R, R2, R2); 256 //ctx.rect(x-R, y-R, R2, R2);
211 } 257 }
258 */
259
212 ctx.restore(); 260 ctx.restore();
213 } //render() 261 } //render()
214 262
diff --git a/js/tools/BrushTool.js b/js/tools/BrushTool.js
index ce8ffbb9..97df84a0 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)) { 203 if ((Math.floor(w) === 0) || (Math.floor(h) === 0)) {
197 return; 204 return;
198 } 205 }
199 206
200 var left = Math.round(midPt[0] - 0.5 * w); 207 var left = Math.round(midPt[0] - 0.5 * w);
201 var top = Math.round(midPt[1] - 0.5 * h); 208 var top = Math.round(midPt[1] - 0.5 * h);
202 209
203 if (!canvas) { 210 if (!canvas) {
204 var newCanvas = NJUtils.makeNJElement("canvas", "Brushstroke", "shape", null, true); 211 var newCanvas = NJUtils.makeNJElement("canvas", "Brushstroke", "shape", null, true);
205 var elementModel = TagTool.makeElement(w, h, planeMat, midPt, newCanvas); 212 var elementModel = TagTool.makeElement(w, h, planeMat, midPt, newCanvas);
206 ElementMediator.addElement(newCanvas, elementModel.data, true); 213 ElementMediator.addElement(newCanvas, elementModel.data, true);
207 214
208 // create all the GL stuff 215 // create all the GL stuff
209 var world = this.getGLWorld(newCanvas, this._useWebGL); 216 var world = this.getGLWorld(newCanvas, this._useWebGL);
210 //store a reference to this newly created canvas 217 //store a reference to this newly created canvas
211 this._brushStrokeCanvas = newCanvas; 218 this._brushStrokeCanvas = newCanvas;
212 219
213 var brushStroke = this._selectedBrushStroke; 220 var brushStroke = this._selectedBrushStroke;
214 if (brushStroke){ 221 if (brushStroke){
215 brushStroke.setWorld(world); 222 brushStroke.setWorld(world);
216 223
217 brushStroke.setPlaneMatrix(planeMat); 224 brushStroke.setPlaneMatrix(planeMat);
218 var planeMatInv = glmat4.inverse( planeMat, [] ); 225 var planeMatInv = glmat4.inverse( planeMat, [] );
219 brushStroke.setPlaneMatrixInverse(planeMatInv); 226 brushStroke.setPlaneMatrixInverse(planeMatInv);
220 brushStroke.setPlaneCenter(midPt); 227 brushStroke.setPlaneCenter(midPt);
221 228
222 world.addObject(brushStroke); 229 world.addObject(brushStroke);
223 world.render(); 230 world.render();
224 //TODO this will not work if there are multiple shapes in the same canvas 231 //TODO this will not work if there are multiple shapes in the same canvas
225 newCanvas.elementModel.shapeModel.GLGeomObj = brushStroke; 232 newCanvas.elementModel.shapeModel.GLGeomObj = brushStroke;
226 } 233 }
227 } //if (!canvas) { 234 } //if (!canvas) {
228 else { 235 else {
229
230 var world = null;
231 if (canvas.elementModel.shapeModel && canvas.elementModel.shapeModel.GLWorld) {
232