HTMLCanvasElement.h 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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(String 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. String to_data_url(StringView type, Optional<double> quality) const;
  26. WebIDL::ExceptionOr<void> to_blob(JS::NonnullGCPtr<WebIDL::CallbackType> callback, StringView 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 void apply_presentational_hints(CSS::StyleProperties&) const override;
  33. virtual JS::GCPtr<Layout::Node> create_layout_node(NonnullRefPtr<CSS::StyleProperties>) override;
  34. enum class HasOrCreatedContext {
  35. No,
  36. Yes,
  37. };
  38. HasOrCreatedContext create_2d_context();
  39. JS::ThrowCompletionOr<HasOrCreatedContext> create_webgl_context(JS::Value options);
  40. void reset_context_to_default_state();
  41. RefPtr<Gfx::Bitmap> m_bitmap;
  42. Variant<JS::NonnullGCPtr<HTML::CanvasRenderingContext2D>, JS::NonnullGCPtr<WebGL::WebGLRenderingContext>, Empty> m_context;
  43. };
  44. }