2014-07-25 12:09:17 +02:00
|
|
|
(function(
|
|
|
|
Engine,
|
|
|
|
Point,
|
|
|
|
Polygon,
|
|
|
|
Vector
|
|
|
|
){
|
|
|
|
|
2014-07-26 06:04:52 +02:00
|
|
|
Engine.Shape.Puller = function(width, height, json){
|
2014-07-25 12:09:17 +02:00
|
|
|
var i, ref, point, poly;
|
|
|
|
|
2014-07-26 06:04:52 +02:00
|
|
|
this.pos = new Vector(0, 0);
|
2014-07-25 12:09:17 +02:00
|
|
|
this.size = new Vector(width, height);
|
2014-07-26 06:04:52 +02:00
|
|
|
this.heightRatio = json.data.width / json.data.height;
|
|
|
|
this.widthRatio = json.data.ar;
|
2014-07-25 12:09:17 +02:00
|
|
|
|
2014-07-26 05:28:47 +02:00
|
|
|
this.resize(width, height, true);
|
|
|
|
|
2014-07-25 12:09:17 +02:00
|
|
|
ref = {};
|
|
|
|
this.points = [];
|
|
|
|
this.polygons = [];
|
|
|
|
|
2014-07-26 06:04:52 +02:00
|
|
|
for (i = 0; i < json.points.length; i++) {
|
2014-07-25 12:09:17 +02:00
|
|
|
point = new Point(
|
2014-07-26 06:04:52 +02:00
|
|
|
json.points[i].id,
|
|
|
|
json.points[i].x,
|
|
|
|
json.points[i].y,
|
2014-07-26 05:28:47 +02:00
|
|
|
this.size
|
2014-07-25 12:09:17 +02:00
|
|
|
);
|
|
|
|
ref[point.id] = point;
|
|
|
|
this.points.push(point);
|
|
|
|
}
|
|
|
|
|
2014-07-26 06:04:52 +02:00
|
|
|
for (i = 0; i < json.polygons.length; i++) {
|
|
|
|
poly = json.polygons[i];
|
2014-07-25 12:09:17 +02:00
|
|
|
this.polygons.push(new Polygon(
|
|
|
|
ref[poly.points[0]],
|
|
|
|
ref[poly.points[1]],
|
|
|
|
ref[poly.points[2]],
|
|
|
|
poly.color
|
|
|
|
));
|
|
|
|
this.polygons[this.polygons.length - 1].noFill = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.ref = undefined;
|
|
|
|
};
|
|
|
|
|
|
|
|
Engine.Shape.Puller.prototype = {
|
|
|
|
|
|
|
|
alpha: 0,
|
|
|
|
|
2014-07-26 05:28:47 +02:00
|
|
|
sizeOffset: 100,
|
|
|
|
|
|
|
|
resize: function(width, height, sizeOnly){
|
2014-07-26 06:04:52 +02:00
|
|
|
var len, p, newWidth, newHeight;
|
2014-07-26 05:28:47 +02:00
|
|
|
|
2014-07-26 06:04:52 +02:00
|
|
|
newHeight = height + this.sizeOffset;
|
|
|
|
newWidth = this.size.y * this.heightRatio;
|
2014-07-26 05:28:47 +02:00
|
|
|
|
2014-07-26 06:04:52 +02:00
|
|
|
if (newWidth < width) {
|
|
|
|
newWidth = width + this.sizeOffset;
|
|
|
|
newHeight = newWidth * this.widthRatio;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.size.y = newHeight;
|
|
|
|
this.size.x = newWidth;
|
|
|
|
|
|
|
|
this.pos.x = -(newWidth / 2);
|
|
|
|
this.pos.y = -(newHeight / 2);
|
2014-07-26 05:28:47 +02:00
|
|
|
|
|
|
|
if (sizeOnly) {
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (p = 0, len = this.points.length; p < len; p++) {
|
|
|
|
this.points[p].resize();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2014-07-25 12:09:17 +02:00
|
|
|
update: function(engine){
|
|
|
|
var p;
|
|
|
|
|
|
|
|
for (p = 0; p < this.points.length; p++) {
|
|
|
|
this.points[p].update(engine);
|
|
|
|
}
|
|
|
|
|
2014-07-26 06:16:16 +02:00
|
|
|
if (this.alpha < 1) {
|
|
|
|
this.alpha = Math.min(this.alpha + 2 * engine.tick, 1);
|
2014-07-25 12:09:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2014-07-26 06:16:16 +02:00
|
|
|
draw: function(ctx, scale, engine){
|
2014-07-26 09:11:43 +02:00
|
|
|
var p, poly;
|
2014-07-25 12:09:17 +02:00
|
|
|
|
|
|
|
ctx.translate(
|
|
|
|
this.pos.x * scale >> 0,
|
|
|
|
this.pos.y * scale >> 0
|
|
|
|
);
|
2014-07-26 06:16:16 +02:00
|
|
|
|
|
|
|
if (this.alpha < 1) {
|
|
|
|
ctx.globalAlpha = this.alpha;
|
|
|
|
}
|
|
|
|
|
2014-07-25 12:09:17 +02:00
|
|
|
ctx.beginPath();
|
|
|
|
for (p = 0; p < this.polygons.length; p++) {
|
2014-07-26 09:11:43 +02:00
|
|
|
poly = this.polygons[p];
|
|
|
|
ctx.moveTo(
|
|
|
|
poly.a.pos.x * scale >> 0,
|
|
|
|
poly.a.pos.y * scale >> 0
|
|
|
|
);
|
|
|
|
ctx.lineTo(
|
|
|
|
poly.b.pos.x * scale >> 0,
|
|
|
|
poly.b.pos.y * scale >> 0
|
|
|
|
);
|
|
|
|
ctx.lineTo(
|
|
|
|
poly.c.pos.x * scale >> 0,
|
|
|
|
poly.c.pos.y * scale >> 0
|
|
|
|
);
|
|
|
|
ctx.lineTo(
|
|
|
|
poly.a.pos.x * scale >> 0,
|
|
|
|
poly.a.pos.y * scale >> 0
|
|
|
|
);
|
2014-07-25 12:09:17 +02:00
|
|
|
}
|
|
|
|
ctx.closePath();
|
2014-07-26 09:11:43 +02:00
|
|
|
ctx.lineWidth = 0.4 * scale;
|
2014-07-28 05:12:37 +02:00
|
|
|
ctx.strokeStyle = 'rgba(108,0,243,0.15)';
|
2014-07-25 12:09:17 +02:00
|
|
|
ctx.stroke();
|
|
|
|
|
2014-07-26 06:16:16 +02:00
|
|
|
if (this.alpha < 1) {
|
|
|
|
ctx.globalAlpha = 1;
|
|
|
|
}
|
|
|
|
|
2014-07-25 12:09:17 +02:00
|
|
|
for (p = 0; p < this.points.length; p++) {
|
|
|
|
this.points[p].draw(ctx, scale);
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.beginPath();
|
|
|
|
for (p = 0; p < this.polygons.length; p++) {
|
|
|
|
if (this.polygons[p].checkChasing()) {
|
2014-07-26 09:11:43 +02:00
|
|
|
poly = this.polygons[p];
|
|
|
|
ctx.moveTo(
|
|
|
|
poly.a.pos.x * scale >> 0,
|
|
|
|
poly.a.pos.y * scale >> 0
|
|
|
|
);
|
|
|
|
ctx.lineTo(
|
|
|
|
poly.b.pos.x * scale >> 0,
|
|
|
|
poly.b.pos.y * scale >> 0
|
|
|
|
);
|
|
|
|
ctx.lineTo(
|
|
|
|
poly.c.pos.x * scale >> 0,
|
|
|
|
poly.c.pos.y * scale >> 0
|
|
|
|
);
|
|
|
|
ctx.lineTo(
|
|
|
|
poly.a.pos.x * scale >> 0,
|
|
|
|
poly.a.pos.y * scale >> 0
|
|
|
|
);
|
2014-07-25 12:09:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
ctx.closePath();
|
2014-07-28 05:04:59 +02:00
|
|
|
ctx.fillStyle = 'rgba(108,0,243,0.05)';
|
2014-07-25 12:09:17 +02:00
|
|
|
ctx.fill();
|
|
|
|
|
2014-07-26 06:16:16 +02:00
|
|
|
ctx.setTransform(1, 0, 0, 1, 0, 0);
|
|
|
|
ctx.translate(
|
|
|
|
engine.width / 2 * engine.scale >> 0,
|
|
|
|
engine.height / 2 * engine.scale >> 0
|
|
|
|
);
|
2014-07-25 12:09:17 +02:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
})(
|
|
|
|
window.Engine,
|
|
|
|
window.Engine.Point.Puller,
|
|
|
|
window.Engine.Polygon.Puller,
|
|
|
|
window.Vector
|
|
|
|
);
|