|
| 1 | +// shape.js |
| 2 | +// |
| 3 | +// much-needed abstraction for a certain shape |
| 4 | +// |
| 5 | +function Shape(thickness, r,g,b,a, points, id) { |
| 6 | + // Build a new shape |
| 7 | + this.t = thickness; |
| 8 | + this.r = r; |
| 9 | + this.g = g; |
| 10 | + this.b = b; |
| 11 | + this.a = a; |
| 12 | + |
| 13 | + this.p = points; |
| 14 | + |
| 15 | + this.id = id; // I REALLY don't like putting this here. |
| 16 | +} |
| 17 | +// we serialize our points to something similar to base64 |
| 18 | +// major changes: we use _ instead of / and - instead of + as the last |
| 19 | +// characters (it's being sent in a query string) |
| 20 | +var b64="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"; |
| 21 | +function numToB64(num) { |
| 22 | + // return a 3-digit base64 number that represents num |
| 23 | + num = num + 131072; // this bias is 64**3 / 2 and allows us to represent negative numbers |
| 24 | + var result = ""; |
| 25 | + for (var i = 0; i < 3; i++) { |
| 26 | + result = b64[ num%64 ] + result; |
| 27 | + num = parseInt(num / 64,10); |
| 28 | + } |
| 29 | + return result; |
| 30 | +} |
| 31 | +function b64ToNum(b) { |
| 32 | + var result = 0; |
| 33 | + for (var i=0,l=b.length; i<l; i++) { |
| 34 | + var chr = b64.indexOf(b[i]); |
| 35 | + if (chr == -1) { |
| 36 | + throw new Error("contained a char that wasn't our base64"); |
| 37 | + } |
| 38 | + result *= 64; |
| 39 | + result += chr; |
| 40 | + } |
| 41 | + return result - 131072; |
| 42 | +} |
| 43 | +function b64ToPoints(b) { |
| 44 | + if ((b.length % 6) !== 0) { |
| 45 | + throw new Error("invalid point length"); |
| 46 | + } |
| 47 | + var points = []; |
| 48 | + for (var i=0,l=b.length; i<l; i+=6) { |
| 49 | + var x = b64ToNum(b.substring(i, i+3)), |
| 50 | + y = b64ToNum(b.substring(i+3, i+6)); |
| 51 | + points.push( [x,y] ); |
| 52 | + } |
| 53 | + return points; |
| 54 | +} |
| 55 | +Shape.fromParams = function(points, t, r,g,b,a){ |
| 56 | + // Returns a new shape with given points (p), thickness t, color rgba. |
| 57 | + t = t? parseFloat(t):3; |
| 58 | + r = r? parseFloat(r):0; |
| 59 | + g = g? parseFloat(g):0; |
| 60 | + b = b? parseFloat(b):0; |
| 61 | + a = a? parseFloat(a):1; |
| 62 | + // now verify points |
| 63 | + points = b64ToPoints(points); |
| 64 | + if (points.length===0) { |
| 65 | + throw new Error("No points"); |
| 66 | + } else { |
| 67 | + return new Shape(t, r,g,b,a, points, 0); |
| 68 | + } |
| 69 | +}; |
| 70 | +Shape.fromJSON = function(data){ |
| 71 | + // this json is assumed to contain normal points (e.g. it's saved on disk) |
| 72 | + return new Shape(data.t, data.r,data.g,data.b,data.a, data.p, data.id||0); |
| 73 | +}; |
| 74 | + |
| 75 | +Shape.prototype.serializePoints = function(){ |
| 76 | + var result = ""; |
| 77 | + for (var i=0,l=this.p.length; i<l; i++) { |
| 78 | + result += numToB64(this.p[i][0])+numToB64(this.p[i][1]); |
| 79 | + } |
| 80 | + return result; |
| 81 | +}; |
| 82 | + |
| 83 | +Shape.prototype.toJSON = function(){ |
| 84 | + return { |
| 85 | + t: this.t, |
| 86 | + r: this.r, |
| 87 | + g: this.g, |
| 88 | + b: this.b, |
| 89 | + a: this.a, |
| 90 | + p: this.p, |
| 91 | + id: this.id |
| 92 | + }; |
| 93 | +}; |
| 94 | +Shape.prototype.toJSONWithSerializedPoints = function(){ |
| 95 | + return { |
| 96 | + t: this.t, |
| 97 | + r: this.r, |
| 98 | + g: this.g, |
| 99 | + b: this.b, |
| 100 | + a: this.a, |
| 101 | + p: this.serializePoints(), |
| 102 | + id: this.id |
| 103 | + }; |
| 104 | +}; |
| 105 | + |
| 106 | +exports.Shape = Shape; |
0 commit comments