HTMLCanvasElement.h 2.1 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/WebGL/WebGLRenderingContext.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>, Empty>;
  18. virtual ~HTMLCanvasElement() override;
  19. bool allocate_painting_surface(size_t minimum_width = 0, size_t minimum_height = 0);
  20. RefPtr<Gfx::PaintingSurface> surface() { return m_surface; }
  21. RefPtr<Gfx::PaintingSurface const> surface() const { return m_surface; }
  22. JS::ThrowCompletionOr<RenderingContext> get_context(String const& type, JS::Value options);
  23. unsigned width() const;
  24. unsigned height() const;
  25. WebIDL::ExceptionOr<void> set_width(unsigned);
  26. WebIDL::ExceptionOr<void> set_height(unsigned);
  27. String to_data_url(StringView type, JS::Value quality);
  28. WebIDL::ExceptionOr<void> to_blob(GC::Ref<WebIDL::CallbackType> callback, StringView type, JS::Value quality);
  29. void present();
  30. private:
  31. HTMLCanvasElement(DOM::Document&, DOM::QualifiedName);
  32. virtual void initialize(JS::Realm&) override;
  33. virtual void visit_edges(Cell::Visitor&) override;
  34. virtual void apply_presentational_hints(CSS::StyleProperties&) const override;
  35. virtual GC::Ptr<Layout::Node> create_layout_node(CSS::StyleProperties) override;
  36. virtual void adjust_computed_style(CSS::StyleProperties&) override;
  37. enum class HasOrCreatedContext {
  38. No,
  39. Yes,
  40. };
  41. HasOrCreatedContext create_2d_context();
  42. JS::ThrowCompletionOr<HasOrCreatedContext> create_webgl_context(JS::Value options);
  43. void reset_context_to_default_state();
  44. RefPtr<Gfx::PaintingSurface> m_surface;
  45. Variant<GC::Ref<HTML::CanvasRenderingContext2D>, GC::Ref<WebGL::WebGLRenderingContext>, Empty> m_context;
  46. };
  47. }