Empirical
CanvasAction.h
Go to the documentation of this file.
1 
21 #ifndef EMP_WEB_CANVAS_ACTION_H
22 #define EMP_WEB_CANVAS_ACTION_H
23 
24 #include <string>
25 
26 #include "RawImage.h"
27 #include "Widget.h"
28 
29 namespace emp {
30 namespace web {
31 
33  class CanvasAction {
34  protected:
36  void Fill(const std::string & style="") {
37  if (style != "") {
38  EM_ASM_ARGS({
39  emp_i.ctx.fillStyle = Pointer_stringify($0);
40  }, style.c_str());
41  }
42  EM_ASM({ emp_i.ctx.fill(); });
43  }
44 
46  void Stroke(const std::string & style="") {
47  if (style != "") {
48  EM_ASM_ARGS({
49  emp_i.ctx.strokeStyle = Pointer_stringify($0);
50  }, style.c_str());
51  }
52  EM_ASM({ emp_i.ctx.stroke(); });
53  }
54 
56  void LineWidth(double line_width=1.0) {
57  EM_ASM_ARGS({
58  emp_i.ctx.lineWidth = $0;
59  }, line_width);
60  }
61 
62  public:
63  CanvasAction() { EMP_TRACK_CONSTRUCT(CanvasAction); }
64  CanvasAction(const CanvasAction &) { EMP_TRACK_CONSTRUCT(CanvasAction); }
65  virtual ~CanvasAction() { EMP_TRACK_DESTRUCT(CanvasAction); }
66 
67 
68  virtual void Apply() = 0;
69  virtual CanvasAction * Clone() const = 0;
70  };
71 
72 
75  protected:
76  std::string color;
77  public:
78  CanvasStrokeColor(const std::string & c) : color(c) { ; }
79 
80  void Apply() {
81  EM_ASM_ARGS({
82  var color = Pointer_stringify($0);
83  emp_i.ctx.strokeStyle = color;
84  }, color.c_str());
85  }
86  CanvasAction * Clone() const { return new CanvasStrokeColor(*this); }
87  };
88 
90  class CanvasRotate : public CanvasAction {
91  protected:
92  double angle;
93  public:
94  CanvasRotate(double a) : angle(a) { ; }
95 
96  void Apply() {
97  EM_ASM_ARGS({
98  emp_i.ctx.rotate($0);
99  }, angle);
100  }
101  CanvasAction * Clone() const { return new CanvasRotate(*this); }
102  };
103 
104 
106  class CanvasFont : public CanvasAction {
107  protected:
108  std::string font;
109  public:
110  CanvasFont(const std::string & f) : font(f) { ; }
111 
112  void Apply() {
113  EM_ASM_ARGS({
114  emp_i.ctx.font = Pointer_stringify($0);
115  }, font.c_str());
116  }
117  CanvasAction * Clone() const { return new CanvasFont(*this); }
118  };
119 
121  class CanvasImage : public CanvasAction {
122  protected:
125  double width; double height;
126  public:
127  CanvasImage(const RawImage & raw_image, double _x=0.0, double _y=0.0, double _w=0.0, double _h=0.0)
128  : image(raw_image), position(_x,_y), width(_w), height(_h) { ; }
129  CanvasImage(const RawImage & raw_image, Point _p, double _w=0.0, double _h=0.0)
130  : image(raw_image), position(_p), width(_w), height(_h) { ; }
131  CanvasImage(const std::string & url, double _x=0.0, double _y=0.0, double _w=0.0, double _h=0.0)
132  : image(url), position(_x,_y), width(_w), height(_h) { ; }
133  CanvasImage(const std::string & url, Point _p, double _w=0.0, double _h=0.0)
134  : image(url), position(_p), width(_w), height(_h) { ; }
135 
136  void Apply() {
137  image.OnLoad([this](){
138  if (width == 0.0) {
139  EM_ASM_ARGS({
140  emp_i.ctx.drawImage(emp_i.images[$0], $1, $2);
141  }, image.GetID(), position.GetX(), position.GetY());
142  } else {
143  EM_ASM_ARGS({
144  emp_i.ctx.drawImage(emp_i.images[$0], $1, $2, $3, $4);
145  }, image.GetID(), position.GetX(), position.GetY(), width, height);
146  }
147  });
148  }
149 
150  CanvasAction * Clone() const { return new CanvasImage(*this); }
151  };
152 
153 }
154 }
155 
156 #endif
Rotate the entire canvas for subsequent drawings.
Definition: CanvasAction.h:90
int GetID() const
Definition: RawImage.h:139
constexpr TYPE GetX() const
Definition: Point2D.h:39
void Apply()
Apply current action to emp_i.ctx.
Definition: CanvasAction.h:136
virtual void Apply()=0
Apply current action to emp_i.ctx.
CanvasAction * Clone() const
Make a copy of the current action.
Definition: CanvasAction.h:150
CanvasRotate(double a)
Definition: CanvasAction.h:94
CanvasAction * Clone() const
Make a copy of the current action.
Definition: CanvasAction.h:117
CanvasAction()
Definition: CanvasAction.h:63
CanvasImage(const std::string &url, double _x=0.0, double _y=0.0, double _w=0.0, double _h=0.0)
Definition: CanvasAction.h:131
Change the default font to be used.
Definition: CanvasAction.h:106
CanvasImage(const RawImage &raw_image, double _x=0.0, double _y=0.0, double _w=0.0, double _h=0.0)
Definition: CanvasAction.h:127
CanvasStrokeColor(const std::string &c)
Definition: CanvasAction.h:78
void Apply()
Apply current action to emp_i.ctx.
Definition: CanvasAction.h:80
virtual ~CanvasAction()
Definition: CanvasAction.h:65
void Apply()
Apply current action to emp_i.ctx.
Definition: CanvasAction.h:96
std::string color
Color to use.
Definition: CanvasAction.h:76
void LineWidth(double line_width=1.0)
Helper function to set the stroke status.
Definition: CanvasAction.h:56
Handle the fundamental loading of an image (without Widget tracking)
CanvasImage(const RawImage &raw_image, Point _p, double _w=0.0, double _h=0.0)
Definition: CanvasAction.h:129
Widgets maintain individual components on a web page and link to Elements.
Base class to maintain canvas actions.
Definition: CanvasAction.h:33
RawImage image
Definition: CanvasAction.h:123
void Apply()
Apply current action to emp_i.ctx.
Definition: CanvasAction.h:112
CanvasFont(const std::string &f)
Definition: CanvasAction.h:110
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
std::string font
Definition: CanvasAction.h:108
virtual CanvasAction * Clone() const =0
Make a copy of the current action.
Set the line color on subsequent draw-related actions.
Definition: CanvasAction.h:74
Fundamental information about a single image.
Definition: RawImage.h:124
CanvasAction(const CanvasAction &)
Definition: CanvasAction.h:64
Point position
Definition: CanvasAction.h:124
double width
Definition: CanvasAction.h:125
CanvasImage(const std::string &url, Point _p, double _w=0.0, double _h=0.0)
Definition: CanvasAction.h:133
CanvasAction * Clone() const
Make a copy of the current action.
Definition: CanvasAction.h:86
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
CanvasAction * Clone() const
Make a copy of the current action.
Definition: CanvasAction.h:101
double angle
Definition: CanvasAction.h:92
void OnLoad(const std::function< void()> &callback_fun)
Add a new function to be called when the image finishes loading.
Definition: RawImage.h:145
void Stroke(const std::string &style="")
Helper function to set the stroke status.
Definition: CanvasAction.h:46