Empirical
Canvas.h
Go to the documentation of this file.
1 
12 #ifndef EMP_WEB_CANVAS_H
13 #define EMP_WEB_CANVAS_H
14 
15 #include <string>
16 
17 #include "../base/vector.h"
18 #include "../geometry/Circle2D.h"
19 
20 #include "CanvasAction.h"
21 #include "CanvasShape.h"
22 
23 namespace emp {
24 namespace web {
25 
27  class Canvas : public internal::WidgetFacet<Canvas> {
28  friend class CanvasInfo;
29  protected:
30 
32  friend Canvas;
33 
34  protected:
35  double width;
36  double height;
37 
39 
40  CanvasInfo(const std::string & in_id="") : internal::WidgetInfo(in_id) { ; }
41  CanvasInfo(const CanvasInfo &) = delete; // No copies of INFO allowed
42  CanvasInfo & operator=(const CanvasInfo &) = delete; // No copies of INFO allowed
43  virtual ~CanvasInfo() { ClearActions(); }
44 
45  std::string TypeName() const override { return "CanvasInfo"; }
46 
47  virtual bool IsCanvasInfo() const override { return true; }
48 
49  virtual void GetHTML(std::stringstream & HTML) override {
50  HTML.str(""); // Clear the current text.
51  HTML << "<canvas id=\"" << id
52  << "\" width=\"" << width
53  << "\" height=\"" << height << "\">";
54  // @CAO We can include fallback content here for browsers that don't support canvas.
55  HTML << "</canvas>";
56  }
57 
58  // Setup a canvas to be drawn on.
59  void TargetCanvas() {
60  EM_ASM_ARGS({
61  var cname = Pointer_stringify($0);
62  var canvas = document.getElementById(cname);
63  emp_i.ctx = canvas.getContext('2d');
64  }, id.c_str());
65  }
66 
67  // Trigger any JS code needed on re-draw.
68  void TriggerJS() override {
69  if (state == Widget::ACTIVE) { // Only draw on active canvases
70  TargetCanvas(); // Prepare the canvas for drawing
71  for (auto & a : actions) a->Apply(); // Run all of the actions
72  }
73  }
74 
75  void AddAction(CanvasAction * new_action) {
76  if (state == Widget::ACTIVE) { // Only draw on active canvases
77  TargetCanvas(); // Prepare the canvas for drawing
78  new_action->Apply(); // Draw the current action
79  }
80  actions.push_back(new_action); // Store the current action.
81  }
82 
83  void ClearActions() {
84  for (auto * a : actions) delete a;
85  actions.resize(0);
86  }
87 
88 
89  public:
90  virtual std::string GetType() override { return "web::CanvasInfo"; }
91 
92  }; // End of ButtonInfo definition.
93 
94 
95  // Get a properly cast version of indo.
96  CanvasInfo * Info() { return (CanvasInfo *) info; }
97  const CanvasInfo * Info() const { return (CanvasInfo *) info; }
98 
99  Canvas(CanvasInfo * in_info) : WidgetFacet(in_info) { ; }
100 
101  public:
103  Canvas(double w, double h, const std::string & in_id="")
104  : WidgetFacet(in_id)
105  {
106  info = new CanvasInfo(in_id);
107  Info()->width = w;
108  Info()->height = h;
109  }
110 
112  Canvas(const Canvas & in) : WidgetFacet(in) { ; }
114  Canvas() { ; }
115  virtual ~Canvas() { ; }
116 
118 
119  double GetWidth() const { return Info()->width; }
120  double GetHeight() const { return Info()->height; }
121 
122  void SetWidth(double w) { Info()->width=w; }
123  void SetHeight(double h) { Info()->height=h; }
124 
126  void SetSize(double w, double h) { Info()->width=w; Info()->height=h; }
127 
130  template <typename... Ts>
131  Canvas & Circle(Point center, double _r, Ts &&... vals) {
132  Info()->AddAction( new CanvasCircle(emp::Circle(center, _r), std::forward<Ts>(vals)...) );
133  return *this;
134  }
135 
136  template <typename... Ts>
137  Canvas & Circle(double _x, double _y, double _r, Ts &&... vals) {
138  Info()->AddAction( new CanvasCircle(emp::Circle(_x, _y, _r), std::forward<Ts>(vals)...) );
139  return *this;
140  }
141 
144  template <typename... Ts>
145  Canvas & Rect(Point corner, double w, double h, Ts &&... vals) {
146  Info()->AddAction( new CanvasRect(corner, w, h, std::forward<Ts>(vals)...) );
147  return *this;
148  }
149 
150  template <typename... Ts>
151  Canvas & Rect(double x, double y, double w, double h, Ts &&... vals) {
152  Info()->AddAction( new CanvasRect(x, y, w, h, std::forward<Ts>(vals)...) );
153  return *this;
154  }
155 
157  template <typename... Ts>
158  Canvas & Image(const emp::RawImage & image, Point corner, Ts &&... vals) {
159  Info()->AddAction( new CanvasImage(image, corner, std::forward<Ts>(vals)...) );
160  return *this;
161  }
162 
163  template <typename... Ts>
164  Canvas & Image(const emp::RawImage & image, double x, double y, Ts &&... vals) {
165  Info()->AddAction( new CanvasImage(image, x, y, std::forward<Ts>(vals)...) );
166  return *this;
167  }
168 
169 
171  template <typename... Ts>
172  Canvas & Line(double x1, double y1, double x2, double y2, Ts &&... vals) {
173  Info()->AddAction( new CanvasLine(x1, y1, x2, y2, std::forward<Ts>(vals)...) );
174  return *this;
175  }
176 
177  template <typename... Ts>
178  Canvas & Line(emp::Point p1, emp::Point p2, Ts &&... vals) {
179  Info()->AddAction( new CanvasLine(p1, p2, std::forward<Ts>(vals)...) );
180  return *this;
181  }
182 
184  template <typename... Ts>
185  Canvas & MultiLine(emp::Point p1, const emp::vector<emp::Point> & points, Ts &&... vals) {
186  Info()->AddAction( new CanvasMultiLine(p1, points, std::forward<Ts>(vals)...) );
187  return *this;
188  }
189 
192  template <typename... Ts>
193  Canvas & Text(emp::Point p, Ts &&... vals) {
194  Info()->AddAction( new CanvasText(p, std::forward<Ts>(vals)...) );
195  return *this;
196  }
197 
198  template <typename... Ts>
199  Canvas & Text(double x, double y, Ts &&... vals) {
200  Info()->AddAction( new CanvasText(x, y, std::forward<Ts>(vals)...) );
201  return *this;
202  }
203 
206  template <typename... Ts>
207  Canvas & CenterText(emp::Point p, Ts &&... vals) {
208  auto * ctext = new CanvasText(p, std::forward<Ts>(vals)...);
209  ctext->Center();
210  Info()->AddAction( ctext );
211  return *this;
212  }
213 
214  template <typename... Ts>
215  Canvas & CenterText(double x, double y, Ts &&... vals) {
216  auto * ctext = new CanvasText({x, y}, std::forward<Ts>(vals)...);
217  ctext->Center();
218  Info()->AddAction( ctext );
219  return *this;
220  }
221 
223  Canvas & Font(const std::string font) {
224  Info()->AddAction( new CanvasFont(font) );
225  return *this;
226  }
227 
229  Canvas & Draw(const emp::Circle & circle,
230  const std::string & fc="", const std::string & lc="") {
231  Info()->AddAction( new CanvasCircle(circle, fc, lc) );
232  return *this;
233  }
234 
236  Canvas & Draw(const CanvasShape & shape) {
237  Info()->AddAction( shape.Clone() );
238  return *this;
239  }
240 
242  Canvas & StrokeColor(std::string c) {
243  Info()->AddAction( new CanvasStrokeColor(c) );
244  return *this;
245  }
246 
248  Canvas & Rotate(double angle) {
249  Info()->AddAction( new CanvasRotate(angle) );
250  return *this;
251  }
252 
254  Canvas & Clear() {
255  Info()->ClearActions();
256  Info()->AddAction( new CanvasClearRect({0,0}, GetWidth(), GetHeight()) );
257  return *this;
258  }
259 
261  Canvas & Clear(const std::string & bg_color) {
262  Info()->ClearActions();
263  Info()->AddAction( new CanvasClearRect({0,0}, GetWidth(), GetHeight()) );
264  Info()->AddAction( new CanvasRect({0,0}, GetWidth(), GetHeight(), bg_color, "") );
265  return *this;
266  }
267 
268  };
269 
270 
271 }
272 }
273 
274 #endif
Canvas & Text(double x, double y, Ts &&...vals)
Definition: Canvas.h:199
Define an arbitrary shape to draw on a canvas (base clase)
Definition: CanvasShape.h:31
void SetHeight(double h)
Set a new height for this Canvas.
Definition: Canvas.h:123
CanvasInfo(const std::string &in_id="")
Definition: Canvas.h:40
virtual bool IsCanvasInfo() const override
Definition: Canvas.h:47
Canvas & Rect(double x, double y, double w, double h, Ts &&...vals)
Definition: Canvas.h:151
Canvas & Clear()
Clear everything off of this canvas.
Definition: Canvas.h:254
Definition: Widget.h:206
Rotate the entire canvas for subsequent drawings.
Definition: CanvasAction.h:90
Canvas & Circle(double _x, double _y, double _r, Ts &&...vals)
Definition: Canvas.h:137
A line segment on the canvas.
Definition: CanvasShape.h:170
Canvas & Line(emp::Point p1, emp::Point p2, Ts &&...vals)
Definition: Canvas.h:178
const CanvasInfo * Info() const
Definition: Canvas.h:97
Clear a rectangular area in a canvas.
Definition: CanvasShape.h:117
Definition: CanvasShape.h:197
WidgetInfo * info
Information associated with this widget.
Definition: Widget.h:82
WidgetFacet is a template that provides accessors into Widget with a derived return type...
Definition: Widget.h:543
Canvas & Circle(Point center, double _r, Ts &&...vals)
Definition: Canvas.h:131
virtual void Apply()=0
Apply current action to emp_i.ctx.
Define a base class for all actions that can be done to widgets, plus simple actions.
void AddAction(CanvasAction *new_action)
Definition: Canvas.h:75
virtual void GetHTML(std::stringstream &HTML) override
Definition: Canvas.h:49
void push_back(PB_Ts &&...args)
Definition: vector.h:189
Definition: Widget.h:102
Canvas & Font(const std::string font)
Update the default font for text.
Definition: Canvas.h:223
Canvas & CenterText(double x, double y, Ts &&...vals)
Definition: Canvas.h:215
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
Change the default font to be used.
Definition: CanvasAction.h:106
Widget::ActivityState state
Is this element active in DOM?
Definition: Widget.h:218
Canvas & Image(const emp::RawImage &image, Point corner, Ts &&...vals)
Add an Image to this canvas at x,y with width w and heigh h.
Definition: Canvas.h:158
virtual ~CanvasInfo()
Definition: Canvas.h:43
virtual bool IsCanvasInfo() const
Definition: Widget.h:241
Canvas(const Widget &in)
Definition: Canvas.h:113
CanvasInfo * Info()
Definition: Canvas.h:96
Canvas()
Definition: Canvas.h:114
double height
pixel height of the canvas.
Definition: Canvas.h:36
Canvas & MultiLine(emp::Point p1, const emp::vector< emp::Point > &points, Ts &&...vals)
Add a Line from x1,y1 to x2,y2. Optional face color and line color.
Definition: Canvas.h:185
Manage an HTML Canvas object.
Definition: Canvas.h:27
Canvas(double w, double h, const std::string &in_id="")
Create a new canvas with the specified size and optional HTML identifier.
Definition: Canvas.h:103
Base class to maintain canvas actions.
Definition: CanvasAction.h:33
void TargetCanvas()
Definition: Canvas.h:59
Canvas(CanvasInfo *in_info)
Definition: Canvas.h:99
If we are in emscripten, make sure to include the header.
Definition: array.h:37
Change the default font to be used.
Definition: CanvasAction.h:121
Build a debug wrapper emp::vector around std::vector.
Definition: vector.h:42
Widget is effectively a smart pointer to a WidgetInfo object, plus some basic accessors.
Definition: Widget.h:78
Canvas & Clear(const std::string &bg_color)
Clear to a specific background color.
Definition: Canvas.h:261
double width
pixel width of the canvas.
Definition: Canvas.h:35
Definition: Canvas.h:31
Canvas & Text(emp::Point p, Ts &&...vals)
Definition: Canvas.h:193
Canvas & StrokeColor(std::string c)
Change the default stroke color.
Definition: Canvas.h:242
#define emp_assert(...)
Definition: assert.h:199
double GetWidth() const
Get the pixel width of this Canvas.
Definition: Canvas.h:119
virtual CanvasAction * Clone() const =0
Make a copy of the current action.
Canvas & Draw(const CanvasShape &shape)
Draw an arbitrary shape onto this canvas.
Definition: Canvas.h:236
void SetWidth(double w)
Set a new width for this Canvas.
Definition: Canvas.h:122
Text to be written on a canvas.
Definition: CanvasShape.h:227
Define simple shapes to draw on a canvas.
virtual std::string GetType() override
Definition: Canvas.h:90
void SetSize(double w, double h)
Set Canvas size.
Definition: Canvas.h:126
emp::vector< CanvasAction * > actions
Definition: Canvas.h:38
Canvas(const Canvas &in)
Link to an existing canvas.
Definition: Canvas.h:112
Set the line color on subsequent draw-related actions.
Definition: CanvasAction.h:74
Fundamental information about a single image.
Definition: RawImage.h:124
void TriggerJS() override
Definition: Canvas.h:68
std::string TypeName() const override
Debugging helpers...
Definition: Canvas.h:45
double GetHeight() const
Get the pixel height of this Canvas.
Definition: Canvas.h:120
Canvas & Image(const emp::RawImage &image, double x, double y, Ts &&...vals)
Definition: Canvas.h:164
Canvas & Draw(const emp::Circle &circle, const std::string &fc="", const std::string &lc="")
Draw a circle onto this canvas.
Definition: Canvas.h:229
CanvasInfo & operator=(const CanvasInfo &)=delete
Track a circle shape to be drawn on a canvas.
Definition: CanvasShape.h:69
Canvas & CenterText(emp::Point p, Ts &&...vals)
Definition: Canvas.h:207
WidgetFacet(const std::string &in_id="")
WidgetFacet cannot be built unless within derived class, so constructors are protected.
Definition: Widget.h:546
void ClearActions()
Definition: Canvas.h:83
Canvas & Rotate(double angle)
Rotate the entire canvas.
Definition: Canvas.h:248
Canvas & Rect(Point corner, double w, double h, Ts &&...vals)
Definition: Canvas.h:145
virtual ~Canvas()
Definition: Canvas.h:115
Track a rectangle shape to be drawn on a canvas.
Definition: CanvasShape.h:94