github.com/nicgrayson/terraform@v0.4.3-0.20150415203910-c4de50829380/website/source/assets/javascripts/app/Engine.Shape.Puller.js (about) 1 (function( 2 Engine, 3 Point, 4 Polygon, 5 Vector 6 ){ 7 8 Engine.Shape.Puller = function(width, height, json){ 9 var i, ref, point, poly; 10 11 this.pos = new Vector(0, 0); 12 this.size = new Vector(width, height); 13 this.heightRatio = json.data.width / json.data.height; 14 this.widthRatio = json.data.ar; 15 16 this.resize(width, height, true); 17 18 ref = {}; 19 this.points = []; 20 this.polygons = []; 21 22 for (i = 0; i < json.points.length; i++) { 23 point = new Point( 24 json.points[i].id, 25 json.points[i].x, 26 json.points[i].y, 27 this.size 28 ); 29 ref[point.id] = point; 30 this.points.push(point); 31 } 32 33 for (i = 0; i < json.polygons.length; i++) { 34 poly = json.polygons[i]; 35 this.polygons.push(new Polygon( 36 ref[poly.points[0]], 37 ref[poly.points[1]], 38 ref[poly.points[2]], 39 poly.color 40 )); 41 this.polygons[this.polygons.length - 1].noFill = true; 42 } 43 44 this.ref = undefined; 45 }; 46 47 Engine.Shape.Puller.prototype = { 48 49 alpha: 0, 50 51 sizeOffset: 100, 52 53 resize: function(width, height, sizeOnly){ 54 var len, p, newWidth, newHeight; 55 56 newHeight = height + this.sizeOffset; 57 newWidth = this.size.y * this.heightRatio; 58 59 if (newWidth < width) { 60 newWidth = width + this.sizeOffset; 61 newHeight = newWidth * this.widthRatio; 62 } 63 64 this.size.y = newHeight; 65 this.size.x = newWidth; 66 67 this.pos.x = -(newWidth / 2); 68 this.pos.y = -(newHeight / 2); 69 70 if (sizeOnly) { 71 return this; 72 } 73 74 for (p = 0, len = this.points.length; p < len; p++) { 75 this.points[p].resize(); 76 } 77 }, 78 79 update: function(engine){ 80 var p; 81 82 for (p = 0; p < this.points.length; p++) { 83 this.points[p].update(engine); 84 } 85 86 if (this.alpha < 1) { 87 this.alpha = Math.min(this.alpha + 2 * engine.tick, 1); 88 } 89 90 return this; 91 }, 92 93 draw: function(ctx, scale, engine){ 94 var p, poly; 95 96 ctx.translate( 97 this.pos.x * scale >> 0, 98 this.pos.y * scale >> 0 99 ); 100 101 if (this.alpha < 1) { 102 ctx.globalAlpha = this.alpha; 103 } 104 105 ctx.beginPath(); 106 for (p = 0; p < this.polygons.length; p++) { 107 poly = this.polygons[p]; 108 ctx.moveTo( 109 poly.a.pos.x * scale >> 0, 110 poly.a.pos.y * scale >> 0 111 ); 112 ctx.lineTo( 113 poly.b.pos.x * scale >> 0, 114 poly.b.pos.y * scale >> 0 115 ); 116 ctx.lineTo( 117 poly.c.pos.x * scale >> 0, 118 poly.c.pos.y * scale >> 0 119 ); 120 ctx.lineTo( 121 poly.a.pos.x * scale >> 0, 122 poly.a.pos.y * scale >> 0 123 ); 124 } 125 ctx.closePath(); 126 ctx.lineWidth = 0.4 * scale; 127 ctx.strokeStyle = 'rgba(108,0,243,0.15)'; 128 ctx.stroke(); 129 130 if (this.alpha < 1) { 131 ctx.globalAlpha = 1; 132 } 133 134 for (p = 0; p < this.points.length; p++) { 135 this.points[p].draw(ctx, scale); 136 } 137 138 ctx.beginPath(); 139 for (p = 0; p < this.polygons.length; p++) { 140 if (this.polygons[p].checkChasing()) { 141 poly = this.polygons[p]; 142 ctx.moveTo( 143 poly.a.pos.x * scale >> 0, 144 poly.a.pos.y * scale >> 0 145 ); 146 ctx.lineTo( 147 poly.b.pos.x * scale >> 0, 148 poly.b.pos.y * scale >> 0 149 ); 150 ctx.lineTo( 151 poly.c.pos.x * scale >> 0, 152 poly.c.pos.y * scale >> 0 153 ); 154 ctx.lineTo( 155 poly.a.pos.x * scale >> 0, 156 poly.a.pos.y * scale >> 0 157 ); 158 } 159 } 160 ctx.closePath(); 161 ctx.fillStyle = 'rgba(108,0,243,0.05)'; 162 ctx.fill(); 163 164 ctx.setTransform(1, 0, 0, 1, 0, 0); 165 ctx.translate( 166 engine.width / 2 * engine.scale >> 0, 167 engine.height / 2 * engine.scale >> 0 168 ); 169 return this; 170 } 171 172 }; 173 174 })( 175 window.Engine, 176 window.Engine.Point.Puller, 177 window.Engine.Polygon.Puller, 178 window.Vector 179 );