/**
* @fileoverview Shared functions to draw various pieces go here
* @author Robert Laing
* @module pieces
*/
function textStroke(ctx, x, y, text, size, colour, lineWidth) {
ctx.font = `${size}px bold sans-serif`;
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.strokeStyle = colour;
ctx.strokeText(text, x, y);
}
function textFill(ctx, x, y, text, size, colour) {
ctx.font = `${size}px bold sans-serif`;
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillStyle = colour;
ctx.fillText(text, x, y);
}
function circleStroke(ctx, x, y, radius, colour, lineWidth) {
ctx.save();
ctx.beginPath();
ctx.lineWidth = lineWidth;
ctx.strokeStyle = colour;
ctx.arc(x, y, radius, 0, Math.PI * 2);
ctx.closePath();
ctx.stroke();
ctx.restore();
}
function circleFill(ctx, x, y, radius, colour) {
ctx.save();
ctx.beginPath();
ctx.fillStyle = colour;
ctx.arc(x, y, radius, 0, Math.PI * 2);
ctx.closePath();
ctx.fill();
ctx.restore();
}
function arrowHead(ctx, x, y, r, angle) {
ctx.save();
ctx.translate(x, y);
ctx.rotate(angle);
ctx.moveTo(0, 0);
ctx.lineTo(-r*0.866, r/2);
ctx.moveTo(0, 0);
ctx.lineTo(-r*0.866, -r/2);
ctx.stroke();
ctx.restore();
}
function arrow(ctx, x1, y1, x2, y2, r, colour) {
ctx.beginPath();
ctx.strokeStyle = colour;
ctx.moveTo(x1, y1);
ctx.lineTo(x2, y2);
arrowHead(ctx, x2, y2, r, Math.atan2(y2 - y1, x2 - x1));
ctx.stroke();
ctx.closePath();
}
export default Object.freeze({
arrow,
circleFill,
circleStroke,
textFill,
textStroke
});