{"size": 1478, "ext": "js", "max_stars_repo_path": "src/main.js", "max_stars_repo_name": "GlobeletJS/tile-gl", "max_stars_repo_stars_event_min_datetime": "2020-11-27T17:49:29.000Z", "max_stars_repo_stars_event_max_datetime": "2022-03-18T02:55:26.000Z", "max_issues_repo_path": "src/main.js", "max_issues_repo_name": "GlobeletJS/tile-gl", "max_issues_repo_issues_event_min_datetime": "2022-03-11T00:40:52.000Z", "max_issues_repo_issues_event_max_datetime": "2022-03-11T00:40:52.000Z", "max_forks_repo_path": "src/main.js", "max_forks_repo_name": "GlobeletJS/tile-gl", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 8, "max_issues_count": 1, "max_forks_count": null, "avg_line_length": 30.7916666667, "max_line_length": 78, "alphanum_fraction": 0.6711772666}
import { setParams } from "./params.js";
import { initPrograms } from "./programs.js";
import { initBackground } from "./background/program.js";

export function initGLpaint(userParams) {
  const { context, framebuffer, preamble, multiTile } = setParams(userParams);

  const programs = initPrograms(context, framebuffer, preamble, multiTile);
  programs["background"] = initBackground(context);

  function prep() {
    context.bindFramebufferAndSetViewport(framebuffer);
    return context.clear();
  }

  function loadBuffers(layer) {
    const { type, buffers } = layer;

    const program = programs[type];
    if (!program) throw "loadBuffers: unknown layer type";

    layer.buffers = program.load(buffers);
  }

  function loadAtlas(atlas) {
    const format = context.gl.ALPHA;
    const { width, height, data } = atlas;
    return context.initTexture({ format, width, height, data, mips: false });
  }

  function initPainter(style) {
    const { id, type, source, minzoom = 0, maxzoom = 24 } = style;

    const program = programs[type];
    if (!program) return () => null;

    const { layout, paint } = style;
    if (type === "line") {
      // We handle line-miter-limit in the paint phase, not layout phase
      paint["line-miter-limit"] = layout["line-miter-limit"];
    }
    const painter = program.initPainter(style);
    return Object.assign(painter, { id, type, source, minzoom, maxzoom });
  }

  return { prep, loadBuffers, loadAtlas, initPainter };
}
