HTMLCanvasElement.h 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <andreas@ladybird.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/ByteBuffer.h>
  8. #include <LibGfx/Forward.h>
  9. #include <LibGfx/PaintingSurface.h>
  10. #include <LibWeb/HTML/HTMLElement.h>
  11. #include <LibWeb/WebIDL/Types.h>
  12. namespace Web::HTML {
  13. class HTMLCanvasElement final : public HTMLElement {
  14. WEB_PLATFORM_OBJECT(HTMLCanvasElement, HTMLElement);
  15. GC_DECLARE_ALLOCATOR(HTMLCanvasElement);
  16. public:
  17. using RenderingContext = Variant<GC::Root<CanvasRenderingContext2D>, GC::Root<WebGL::WebGLRenderingContext>, GC::Root<WebGL::WebGL2RenderingContext>, Empty>;
  18. virtual ~HTMLCanvasElement() override;
  19. Gfx::IntSize bitmap_size_for_canvas(size_t minimum_width = 0, size_t minimum_height = 0) const;
  20. JS::ThrowCompletionOr<RenderingContext> get_context(String const& type, JS::Value options);
  21. enum class HasOrCreatedContext {
  22. No,
  23. Yes,
  24. };
  25. HasOrCreatedContext create_2d_context();
  26. WebIDL::UnsignedLong width() const;
  27. WebIDL::UnsignedLong height() const;
  28. WebIDL::ExceptionOr<void> set_width(WebIDL::UnsignedLong);
  29. WebIDL::ExceptionOr<void> set_height(WebIDL::UnsignedLong);
  30. String to_data_url(StringView type, JS::Value quality);
  31. WebIDL::ExceptionOr<void> to_blob(GC::Ref<WebIDL::CallbackType> callback, StringView type, JS::Value quality);
  32. void present();
  33. RefPtr<Gfx::PaintingSurface> surface() const;
  34. void allocate_painting_surface_if_needed();
  35. private:
  36. HTMLCanvasElement(DOM::Document&, DOM::QualifiedName);
  37. virtual void initialize(JS::Realm&) override;
  38. virtual void visit_edges(Cell::Visitor&) override;
  39. virtual void apply_presentational_hints(CSS::StyleProperties&) const override;
  40. virtual GC::Ptr<Layout::Node> create_layout_node(CSS::StyleProperties) override;
  41. virtual void adjust_computed_style(CSS::StyleProperties&) override;
  42. template<typename ContextType>
  43. JS::ThrowCompletionOr<HasOrCreatedContext> create_webgl_context(JS::Value options);
  44. void reset_context_to_default_state();
  45. void notify_context_about_canvas_size_change();
  46. Variant<GC::Ref<HTML::CanvasRenderingContext2D>, GC::Ref<WebGL::WebGLRenderingContext>, GC::Ref<WebGL::WebGL2RenderingContext>, Empty> m_context;
  47. };
  48. }