terraform/website/source/javascripts/app/Engine.Shape.js

103 lines
1.8 KiB
JavaScript
Raw Normal View History

2014-07-24 09:37:27 +02:00
(function(
Engine,
Point,
Polygon,
Vector
){
Engine.Shape = function(x, y, width, height, points, polygons, simple){
2014-07-24 09:37:27 +02:00
var i, ref, point, poly;
this.pos = new Vector(x, y);
this.size = new Vector(width, height);
ref = {};
this.points = [];
this.polygons = [];
for (i = 0; i < points.length; i++) {
point = new Point(
points[i].id,
points[i].x * this.size.x,
points[i].y * this.size.y,
this.size.x,
this.size.y
);
ref[point.id] = point;
this.points.push(point);
}
for (i = 0; i < polygons.length; i++) {
poly = polygons[i];
this.polygons.push(new Polygon(
ref[poly.points[0]],
ref[poly.points[1]],
ref[poly.points[2]],
poly.color,
simple
2014-07-24 09:37:27 +02:00
));
}
};
Engine.Shape.prototype = {
update: function(engine){
var p;
for (p = 0; p < this.points.length; p++) {
this.points[p].update(engine);
}
for (p = 0; p < this.polygons.length; p++) {
2014-07-26 08:09:04 +02:00
this.polygons[p].update(engine);
2014-07-24 09:37:27 +02:00
}
2014-07-24 09:46:14 +02:00
return this;
2014-07-24 09:37:27 +02:00
},
draw: function(ctx, scale, engine){
var p, poly;
2014-07-24 09:37:27 +02:00
ctx.translate(
this.pos.x * scale >> 0,
this.pos.y * scale >> 0
);
2014-07-24 09:37:27 +02:00
for (p = 0; p < this.polygons.length; p++) {
poly = this.polygons[p];
ctx.beginPath();
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.closePath();
ctx.fillStyle = poly.fillStyle;
ctx.fill();
ctx.lineWidth = 0.25 * scale;
ctx.strokeStyle = poly.strokeStyle;
ctx.stroke();
2014-07-24 09:37:27 +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-24 09:46:14 +02:00
return this;
2014-07-24 09:37:27 +02:00
}
};
})(
window.Engine,
window.Engine.Point,
window.Engine.Polygon,
window.Vector
);