aboutsummaryrefslogtreecommitdiff
path: root/js/lib/geom/brush-stroke.js
diff options
context:
space:
mode:
Diffstat (limited to 'js/lib/geom/brush-stroke.js')
-rwxr-xr-xjs/lib/geom/brush-stroke.js606
1 files changed, 400 insertions, 206 deletions
diff --git a/js/lib/geom/brush-stroke.js b/js/lib/geom/brush-stroke.js
index 4c42539a..70429ca9 100755
--- a/js/lib/geom/brush-stroke.js
+++ b/js/lib/geom/brush-stroke.js
@@ -4,9 +4,12 @@ No rights, expressed or implied, whatsoever to this software are provided by Mot
4(c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved. 4(c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved.
5</copyright> */ 5</copyright> */
6 6
7// Todo: This entire class should be converted to a module
8var VecUtils = require("js/helper-classes/3D/vec-utils").VecUtils; 7var VecUtils = require("js/helper-classes/3D/vec-utils").VecUtils;
9var GeomObj = require("js/lib/geom/geom-obj").GeomObj; 8var GeomObj = require("js/lib/geom/geom-obj").GeomObj;
9var CanvasController = require("js/controllers/elements/canvas-controller").CanvasController;
10var ViewUtils = require("js/helper-classes/3D/view-utils").ViewUtils;
11
12// Todo: This entire class should be converted to a module
10 13
11/////////////////////////////////////////////////////////////////////// 14///////////////////////////////////////////////////////////////////////
12// Class GLBrushStroke 15// Class GLBrushStroke
@@ -17,20 +20,20 @@ var BrushStroke = function GLBrushStroke() {
17 /////////////////////////////////////////////////// 20 ///////////////////////////////////////////////////
18 // Instance variables 21 // Instance variables
19 /////////////////////////////////////////////////// 22 ///////////////////////////////////////////////////
20 this._Points = []; 23 this._Points = []; //current state of points in stage-world space (may be different from input)
24 this._OrigPoints = []; //copy of input points without any smoothing
25 this._LocalPoints = []; //_Points in local coordinates...do this before rendering the points in the canvas
26 this._stageWorldCenter = [0,0,0]; //coordinate for the canvas midPoint: a 3D vector in stage world space
21 this._BBoxMin = [0, 0, 0]; 27 this._BBoxMin = [0, 0, 0];
22 this._BBoxMax = [0, 0, 0]; 28 this._BBoxMax = [0, 0, 0];
23 this._dirty = true; 29 this._isDirty = true;
24 30 this._isInit = false;
25 //whether or not to use the canvas drawing to stroke/fill 31
26 this._useCanvasDrawing = true; 32 //the HTML5 canvas that holds this brush stroke
27 33 this._canvas = null;
28 //the X and Y location of this subpath's canvas in stage world space of Ninja
29 this._canvasX = 0;
30 this._canvasY = 0;
31 34
32 //stroke information 35 //stroke information
33 this._strokeWidth = 0.0; 36 this._strokeWidth = 1.0;
34 this._strokeColor = [0.4, 0.4, 0.4, 1.0]; 37 this._strokeColor = [0.4, 0.4, 0.4, 1.0];
35 this._secondStrokeColor = [1, 0.4, 0.4, 1.0]; 38 this._secondStrokeColor = [1, 0.4, 0.4, 1.0];
36 this._strokeHardness = 100; 39 this._strokeHardness = 100;
@@ -39,10 +42,7 @@ var BrushStroke = function GLBrushStroke() {
39 this._strokeDoSmoothing = false; 42 this._strokeDoSmoothing = false;
40 this._strokeUseCalligraphic = false; 43 this._strokeUseCalligraphic = false;
41 this._strokeAngle = 0; 44 this._strokeAngle = 0;
42 45 this._strokeAmountSmoothing = 0;
43 //the wetness of the brush (currently this is multiplied to the square of the stroke width, but todo should be changed to not depend on stroke width entirely
44 //smaller value means more samples for the path
45 this._WETNESS_FACTOR = 0.25;
46 46
47 //threshold that tells us whether two samples are too far apart 47 //threshold that tells us whether two samples are too far apart
48 this._MAX_SAMPLE_DISTANCE_THRESHOLD = 5; 48 this._MAX_SAMPLE_DISTANCE_THRESHOLD = 5;
@@ -51,7 +51,7 @@ var BrushStroke = function GLBrushStroke() {
51 this._MIN_SAMPLE_DISTANCE_THRESHOLD = 2; 51 this._MIN_SAMPLE_DISTANCE_THRESHOLD = 2;
52 52
53 //prevent extremely long paths that can take a long time to render 53 //prevent extremely long paths that can take a long time to render
54 this._MAX_ALLOWED_SAMPLES = 500; 54 this._MAX_ALLOWED_SAMPLES = 5000;
55 55
56 //drawing context 56 //drawing context
57 this._world = null; 57 this._world = null;
@@ -61,10 +61,15 @@ var BrushStroke = function GLBrushStroke() {
61 this._planeMat = null; 61 this._planeMat = null;
62 this._planeMatInv = null; 62 this._planeMatInv = null;
63 this._planeCenter = null; 63 this._planeCenter = null;
64 this._dragPlane = null;
64 65
65 ///////////////////////////////////////////////////////// 66 /////////////////////////////////////////////////////////
66 // Property Accessors/Setters 67 // Property Accessors/Setters
67 ///////////////////////////////////////////////////////// 68 /////////////////////////////////////////////////////////
69 this.setCanvas = function(c) {
70 this._canvas = c;
71 }
72
68 this.setWorld = function (world) { 73 this.setWorld = function (world) {
69 this._world = world; 74 this._world = world;
70 }; 75 };
@@ -97,20 +102,8 @@ var BrushStroke = function GLBrushStroke() {
97 this._planeCenter = pc; 102 this._planeCenter = pc;
98 }; 103 };
99 104
100 this.getCanvasX = function(){ 105 this.setDragPlane = function(p){
101 return this._canvasX; 106 this._dragPlane = p;
102 };
103
104 this.getCanvasY = function(){
105 return this._canvasY;
106 };
107
108 this.setCanvasX = function(cx){
109 this._canvasX=cx;
110 };
111
112 this.setCanvasY = function(cy){
113 this._canvasY=cy;
114 }; 107 };
115 108
116 this.getNumPoints = function () { 109 this.getNumPoints = function () {
@@ -125,30 +118,38 @@ var BrushStroke = function GLBrushStroke() {
125 //add the point only if it is some epsilon away from the previous point 118 //add the point only if it is some epsilon away from the previous point
126 var numPoints = this._Points.length; 119 var numPoints = this._Points.length;
127 if (numPoints>0) { 120 if (numPoints>0) {
128 var threshold = this._MIN_SAMPLE_DISTANCE_THRESHOLD;//this._WETNESS_FACTOR*this._strokeWidth; 121 var threshold = this._MIN_SAMPLE_DISTANCE_THRESHOLD;
129 var prevPt = this._Points[numPoints-1]; 122 var prevPt = this._Points[numPoints-1];
130 var diffPt = [prevPt[0]-pt[0], prevPt[1]-pt[1]]; 123 var diffPt = [prevPt[0]-pt[0], prevPt[1]-pt[1]];
131 var diffPtMag = Math.sqrt(diffPt[0]*diffPt[0] + diffPt[1]*diffPt[1]); 124 var diffPtMag = Math.sqrt(diffPt[0]*diffPt[0] + diffPt[1]*diffPt[1]);
132 if (diffPtMag>threshold){ 125 if (diffPtMag>threshold){
133 this._Points.push(pt); 126 this._Points.push(pt);
134 this._dirty=true; 127 this._isDirty=true;
128 this._isInit = false;
135 } 129 }
136 } else { 130 } else {
137 this._Points.push(pt); 131 this._Points.push(pt);
138 this._dirty=true; 132 this._isDirty=true;
133 this._isInit = false;
139 } 134 }
140 }; 135 };
141 136
142 this.insertPoint = function(pt, index){ 137 this.insertPoint = function(pt, index){
143 this._Points.splice(index, 0, pt); this._dirty=true; 138 this._Points.splice(index, 0, pt);
139 this._isDirty=true;
140 this._isInit = false;
144 }; 141 };
145 142
146 this.isDirty = function(){ 143 this.isDirty = function(){
147 return this._dirty; 144 return this._isDirty;
148 }; 145 };
149 146
150 this.makeDirty = function(){ 147 this.makeDirty = function(){
151 this._dirty=true; 148 this._isDirty=true;
149 };
150
151 this.getStageWorldCenter = function() {
152 return this._stageWorldCenter;
152 }; 153 };
153 154
154 this.getBBoxMin = function () { 155 this.getBBoxMin = function () {
@@ -165,7 +166,10 @@ var BrushStroke = function GLBrushStroke() {
165 166
166 this.setStrokeWidth = function (w) { 167 this.setStrokeWidth = function (w) {
167 this._strokeWidth = w; 168 this._strokeWidth = w;
168 this._dirty=true; 169 if (this._strokeWidth<1) {
170 this._strokeWidth = 1;
171 }
172 this._isDirty=true;
169 }; 173 };
170 174
171 this.getStrokeMaterial = function () { 175 this.getStrokeMaterial = function () {
@@ -173,7 +177,7 @@ var BrushStroke = function GLBrushStroke() {
173 }; 177 };
174 178
175 this.setStrokeMaterial = function (m) { 179 this.setStrokeMaterial = function (m) {
176 this._strokeMaterial = m; 180 this._strokeMaterial = m; this._isDirty = true;
177 }; 181 };
178 182
179 this.getStrokeColor = function () { 183 this.getStrokeColor = function () {
@@ -181,27 +185,65 @@ var BrushStroke = function GLBrushStroke() {
181 }; 185 };
182 186
183 this.setStrokeColor = function (c) { 187 this.setStrokeColor = function (c) {
184 this._strokeColor = c; 188 this._strokeColor = c; this._isDirty = true;
185 }; 189 };
186 190
187 this.setSecondStrokeColor = function(c){ 191 this.setSecondStrokeColor = function(c){
188 this._secondStrokeColor=c; 192 this._secondStrokeColor=c; this._isDirty = true;
189 } 193 }
190 194
191 this.setStrokeHardness = function(h){ 195 this.setStrokeHardness = function(h){
192 this._strokeHardness=h; 196 if (this._strokeHardness!==h){
197 this._strokeHardness=h;
198 this._isDirty = true;
199 }
200 }
201 this.getStrokeHardness = function(){
202 return this._strokeHardness;
193 } 203 }
194 204
195 this.setDoSmoothing = function(s){ 205 this.setDoSmoothing = function(s){
196 this._strokeDoSmoothing = s; 206 if (this._strokeDoSmoothing!==s) {
207 this._strokeDoSmoothing = s;
208 this._isDirty = true;
209 }
210 }
211
212 this.getDoSmoothing = function(){
213 return this._strokeDoSmoothing;
214 }
215
216 this.setSmoothingAmount = functi