HTMLCanvasElement.h 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. public:
  14. using WrapperType = Bindings::HTMLCanvasElementWrapper;
  15. using RenderingContext = Variant<NonnullRefPtr<CanvasRenderingContext2D>, NonnullRefPtr<WebGL::WebGLRenderingContext>, Empty>;
  16. HTMLCanvasElement(DOM::Document&, DOM::QualifiedName);
  17. virtual ~HTMLCanvasElement() override;
  18. Gfx::Bitmap const* bitmap() const { return m_bitmap; }
  19. Gfx::Bitmap* bitmap() { return m_bitmap; }
  20. bool create_bitmap(size_t minimum_width = 0, size_t minimum_height = 0);
  21. JS::ThrowCompletionOr<RenderingContext> get_context(String const& type, JS::Value options);
  22. unsigned width() const;
  23. unsigned height() const;
  24. void set_width(unsigned);
  25. void set_height(unsigned);
  26. String to_data_url(String const& type, Optional<double> quality) const;
  27. void present();
  28. private:
  29. virtual RefPtr<Layout::Node> create_layout_node(NonnullRefPtr<CSS::StyleProperties>) override;
  30. enum class HasOrCreatedContext {
  31. No,
  32. Yes,
  33. };
  34. HasOrCreatedContext create_2d_context();
  35. JS::ThrowCompletionOr<HasOrCreatedContext> create_webgl_context(JS::Value options);
  36. void reset_context_to_default_state();
  37. RefPtr<Gfx::Bitmap> m_bitmap;
  38. RenderingContext m_context;
  39. };
  40. }