CanvasRenderingContext2D.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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_drawing_state.line_width = line_width; }
  37. float line_width() const { return m_drawing_state.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. void save();
  54. void restore();
  55. HTMLCanvasElement* canvas() { return m_element; }
  56. private:
  57. explicit CanvasRenderingContext2D(HTMLCanvasElement&);
  58. void did_draw(const Gfx::FloatRect&);
  59. OwnPtr<Gfx::Painter> painter();
  60. WeakPtr<HTMLCanvasElement> m_element;
  61. // https://html.spec.whatwg.org/multipage/canvas.html#drawing-state
  62. struct DrawingState {
  63. Gfx::AffineTransform transform;
  64. Gfx::Color fill_style { Gfx::Color::Black };
  65. Gfx::Color stroke_style { Gfx::Color::Black };
  66. float line_width { 1 };
  67. };
  68. DrawingState m_drawing_state;
  69. Vector<DrawingState> m_drawing_state_stack;
  70. Gfx::Path m_path;
  71. };
  72. }