Files
airdef/tools/web_remote/static/symbols.js
T

103 lines
3.6 KiB
JavaScript

// Pictograms are drawn in screen pixels, independent of the map zoom. Airborne
// symbols point north before rotation; heading is clockwise radians from north.
export function drawSymbol(ctx, x, y, {kind, symbol, color = '#d6e5ea', selected = false, uncertain = false, heading} = {}) {
const type = symbol || (kind === 'contact' ? 'unknown' : kind) || 'unknown';
const airborne = ['drone', 'cruise', 'supersonic', 'ballistic', 'jet', 'interceptor'].includes(type);
ctx.save();
ctx.translate(x, y);
ctx.lineJoin = 'round';
ctx.lineCap = 'round';
// A dark halo keeps the silhouette legible on grid lines and other tracks.
const shape = (points, fill = true) => {
ctx.beginPath();
points.forEach(([px, py], i) => i ? ctx.lineTo(px, py) : ctx.moveTo(px, py));
if (fill) ctx.closePath();
ctx.strokeStyle = '#071219';
ctx.lineWidth = 4;
ctx.stroke();
if (fill) {
ctx.fillStyle = color;
ctx.fill();
}
ctx.strokeStyle = color;
ctx.lineWidth = 1.5;
ctx.stroke();
};
const line = (...points) => shape(points, false);
const circle = (cx, cy, r, fill = false) => {
ctx.beginPath();
ctx.arc(cx, cy, r, 0, Math.PI * 2);
ctx.strokeStyle = '#071219';
ctx.lineWidth = 4;
ctx.stroke();
if (fill) { ctx.fillStyle = color; ctx.fill(); }
ctx.strokeStyle = color;
ctx.lineWidth = 1.5;
ctx.stroke();
};
if (selected || uncertain) {
ctx.beginPath();
ctx.arc(0, 0, selected ? 17 : 15, 0, Math.PI * 2);
ctx.setLineDash(uncertain ? [3, 3] : []);
ctx.strokeStyle = '#071219';
ctx.lineWidth = 4;
ctx.stroke();
ctx.strokeStyle = selected ? '#f5fbff' : color;
ctx.lineWidth = selected ? 2 : 1.5;
ctx.stroke();
ctx.setLineDash([]);
}
if (airborne && Number.isFinite(heading)) ctx.rotate(heading);
switch (type) {
case 'drone':
// Broad straight wing, slim fuselage and a visible propeller.
shape([[0,-10],[2,-5],[2,-2],[11,1],[11,3],[2,2],[2,7],[5,9],[5,10],[0,9],[-5,10],[-5,9],[-2,7],[-2,2],[-11,3],[-11,1],[-2,-2],[-2,-5]]);
line([-4,-11],[4,-11]);
break;
case 'cruise':
shape([[0,-11],[2,-6],[2,-1],[8,3],[8,5],[2,3],[2,8],[4,10],[0,9],[-4,10],[-2,8],[-2,3],[-8,5],[-8,3],[-2,-1],[-2,-6]]);
break;
case 'supersonic':
shape([[0,-12],[2,-5],[10,8],[3,6],[1,10],[-1,10],[-3,6],[-10,8],[-2,-5]]);
line([-4,10],[-4,12]); line([4,10],[4,12]);
break;
case 'ballistic':
shape([[0,-12],[3,-7],[3,5],[6,9],[3,9],[2,11],[-2,11],[-3,9],[-6,9],[-3,5],[-3,-7]]);
line([-2,13],[2,13]);
break;
case 'jet':
shape([[0,-12],[2,-6],[2,-3],[11,3],[11,5],[2,2],[2,7],[6,10],[0,9],[-6,10],[-2,7],[-2,2],[-11,5],[-11,3],[-2,-3],[-2,-6]]);
break;
case 'interceptor':
shape([[0,-12],[2,-7],[2,0],[7,6],[7,9],[2,7],[0,11],[-2,7],[-7,9],[-7,6],[-2,0],[-2,-7]]);
break;
case 'radar':
// Dish faces upper left, with a mast and base.
line([-9,-8],[1,2],[5,-2]);
line([-9,-8],[-7,-1],[-2,3],[5,4]);
line([-2,3],[-2,9]); line([-7,10],[4,10]);
circle(0, -6, 1.5, true);
break;
case 'unit':
shape([[-10,-7],[10,-7],[10,8],[-10,8],[-10,-7]], false);
line([-6,4],[6,-4]); line([-6,-4],[6,4]);
break;
case 'object':
shape([[-10,0],[0,-9],[10,0],[8,0],[8,9],[-8,9],[-8,0]]);
ctx.fillStyle = '#071219';
ctx.fillRect(-2, 3, 4, 6);
break;
default:
shape([[0,-11],[11,0],[0,11],[-11,0]]);
ctx.fillStyle = '#071219';
ctx.font = 'bold 16px system-ui, sans-serif';
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';
ctx.fillText('?', 0, 1);
}
ctx.restore();
}