CanvasRenderingContext2D.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #pragma once
  27. #include <AK/RefCounted.h>
  28. #include <LibGfx/AffineTransform.h>
  29. #include <LibGfx/Color.h>
  30. #include <LibGfx/Forward.h>
  31. #include <LibGfx/Painter.h>
  32. #include <LibGfx/Path.h>
  33. #include <LibWeb/Bindings/Wrappable.h>
  34. namespace Web {
  35. class CanvasRenderingContext2D
  36. : public RefCounted<CanvasRenderingContext2D>
  37. , public Bindings::Wrappable {
  38. AK_MAKE_NONCOPYABLE(CanvasRenderingContext2D);
  39. AK_MAKE_NONMOVABLE(CanvasRenderingContext2D);
  40. public:
  41. using WrapperType = Bindings::CanvasRenderingContext2DWrapper;
  42. static NonnullRefPtr<CanvasRenderingContext2D> create(HTMLCanvasElement& element) { return adopt(*new CanvasRenderingContext2D(element)); }
  43. ~CanvasRenderingContext2D();
  44. void set_fill_style(String);
  45. String fill_style() const;
  46. void set_stroke_style(String);
  47. String stroke_style() const;
  48. void fill_rect(float x, float y, float width, float height);
  49. void stroke_rect(float x, float y, float width, float height);
  50. void draw_image(const HTMLImageElement&, float x, float y);
  51. void scale(float sx, float sy);
  52. void translate(float x, float y);
  53. void set_line_width(float line_width) { m_line_width = line_width; }
  54. float line_width() const { return m_line_width; }
  55. void begin_path();
  56. void close_path();
  57. void move_to(float x, float y);
  58. void line_to(float x, float y);
  59. void quadratic_curve_to(float cx, float cy, float x, float y);
  60. void stroke();
  61. void fill(Gfx::Painter::WindingRule);
  62. RefPtr<ImageData> create_image_data(JS::GlobalObject&, int width, int height) const;
  63. void put_image_data(const ImageData&, float x, float y);
  64. HTMLCanvasElement* element() { return m_element; }
  65. private:
  66. explicit CanvasRenderingContext2D(HTMLCanvasElement&);
  67. void did_draw(const Gfx::FloatRect&);
  68. OwnPtr<Gfx::Painter> painter();
  69. WeakPtr<HTMLCanvasElement> m_element;
  70. Gfx::AffineTransform m_transform;
  71. Gfx::Color m_fill_style;
  72. Gfx::Color m_stroke_style;
  73. float m_line_width { 1 };
  74. Gfx::Path m_path;
  75. };
  76. }