planet-wars/mapbuilder/deprecated/src/index.ts

94 lines
2.2 KiB
TypeScript
Raw Normal View History

2019-10-29 18:30:17 +01:00
import {init_grid} from "./grid"
2019-10-20 11:08:13 +02:00
2019-10-20 19:32:05 +02:00
let gridlineEls:any;
let gridlineNeedsUpdate = true;
function get_line_widths(): string[][] {
if (gridlineNeedsUpdate) {
gridlineEls = document.getElementsByClassName("gridline");
gridlineNeedsUpdate = false;
}
const out: string[][] = [[], [], []];
for(let el of <any[]>gridlineEls) {
let i = 0;
for( let c of el.getElementsByTagName('input')) {
out[i].push(
c.value
);
i += 1;
}
}
return out;
}
2019-10-20 11:08:13 +02:00
function main() {
2019-10-20 15:53:23 +02:00
const canvas = <HTMLCanvasElement>document.getElementById("canvas");
const ctx = canvas.getContext('2d');
const width = canvas.clientWidth;
const height = canvas.clientHeight;
canvas.width = width;
canvas.height = height;
2019-10-20 19:32:05 +02:00
function draw() {
ctx.clearRect(0, 0, width, height);
2019-10-20 15:53:23 +02:00
2019-10-20 19:32:05 +02:00
ctx.save();
2019-10-20 15:53:23 +02:00
2019-10-20 19:32:05 +02:00
ctx.translate(width / 2, height / 2);
2019-10-20 15:53:23 +02:00
2019-10-20 19:32:05 +02:00
ctx.strokeStyle = "#333";
draw_grid(ctx, get_line_widths(), [width / 2, height / 2], [width, height]);
ctx.fillStyle = "#ff5f00";
ctx.fillRect(0, 0, 100, 100);
ctx.restore();
requestAnimationFrame(draw);
}
2019-10-20 15:53:23 +02:00
2019-10-20 11:08:13 +02:00
console.log("hallo");
2019-10-20 19:32:05 +02:00
draw();
2019-10-20 11:08:13 +02:00
}
2019-10-20 15:53:23 +02:00
function draw_grid(
ctx: CanvasRenderingContext2D,
2019-10-20 19:32:05 +02:00
lines: string[][], // number of pixels between lines, with smaller gettings lines
2019-10-20 15:53:23 +02:00
transform: [number, number],
2019-10-20 19:32:05 +02:00
size: [number, number]) {
2019-10-20 15:53:23 +02:00
ctx.save();
ctx.translate(-transform[0], -transform[1]);
2019-10-20 19:32:05 +02:00
for (let i = lines[0].length - 1; i >= 0 ; i -= 1) {
ctx.lineWidth = parseInt(lines[0][i]);
const delta = parseInt(lines[1][i]);
ctx.strokeStyle = "transparent";
ctx.strokeStyle = lines[2][i];
2019-10-20 15:53:23 +02:00
let xoff = transform[0] % delta;
let yoff = transform[1] % delta;
ctx.beginPath();
2019-10-20 19:32:05 +02:00
for (let j = xoff; j < size[0]; j += delta) {
ctx.moveTo(j, 0);
ctx.lineTo(j, size[1]);
2019-10-20 15:53:23 +02:00
}
for (let j = yoff; j < size[1]; j += delta) {
ctx.moveTo(0, j);
ctx.lineTo(size[0], j);
}
ctx.stroke();
}
ctx.restore();
}
setTimeout(main, 200);
2019-10-29 18:30:17 +01:00
init_grid();