CanvasRenderingContext2D.h 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/RefCounted.h>
  8. #include <LibGfx/AffineTransform.h>
  9. #include <LibGfx/Color.h>
  10. #include <LibGfx/Forward.h>
  11. #include <LibGfx/Painter.h>
  12. #include <LibGfx/Path.h>
  13. #include <LibWeb/Bindings/Wrappable.h>
  14. #include <LibWeb/DOM/ExceptionOr.h>
  15. namespace Web::HTML {
  16. class CanvasRenderingContext2D
  17. : public RefCounted<CanvasRenderingContext2D>
  18. , public Bindings::Wrappable {
  19. AK_MAKE_NONCOPYABLE(CanvasRenderingContext2D);
  20. AK_MAKE_NONMOVABLE(CanvasRenderingContext2D);
  21. public:
  22. using WrapperType = Bindings::CanvasRenderingContext2DWrapper;
  23. static NonnullRefPtr<CanvasRenderingContext2D> create(HTMLCanvasElement& element) { return adopt_ref(*new CanvasRenderingContext2D(element)); }
  24. ~CanvasRenderingContext2D();
  25. void set_fill_style(String);
  26. String fill_style() const;
  27. void set_stroke_style(String);
  28. String stroke_style() const;
  29. void fill_rect(float x, float y, float width, float height);
  30. void stroke_rect(float x, float y, float width, float height);
  31. void clear_rect(float x, float y, float width, float height);
  32. void draw_image(const HTMLImageElement&, float x, float y);
  33. void scale(float sx, float sy);
  34. void translate(float x, float y);
  35. void rotate(float degrees);
  36. void set_line_width(float line_width) { m_line_width = line_width; }
  37. float line_width() const { return m_line_width; }
  38. void begin_path();
  39. void close_path();
  40. void move_to(float x, float y);
  41. void line_to(float x, float y);
  42. void quadratic_curve_to(float cx, float cy, float x, float y);
  43. DOM::ExceptionOr<void> arc(float x, float y, float radius, float start_angle, float end_angle, bool counter_clockwise);
  44. DOM::ExceptionOr<void> ellipse(float x, float y, float radius_x, float radius_y, float rotation, float start_angle, float end_angle, bool counter_clockwise);
  45. void rect(float x, float y, float width, float height);
  46. void stroke();
  47. void fill_text(const String&, float x, float y, Optional<double> max_width);
  48. // FIXME: We should only have one fill(), really. Fix the wrapper generator!
  49. void fill(Gfx::Painter::WindingRule);
  50. void fill(const String& fill_rule);
  51. RefPtr<ImageData> create_image_data(int width, int height) const;
  52. void put_image_data(const ImageData&, float x, float y);
  53. HTMLCanvasElement* canvas() { return m_element; }
  54. private:
  55. explicit CanvasRenderingContext2D(HTMLCanvasElement&);
  56. void did_draw(const Gfx::FloatRect&);
  57. OwnPtr<Gfx::Painter> painter();
  58. WeakPtr<HTMLCanvasElement> m_element;
  59. Gfx::AffineTransform m_transform;
  60. Gfx::Color m_fill_style;
  61. Gfx::Color m_stroke_style;
  62. float m_line_width { 1 };
  63. Gfx::Path m_path;
  64. };
  65. }