HTMLCanvasElement.h 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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/ByteBuffer.h>
  8. #include <LibGfx/Forward.h>
  9. #include <LibWeb/HTML/HTMLElement.h>
  10. #include <LibWeb/WebGL/WebGLRenderingContext.h>
  11. namespace Web::HTML {
  12. class HTMLCanvasElement final : public HTMLElement {
  13. WEB_PLATFORM_OBJECT(HTMLCanvasElement, HTMLElement);
  14. public:
  15. using RenderingContext = Variant<JS::Handle<CanvasRenderingContext2D>, JS::Handle<WebGL::WebGLRenderingContext>, Empty>;
  16. virtual ~HTMLCanvasElement() override;
  17. Gfx::Bitmap const* bitmap() const { return m_bitmap; }
  18. Gfx::Bitmap* bitmap() { return m_bitmap; }
  19. bool create_bitmap(size_t minimum_width = 0, size_t minimum_height = 0);
  20. JS::ThrowCompletionOr<RenderingContext> get_context(DeprecatedString const& type, JS::Value options);
  21. unsigned width() const;
  22. unsigned height() const;
  23. WebIDL::ExceptionOr<void> set_width(unsigned);
  24. WebIDL::ExceptionOr<void> set_height(unsigned);
  25. DeprecatedString to_data_url(DeprecatedString const& type, Optional<double> quality) const;
  26. WebIDL::ExceptionOr<void> to_blob(JS::NonnullGCPtr<WebIDL::CallbackType> callback, DeprecatedString const& type, Optional<double> quality);
  27. void present();
  28. private:
  29. HTMLCanvasElement(DOM::Document&, DOM::QualifiedName);
  30. virtual void initialize(JS::Realm&) override;
  31. virtual void visit_edges(Cell::Visitor&) override;
  32. virtual JS::GCPtr<Layout::Node> create_layout_node(NonnullRefPtr<CSS::StyleProperties>) override;
  33. enum class HasOrCreatedContext {
  34. No,
  35. Yes,
  36. };
  37. HasOrCreatedContext create_2d_context();
  38. JS::ThrowCompletionOr<HasOrCreatedContext> create_webgl_context(JS::Value options);
  39. void reset_context_to_default_state();
  40. RefPtr<Gfx::Bitmap> m_bitmap;
  41. Variant<JS::NonnullGCPtr<HTML::CanvasRenderingContext2D>, JS::NonnullGCPtr<WebGL::WebGLRenderingContext>, Empty> m_context;
  42. };
  43. }