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

88 lines
1.7 KiB
JavaScript
Raw Normal View History

2014-07-24 05:35:57 +02:00
(function(
Engine,
Vector
){
Engine.Polygon = function(a, b, c, color){
this.a = a;
this.b = b;
this.c = c;
2014-07-24 09:37:27 +02:00
this.color = Engine.clone(color);
2014-07-24 05:35:57 +02:00
this.maxL = this.color.l;
2014-07-24 07:28:16 +02:00
this.strokeColor = {
h: this.color.h,
s: 0,
l: 100,
a: 1
};
2014-07-24 05:35:57 +02:00
this.color.l = 0;
this.fillStyle = this.hslaTemplate.substitute(this.color);
2014-07-24 09:37:27 +02:00
this.strokeStyle = this.hslaTemplate.substitute(this.strokeColor);
2014-07-24 05:35:57 +02:00
};
Engine.Polygon.prototype = {
rgbaTemplate: 'rgba({r},{g},{b},{a})',
hslaTemplate: 'hsla({h},{s}%,{l}%,{a})',
hueShiftSpeed: 20,
2014-07-24 07:28:16 +02:00
duration: 2,
delay: 0,
start: 0,
2014-07-24 05:35:57 +02:00
// Determine color fill?
update: function(engine){
var delta;
2014-07-24 07:28:16 +02:00
this.start += engine.tick;
delta = this.start;
2014-07-24 05:35:57 +02:00
if (
delta > this.delay &&
delta < this.delay + this.duration + 1 &&
this.color.l < this.maxL
) {
this.color.l = this.maxL * (delta - this.delay) / this.duration;
2014-07-24 07:28:16 +02:00
this.strokeColor.s = this.color.s * (delta - this.delay) / this.duration;
this.strokeColor.l = (this.maxL - 100) * (delta - this.delay) / this.duration + 100;
2014-07-24 05:35:57 +02:00
if (this.color.l > this.maxL) {
this.color.l = this.maxL;
}
2014-07-24 07:28:16 +02:00
this.strokeStyle = this.hslaTemplate.substitute(this.strokeColor);
2014-07-24 05:35:57 +02:00
this.fillStyle = this.hslaTemplate.substitute(this.color);
}
},
draw: function(ctx, scale){
ctx.beginPath();
ctx.moveTo(
this.a.pos.x * scale >> 0,
this.a.pos.y * scale >> 0
2014-07-24 05:35:57 +02:00
);
ctx.lineTo(
this.b.pos.x * scale >> 0,
this.b.pos.y * scale >> 0
2014-07-24 05:35:57 +02:00
);
ctx.lineTo(
this.c.pos.x * scale >> 0,
this.c.pos.y * scale >> 0
2014-07-24 05:35:57 +02:00
);
ctx.closePath();
ctx.fillStyle = this.fillStyle;
ctx.lineWidth = 0.25 * scale;
2014-07-24 07:28:16 +02:00
ctx.strokeStyle = this.strokeStyle;
2014-07-24 05:35:57 +02:00
ctx.fill();
ctx.stroke();
}
};
})(window.Engine, window.Vector);