Render a Path as a single SVG <path> element

main
Daniel Asher Resnick 11 months ago
parent f44777e585
commit bc9e7d5477
  1. 30
      HexGrid/Path.pm

@ -35,9 +35,9 @@ sub get_edge_direction($tile1, $tile2)
#TODO: should die here, to be caught/bubbled in render...
}
sub curve_path($x1, $y1, $qx, $qy, $x2, $y2)
sub curve_to($qx, $qy, $x, $y)
{
return "M $x1,$y1 Q $qx,$qy $x2,$y2";
return "Q $qx,$qy $x,$y";
}
@ -48,7 +48,7 @@ sub render($this, $grid, $svg)
return unless @{$this->tiles};
my $g = $svg->g(id => $this->id, class => $this->css_class);
my ($x1, $x2, $y1, $y2);
my ($x0, $x, $y0, $y);
my $current_tile = shift @{$this->tiles};
unless (@{$this->tiles})
@ -59,13 +59,13 @@ sub render($this, $grid, $svg)
fill => $this->colour, style => $this->style, class => $this->css_class);
return;
}
my $path_spec;
my $previous_tile = $current_tile;
$current_tile = shift @{$this->tiles};
my $next_edge = get_edge_direction($previous_tile, $current_tile);
($x1, $y1) = $grid->coords_of_centre($previous_tile->nw, $previous_tile->sw);
($x2, $y2) = $grid->coords_of_edge($previous_tile->nw, $previous_tile->sw, $next_edge);
$g->line(x1 => $x1, y1 => $y1, x2 => $x2, y2 => $y2,
stroke => $this->colour, style => $this->style, class => $this->css_class);
($x0, $y0) = $grid->coords_of_centre($previous_tile->nw, $previous_tile->sw);
($x, $y) = $grid->coords_of_edge($previous_tile->nw, $previous_tile->sw, $next_edge);
$path_spec .= "M $x0,$y0 L $x,$y";
my $previous_edge; # not defined yet
my $next_tile; # not defined yet
@ -75,23 +75,21 @@ sub render($this, $grid, $svg)
$previous_edge = -$next_edge;
$next_edge = get_edge_direction($current_tile, $next_tile);
($x1,$y1) = $grid->coords_of_edge($current_tile->nw, $current_tile->sw, $previous_edge);
my ($qx,$qy) = $grid->coords_of_centre($current_tile->nw, $current_tile->sw);
($x2,$y2) = $grid->coords_of_edge($current_tile->nw, $current_tile->sw, $next_edge);
($x,$y) = $grid->coords_of_edge($current_tile->nw, $current_tile->sw, $next_edge);
$g->path(d => curve_path($x1,$y1, $qx,$qy, $x2,$y2), fill => 'transparent',
stroke => $this->colour, style => $this->style, class => $this->css_class
);
$path_spec .= " " . curve_to($qx,$qy, $x,$y);
$previous_tile = $current_tile;
$current_tile = $next_tile;
}
# When loop is done (or if it was empty) $current_tile is the last tile
# $next_edge will be the last used edge, so use it's opposite for the source of last line
($x1, $y1) = $grid->coords_of_edge($current_tile->nw, $current_tile->sw, -$next_edge);
($x2, $y2) = $grid->coords_of_centre($current_tile->nw, $current_tile->sw);
$g->line(x1 => $x1, y1 => $y1, x2 => $x2, y2 => $y2,
stroke => $this->colour, style => $this->style, class => $this->css_class);
($x, $y) = $grid->coords_of_centre($current_tile->nw, $current_tile->sw);
$path_spec .= " L $x,$y";
$g->path(d => $path_spec, fill => 'transparent',
stroke => $this->colour, style => $this->style, class => $this->css_class
);
}
1;

Loading…
Cancel
Save