13 #ifndef EMP_WEB_CANVAS_UTILS_H 14 #define EMP_WEB_CANVAS_UTILS_H 18 #include "../base/vector.h" 19 #include "../Evolve/StateGrid.h" 20 #include "../geometry/Circle2D.h" 21 #include "../geometry/Surface2D.h" 22 #include "../tools/BitMatrix.h" 30 const std::string & fill=
"",
31 const std::string & line=
"")
34 canvas.
Draw(circle, fill, line);
39 template <
size_t COLS,
size_t ROWS>
44 double cell_w = w / (double) COLS;
45 double cell_h = h / (double) ROWS;
47 for (
size_t x = 0; x < COLS; x++) {
48 for (
size_t y = 0; y < ROWS; y++) {
49 if (matrix.
Get(x,y)) {
50 canvas.
Rect({x*cell_w, y*cell_h}, cell_w, cell_h,
"black");
62 template <
typename BODY_TYPE>
73 canvas.
Rect({0,0}, w, h,
"black");
77 for (
auto body : body_set) {
79 canvas.
Circle(body->GetPerimeter(),
"", color_map[body->GetColorID()]);
89 template <
typename BODY_TYPE>
108 std::string line_color,
109 double cell_width,
double cell_height,
110 double offset_x,
double offset_y)
118 const size_t grid_rows = grid.size();
119 const size_t grid_cols = grid[0].size();
120 for (
size_t row = 0; row < grid_rows; row++) {
121 const double cur_y = offset_y + row*cell_height;
122 for (
size_t col = 0; col < grid_cols; col++) {
123 const double cur_x = offset_x + col*cell_width;
124 const std::string & cur_color = color_map[grid[row][col]];
125 canvas.
Rect({cur_x,cur_y}, cell_width, cell_height, cur_color, line_color);
140 std::string line_color,
141 double cell_w,
double cell_h)
143 const double canvas_w = canvas.
GetWidth();
144 const double canvas_h = canvas.
GetHeight();
145 const double grid_w = cell_w * grid[0].size();
146 const double grid_h = cell_h * grid.size();
149 const double offset_x = (canvas_w <= grid_w) ? 0 : (canvas_w - grid_w) / 2;
150 const double offset_y = (canvas_h <= grid_h) ? 0 : (canvas_h - grid_h) / 2;
153 Draw(canvas, grid, color_map, line_color, cell_w, cell_h, offset_x, offset_y);
164 std::string line_color=
"black")
167 const double cell_w = canvas.
GetWidth() / grid[0].size();
168 const double cell_h = canvas.
GetHeight() / grid.size();
170 Draw(canvas, grid, color_map, line_color, cell_w, cell_h);
187 std::string line_color,
188 double cell_width,
double cell_height,
189 double offset_x,
double offset_y)
197 const size_t grid_rows = grid.
size() / grid_cols;
199 for (
size_t row = 0; row < grid_rows; row++) {
200 const double cur_y = offset_y + row*cell_height;
201 for (
size_t col = 0; col < grid_cols; col++) {
202 const double cur_x = offset_x + col*cell_width;
203 const std::string & cur_color = color_map[grid[
id++]];
204 canvas.
Rect({cur_x,cur_y}, cell_width, cell_height, cur_color, line_color);
217 std::string line_color=
"black")
220 const double canvas_w = canvas.
GetWidth();
221 const double canvas_h = canvas.
GetHeight();
224 const double cell_w = canvas_w / state_grid.
GetWidth();
225 const double cell_h = canvas_h / state_grid.
GetHeight();
228 const double grid_w = cell_w * state_grid.
GetWidth();
229 const double grid_h = cell_h * state_grid.
GetHeight();
232 const double offset_x = (canvas_w <= grid_w) ? 0 : (canvas_w - grid_w) / 2;
233 const double offset_y = (canvas_h <= grid_h) ? 0 : (canvas_h - grid_h) / 2;
242 for (
size_t row = 0; row < state_grid.
GetHeight(); row++) {
243 const double cur_y = offset_y + row*cell_h;
244 for (
size_t col = 0; col < state_grid.
GetWidth(); col++) {
245 const double cur_x = offset_x + col*cell_w;
246 const int state = state_grid.
GetStates()[
id++];
247 if (state < 0)
continue;
248 const std::string & cur_color = color_map[(size_t) state];
249 canvas.
Rect({cur_x,cur_y}, cell_w, cell_h, cur_color, line_color);
262 const std::string & bg_color,
const std::string & line_color) {
263 canvas.
Clear(bg_color);
265 const double canvas_x = (double) canvas.
GetWidth();
266 const double canvas_y = (double) canvas.
GetHeight();
267 const double cell_width = canvas_x / cols;
268 const double cell_height = canvas_y / rows;
270 for (
size_t i = 0; i <= cols; i++) {
271 double x = cell_width * i;
272 canvas.
Line( {x,0}, {x,canvas_y}, line_color);
274 for (
size_t i = 0; i <= rows; i++) {
275 double y = cell_height * i;
276 canvas.
Line( {0,y}, {canvas_x,y}, line_color);
280 template <
typename CONTAINER_T,
typename POINT_FUN_T,
typename COLOR_FUN_T>
282 POINT_FUN_T && point_fun, COLOR_FUN_T && color_fun,
283 const std::string & line_color=
"black")
286 for (
auto obj : container) {
287 const auto pos = point_fun(obj);
288 const auto color = color_fun(obj);
289 canvas.
Circle(pos, radius, color, line_color);
Canvas & Clear()
Clear everything off of this canvas.
Definition: Canvas.h:254
Canvas & Circle(Point center, double _r, Ts &&...vals)
Definition: Canvas.h:131
const emp::vector< Ptr< BODY_TYPE > > & GetConstBodySet() const
Definition: Surface2D.h:53
bool Get(size_t col, size_t row) const
Definition: BitMatrix.h:101
Definition: Surface2D.h:38
Definition: Circle2D.h:16
A StateGrid describes a map of grid positions to the current state of each position.
Definition: StateGrid.h:105
const emp::vector< std::string > & GetHueMap(size_t map_size, double min_h=0.0, double max_h=360.0, int s=100, int l=50)
Definition: color_map.h:65
double GetWidth() const
Definition: Surface2D.h:48
size_t size() const
Definition: vector.h:151
Manage an HTML canvas object.
Canvas & Line(double x1, double y1, double x2, double y2, Ts &&...vals)
Add a Line from x1,y1 to x2,y2. Optional face color and line color.
Definition: Canvas.h:172
A simple class to manage a COLS x ROWS matrix of bits.
Definition: BitMatrix.h:38
Tools to dynamically build (and cache) color maps.
double GetHeight() const
Definition: Surface2D.h:49
Manage an HTML Canvas object.
Definition: Canvas.h:27
If we are in emscripten, make sure to include the header.
Definition: array.h:37
void Draw(Canvas canvas, const emp::Circle &circle, const std::string &fill="", const std::string &line="")
Draw a Circle onto the canvas.
Definition: canvas_utils.h:29
double GetWidth() const
Get the pixel width of this Canvas.
Definition: Canvas.h:119
size_t GetHeight() const
Definition: StateGrid.h:126
const emp::vector< int > GetStates() const
Definition: StateGrid.h:128
void DrawPoints(Canvas canvas, CONTAINER_T &&container, double radius, POINT_FUN_T &&point_fun, COLOR_FUN_T &&color_fun, const std::string &line_color="black")
Definition: canvas_utils.h:281
double GetHeight() const
Get the pixel height of this Canvas.
Definition: Canvas.h:120
Canvas & Draw(const emp::Circle &circle, const std::string &fc="", const std::string &lc="")
Draw a circle onto this canvas.
Definition: Canvas.h:229
size_t GetWidth() const
Definition: StateGrid.h:125
void DrawGridBG(Canvas canvas, size_t rows, size_t cols, const std::string &bg_color, const std::string &line_color)
Definition: canvas_utils.h:261
Canvas & Rect(Point corner, double w, double h, Ts &&...vals)
Definition: Canvas.h:145