Empirical
CanvasShape.h
Go to the documentation of this file.
1 
17 #ifndef EMP_WEB_CANVAS_SHAPE_H
18 #define EMP_WEB_CANVAS_SHAPE_H
19 
20 #include <string>
21 
22 #include "../base/vector.h"
23 #include "../geometry/Circle2D.h"
24 
25 #include "CanvasAction.h"
26 
27 namespace emp {
28 namespace web {
29 
31  class CanvasShape : public CanvasAction {
32  protected:
33  Point p;
34  std::string fill_color;
35  std::string line_color;
36  double line_width;
37 
38  public:
39  CanvasShape(double _x, double _y, const std::string & fc="", const std::string & lc="", double lw=1.0)
40  : p(_x,_y), fill_color(fc), line_color(lc), line_width(lw) { ; }
41  CanvasShape(Point _p, const std::string & fc="", const std::string & lc="", double lw=1.0)
42  : p(_p), fill_color(fc), line_color(lc), line_width(lw) { ; }
43  virtual ~CanvasShape() { ; }
44 
46  void MoveTo(Point _p) { p = _p; }
47 
49  void MoveTo(double _x, double _y) { p.Set(_x,_y); }
50 
52  void SetLineWidth(double lw = 1.0) { line_width = lw; }
53 
55  void SetFillColor(const std::string & color) { fill_color = color; }
56 
58  void SetLineColor(const std::string & color) { line_color = color; }
59 
61  void ApplyColor() {
62  LineWidth(line_width);
63  Fill(fill_color);
64  Stroke(line_color);
65  }
66  };
67 
69  class CanvasCircle : public CanvasShape {
70  double radius;
71  public:
72  CanvasCircle(double _x, double _y, double _r,
73  const std::string & fc="", const std::string & lc="", double lw=1.0)
74  : CanvasShape(_x, _y, fc, lc, lw), radius(_r) { ; }
75 
76  CanvasCircle(Point _p, double _r, const std::string & fc="", const std::string & lc="", double lw=1.0)
77  : CanvasShape(_p, fc, lc, lw), radius(_r) { ; }
78 
79  CanvasCircle(emp::Circle circle, const std::string & fc="", const std::string & lc="", double lw=1.0)
80  : CanvasShape(circle.GetCenterX(), circle.GetCenterY(), fc, lc, lw)
81  , radius(circle.GetRadius()) { ; }
82 
83  void Apply() {
84  EM_ASM_ARGS({
85  emp_i.ctx.beginPath();
86  emp_i.ctx.arc($0, $1, $2, 0, Math.PI*2);
87  }, p.GetX(), p.GetY(), radius); // Draw the circle
88  ApplyColor();
89  }
90  CanvasAction * Clone() const { return new CanvasCircle(*this); }
91  };
92 
94  class CanvasRect : public CanvasShape {
95  double w;
96  double h;
97  public:
98  CanvasRect(Point _p, double _w, double _h,
99  const std::string & fc="", const std::string & lc="")
100  : CanvasShape(_p, fc, lc), w(_w), h(_h) { ; }
101 
102  CanvasRect(double _x, double _y, double _w, double _h,
103  const std::string & fc="", const std::string & lc="")
104  : CanvasShape(_x, _y, fc, lc), w(_w), h(_h) { ; }
105 
106  void Apply() {
107  EM_ASM_ARGS({
108  emp_i.ctx.beginPath();
109  emp_i.ctx.rect($0, $1, $2, $3);
110  }, p.GetX(), p.GetY(), w, h); // Draw the rectangle
111  ApplyColor();
112  }
113  CanvasAction * Clone() const { return new CanvasRect(*this); }
114  };
115 
117  class CanvasClearRect : public CanvasShape {
118  double w;
119  double h;
120  public:
121  CanvasClearRect(Point _p, double _w, double _h)
122  : CanvasShape(_p), w(_w), h(_h) { ; }
123 
124  void Apply() {
125  EM_ASM_ARGS({
126  emp_i.ctx.clearRect($0, $1, $2, $3);
127  }, p.GetX(), p.GetY(), w, h); // Draw the rectangle
128  }
129  CanvasAction * Clone() const { return new CanvasClearRect(*this); }
130  };
131 
133  class CanvasPolygon : public CanvasShape {
134  private:
135  emp::vector<Point> points;
136  public:
137  CanvasPolygon(const std::string & fc="", const std::string & lc="")
138  : CanvasShape(0, 0, fc, lc) { ; }
139  CanvasPolygon(const emp::vector<Point> & p, const std::string & fc="", const std::string & lc="")
140  : CanvasShape(0, 0, fc, lc), points(p) { ; }
141  CanvasPolygon(Point _p, const std::string & fc="", const std::string & lc="")
142  : CanvasShape(_p, fc, lc) { ; }
143  CanvasPolygon(double _x, double _y, const std::string & fc="", const std::string & lc="")
144  : CanvasShape(_x, _y, fc, lc) { ; }
145 
146  CanvasPolygon & AddPoint(double x, double y) { points.emplace_back(x,y); return *this; }
147  CanvasPolygon & AddPoint(Point p) { points.emplace_back(p); return *this; }
148 
149  void Apply() {
150  EM_ASM_ARGS({
151  emp_i.ctx.translate($0,$1);
152  emp_i.ctx.beginPath();
153  emp_i.ctx.moveTo($2, $3);
154  }, p.GetX(), p.GetY(), points[0].GetX(), points[0].GetY()); // Setup the polygon
155  for (size_t i = 1; i < points.size(); i++) {
156  EM_ASM_ARGS({
157  emp_i.ctx.lineTo($0, $1);
158  }, points[i].GetX(), points[i].GetY()); // Draw the lines for the polygon
159  }
160  EM_ASM_ARGS({
161  emp_i.ctx.closePath();
162  emp_i.ctx.translate($0,$1);
163  }, -p.GetX(), -p.GetY()); // Close the polygon
164  ApplyColor();
165  }
166  CanvasAction * Clone() const { return new CanvasPolygon(*this); }
167  };
168 
170  class CanvasLine : public CanvasShape {
171  private:
172  double x2;
173  double y2;
174  public:
175  CanvasLine(double _x1, double _y1, double _x2, double _y2,
176  const std::string & lc="", double lw=1.0)
177  : CanvasShape(_x1, _y1, "", lc, lw), x2(_x2), y2(_y2) { ; }
178  CanvasLine(Point p1, Point p2, const std::string & lc="", double lw=1.0)
179  : CanvasLine(p1.GetX(), p1.GetY(), p2.GetX(), p2.GetY(), lc, lw) { ; }
180 
181  void Apply() {
182  EM_ASM_ARGS({
183  emp_i.ctx.beginPath();
184  emp_i.ctx.moveTo($0, $1);
185  emp_i.ctx.lineTo($2, $3);
186 // emp_i.ctx.closePath();
187  }, p.GetX(), p.GetY(), x2, y2);
188  // ApplyColor();
191  }
192  CanvasAction * Clone() const { return new CanvasLine(*this); }
193  };
194 
197  class CanvasMultiLine : public CanvasShape {
198  private:
199  emp::vector<Point> points;
200 
201  public:
202  CanvasMultiLine(double _x1, double _y1, const emp::vector<Point> & _points,
203  const std::string & lc="", double lw=1.0)
204  : CanvasShape(_x1, _y1, "", lc, lw), points(_points) { ; }
206  const std::string & lc="", double lw=1.0)
207  : CanvasMultiLine(p1.GetX(), p1.GetY(), _points, lc, lw) { ; }
208 
209  void Apply() {
210  // Startup the line path.
211  EM_ASM_ARGS({
212  emp_i.ctx.beginPath();
213  emp_i.ctx.moveTo($0, $1);
214  }, p.GetX(), p.GetY());
215  // Loop through all internal points...
216  for (auto p : points) {
217  EM_ASM_ARGS({ emp_i.ctx.lineTo($0, $1); }, p.GetX(), p.GetY());
218  }
219 
222  }
223  CanvasAction * Clone() const { return new CanvasMultiLine(*this); }
224  };
225 
227  class CanvasText : public CanvasShape {
228  protected:
229  std::string text;
230  bool center;
231  public:
232  CanvasText(Point p, const std::string & _text,
233  const std::string & fc="", const std::string & lc="")
234  : CanvasShape(p, fc, lc), text(_text), center(false) { ; }
235 
236  void Apply() {
237  if (center) {
238  EM_ASM({ emp_i.ctx.textAlign = "center"; });
239  EM_ASM({ emp_i.ctx.textBaseline = "middle"; });
240  }
241  EM_ASM_ARGS({
242  emp_i.ctx.fillStyle = Pointer_stringify($3);
243  var text = Pointer_stringify($2);
244  emp_i.ctx.fillText(text,$0,$1);
245  }, p.GetX(), p.GetY(), text.c_str(), fill_color.c_str());
246  }
247 
249  void Center(bool c=true) { center = c; }
250 
252  bool GetCenter() const { return center; }
253 
254  CanvasAction * Clone() const { return new CanvasText(*this); }
255  };
256 
257 }
258 }
259 
260 #endif
Define an arbitrary shape to draw on a canvas (base clase)
Definition: CanvasShape.h:31
void Center(bool c=true)
Center this text.
Definition: CanvasShape.h:249
void SetLineColor(const std::string &color)
Change the stroke color of this shape.
Definition: CanvasShape.h:58
void Apply()
Apply current action to emp_i.ctx.
Definition: CanvasShape.h:181
CanvasCircle(Point _p, double _r, const std::string &fc="", const std::string &lc="", double lw=1.0)
Definition: CanvasShape.h:76
A line segment on the canvas.
Definition: CanvasShape.h:170
constexpr TYPE GetX() const
Definition: Point2D.h:39
CanvasCircle(emp::Circle circle, const std::string &fc="", const std::string &lc="", double lw=1.0)
Definition: CanvasShape.h:79
CanvasLine(double _x1, double _y1, double _x2, double _y2, const std::string &lc="", double lw=1.0)
Y-position for second point of line segment.
Definition: CanvasShape.h:175
Clear a rectangular area in a canvas.
Definition: CanvasShape.h:117
bool center
Should this text be centered (or is anchor on left side)?
Definition: CanvasShape.h:230
Definition: CanvasShape.h:197
CanvasLine(Point p1, Point p2, const std::string &lc="", double lw=1.0)
Definition: CanvasShape.h:178
void Apply()
Apply current action to emp_i.ctx.
Definition: CanvasShape.h:149
Define a base class for all actions that can be done to widgets, plus simple actions.
bool GetCenter() const
Identify if text is centered.
Definition: CanvasShape.h:252
std::string fill_color
Internal color to fill shape with.
Definition: CanvasShape.h:34
virtual ~CanvasShape()
Definition: CanvasShape.h:43
CanvasAction * Clone() const
Make a copy of the current action.
Definition: CanvasShape.h:166
void MoveTo(Point _p)
Shift the position of this shape to a point.
Definition: CanvasShape.h:46
std::string line_color
Border color for shape.
Definition: CanvasShape.h:35
size_t size() const
Definition: vector.h:151
Point2D & Set(TYPE _x, TYPE _y)
Definition: Point2D.h:43
void emplace_back(ARGS &&...args)
Definition: vector.h:219
CanvasCircle(double _x, double _y, double _r, const std::string &fc="", const std::string &lc="", double lw=1.0)
Definition: CanvasShape.h:72
void Apply()
Apply current action to emp_i.ctx.
Definition: CanvasShape.h:236
CanvasRect(Point _p, double _w, double _h, const std::string &fc="", const std::string &lc="")
Definition: CanvasShape.h:98
CanvasMultiLine(double _x1, double _y1, const emp::vector< Point > &_points, const std::string &lc="", double lw=1.0)
Definition: CanvasShape.h:202
void Apply()
Apply current action to emp_i.ctx.
Definition: CanvasShape.h:124
CanvasClearRect(Point _p, double _w, double _h)
Definition: CanvasShape.h:121
std::string text
Specific text to be written.
Definition: CanvasShape.h:229
CanvasPolygon & AddPoint(Point p)
Definition: CanvasShape.h:147
CanvasMultiLine(Point p1, const emp::vector< Point > &_points, const std::string &lc="", double lw=1.0)
Definition: CanvasShape.h:205
CanvasPolygon(double _x, double _y, const std::string &fc="", const std::string &lc="")
Definition: CanvasShape.h:143
CanvasPolygon(const emp::vector< Point > &p, const std::string &fc="", const std::string &lc="")
Definition: CanvasShape.h:139
CanvasAction * Clone() const
Make a copy of the current action.
Definition: CanvasShape.h:254
void SetLineWidth(double lw=1.0)
Setup details needed before drawing lines.
Definition: CanvasShape.h:52
void Apply()
Apply current action to emp_i.ctx.
Definition: CanvasShape.h:106
CanvasShape(Point _p, const std::string &fc="", const std::string &lc="", double lw=1.0)
Definition: CanvasShape.h:41
void LineWidth(double line_width=1.0)
Helper function to set the stroke status.
Definition: CanvasAction.h:56
CanvasPolygon(const std::string &fc="", const std::string &lc="")
Definition: CanvasShape.h:137
CanvasAction * Clone() const
Make a copy of the current action.
Definition: CanvasShape.h:113
Base class to maintain canvas actions.
Definition: CanvasAction.h:33
CanvasShape(double _x, double _y, const std::string &fc="", const std::string &lc="", double lw=1.0)
Definition: CanvasShape.h:39
void ApplyColor()
Actually change the color on screen.
Definition: CanvasShape.h:61
Point p
Anchor point for this shape.
Definition: CanvasShape.h:33
If we are in emscripten, make sure to include the header.
Definition: array.h:37
CanvasText(Point p, const std::string &_text, const std::string &fc="", const std::string &lc="")
Definition: CanvasShape.h:232
Build a debug wrapper emp::vector around std::vector.
Definition: vector.h:42
Text to be written on a canvas.
Definition: CanvasShape.h:227
void MoveTo(double _x, double _y)
Shift the position of this shape to coordinates.
Definition: CanvasShape.h:49
void Apply()
Apply current action to emp_i.ctx.
Definition: CanvasShape.h:209
CanvasPolygon & AddPoint(double x, double y)
Definition: CanvasShape.h:146
CanvasAction * Clone() const
Make a copy of the current action.
Definition: CanvasShape.h:223
CanvasAction * Clone() const
Make a copy of the current action.
Definition: CanvasShape.h:192
CanvasRect(double _x, double _y, double _w, double _h, const std::string &fc="", const std::string &lc="")
Definition: CanvasShape.h:102
double line_width
How wide should lines be?
Definition: CanvasShape.h:36
constexpr TYPE GetY() const
Definition: Point2D.h:40
void Fill(const std::string &style="")
Helper function to set the fill status.
Definition: CanvasAction.h:36
CanvasPolygon(Point _p, const std::string &fc="", const std::string &lc="")
Definition: CanvasShape.h:141
Track a circle shape to be drawn on a canvas.
Definition: CanvasShape.h:69
CanvasAction * Clone() const
Make a copy of the current action.
Definition: CanvasShape.h:90
An arbitrary-sized polygon to be drawn on a canvas.
Definition: CanvasShape.h:133
void Apply()
Apply current action to emp_i.ctx.
Definition: CanvasShape.h:83
void SetFillColor(const std::string &color)
Change the fill color of this shape.
Definition: CanvasShape.h:55
CanvasAction * Clone() const
Make a copy of the current action.
Definition: CanvasShape.h:129
Track a rectangle shape to be drawn on a canvas.
Definition: CanvasShape.h:94
void Stroke(const std::string &style="")
Helper function to set the stroke status.
Definition: CanvasAction.h:46