HTMLCanvasElement.h 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. JS_DECLARE_ALLOCATOR(HTMLCanvasElement);
  15. public:
  16. using RenderingContext = Variant<JS::Handle<CanvasRenderingContext2D>, JS::Handle<WebGL::WebGLRenderingContext>, Empty>;
  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. WebIDL::ExceptionOr<void> set_width(unsigned);
  25. WebIDL::ExceptionOr<void> set_height(unsigned);
  26. String to_data_url(StringView type, Optional<double> quality);
  27. WebIDL::ExceptionOr<void> to_blob(JS::NonnullGCPtr<WebIDL::CallbackType> callback, StringView type, Optional<double> quality);
  28. void present();
  29. private:
  30. HTMLCanvasElement(DOM::Document&, DOM::QualifiedName);
  31. virtual void initialize(JS::Realm&) override;
  32. virtual void visit_edges(Cell::Visitor&) override;
  33. virtual void apply_presentational_hints(CSS::StyleProperties&) const override;
  34. virtual JS::GCPtr<Layout::Node> create_layout_node(NonnullRefPtr<CSS::StyleProperties>) override;
  35. enum class HasOrCreatedContext {
  36. No,
  37. Yes,
  38. };
  39. HasOrCreatedContext create_2d_context();
  40. JS::ThrowCompletionOr<HasOrCreatedContext> create_webgl_context(JS::Value options);
  41. void reset_context_to_default_state();
  42. RefPtr<Gfx::Bitmap> m_bitmap;
  43. Variant<JS::NonnullGCPtr<HTML::CanvasRenderingContext2D>, JS::NonnullGCPtr<WebGL::WebGLRenderingContext>, Empty> m_context;
  44. };
  45. }