All files / geography/maps/src/places placeIcons.ts

51.85% Statements 14/27
16.66% Branches 1/6
66.66% Functions 2/3
54.16% Lines 13/24

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45            19x               7137x                                 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x   2x    
import { faBook, faLeaf, faBasketShopping, faRestroom, faSeedling, faTree, faUsers, faUtensils,
  faCartShopping, faRecycle, faCarrot, faBicycle, faFaucetDrip, faChair, faPalette,
  faLandmark, faImages, faMasksTheater, faPaintbrush, faBinoculars, faChildReaching, faWater,
  faMonument, faSprayCan, faBasketball, faPersonSkating, faSpa, faMountainSun, faMugSaucer, faBreadSlice } from "@fortawesome/free-solid-svg-icons";
import type { PlaceCategory } from "@overworld/places/catalog";
 
const definitions = { park: faTree, "food-cart-pod": faUtensils, "community-center": faUsers, library: faBook,
  "community-garden": faLeaf, "farmers-market": faBasketShopping, "public-restroom": faRestroom, "heritage-tree": faSeedling,
  "grocery-store": faCartShopping, "reuse-repair": faRecycle, "csa-pickup": faCarrot, "bicycle-parking": faBicycle,
  "drinking-fountain": faFaucetDrip, "picnic-seating": faChair, "park-artwork": faPalette,
  museum: faLandmark, "art-gallery": faImages, theatre: faMasksTheater, "arts-centre": faPaintbrush,
  viewpoint: faBinoculars, playground: faChildReaching, "splash-pad": faWater, "historic-site": faMonument,
  "public-art": faSprayCan, "sports-court": faBasketball, skatepark: faPersonSkating, "public-garden": faSpa,
  "nature-reserve": faMountainSun, cafe: faMugSaucer, bakery: faBreadSlice };
export const placeIconDefinition = (category: PlaceCategory) => definitions[category];
 
export function createPlaceIconImage(category: PlaceCategory): ImageData {
  const [width, height, , , paths] = placeIconDefinition(category).icon;
  const canvas = document.createElement("canvas");
  canvas.width = canvas.height = 48;
  const context = canvas.getContext("2d");
  if (!context) throw new Error("Place marker icons require canvas rendering");
  const scale = 36 / Math.max(width, height);
  context.translate((48 - width * scale) / 2, (48 - height * scale) / 2);
  context.scale(scale, scale);
  context.fillStyle = "#ffffff";
  for (const path of Array.isArray(paths) ? paths : [paths]) context.fill(new Path2D(path));
  return context.getImageData(0, 0, 48, 48);
}
 
export function createPlaceIconElement(category: PlaceCategory): SVGSVGElement {
  const [width, height, , , paths] = placeIconDefinition(category).icon;
  const svg = document.createElementNS("http://www.w3.org/2000/svg", "svg");
  svg.setAttribute("viewBox", `0 0 ${width} ${height}`);
  svg.setAttribute("aria-hidden", "true");
  svg.setAttribute("focusable", "false");
  svg.setAttribute("fill", "currentColor");
  for (const data of Array.isArray(paths) ? paths : [paths]) {
    const path = document.createElementNS("http://www.w3.org/2000/svg", "path");
    path.setAttribute("d", data);
    svg.appendChild(path);
  }
  return svg;
}