/src/serenity/Userland/Libraries/LibWeb/HTML/HTMLCanvasElement.h
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2020, Andreas Kling <kling@serenityos.org> |
3 | | * |
4 | | * SPDX-License-Identifier: BSD-2-Clause |
5 | | */ |
6 | | |
7 | | #pragma once |
8 | | |
9 | | #include <AK/ByteBuffer.h> |
10 | | #include <LibGfx/Forward.h> |
11 | | #include <LibWeb/HTML/HTMLElement.h> |
12 | | #include <LibWeb/WebGL/WebGLRenderingContext.h> |
13 | | |
14 | | namespace Web::HTML { |
15 | | |
16 | | class HTMLCanvasElement final : public HTMLElement { |
17 | | WEB_PLATFORM_OBJECT(HTMLCanvasElement, HTMLElement); |
18 | | JS_DECLARE_ALLOCATOR(HTMLCanvasElement); |
19 | | |
20 | | public: |
21 | | using RenderingContext = Variant<JS::Handle<CanvasRenderingContext2D>, JS::Handle<WebGL::WebGLRenderingContext>, Empty>; |
22 | | |
23 | | virtual ~HTMLCanvasElement() override; |
24 | | |
25 | 0 | Gfx::Bitmap const* bitmap() const { return m_bitmap; } |
26 | 0 | Gfx::Bitmap* bitmap() { return m_bitmap; } |
27 | | bool create_bitmap(size_t minimum_width = 0, size_t minimum_height = 0); |
28 | | |
29 | | JS::ThrowCompletionOr<RenderingContext> get_context(String const& type, JS::Value options); |
30 | | |
31 | | unsigned width() const; |
32 | | unsigned height() const; |
33 | | |
34 | | WebIDL::ExceptionOr<void> set_width(unsigned); |
35 | | WebIDL::ExceptionOr<void> set_height(unsigned); |
36 | | |
37 | | String to_data_url(StringView type, Optional<double> quality); |
38 | | WebIDL::ExceptionOr<void> to_blob(JS::NonnullGCPtr<WebIDL::CallbackType> callback, StringView type, Optional<double> quality); |
39 | | |
40 | | void present(); |
41 | | |
42 | | private: |
43 | | HTMLCanvasElement(DOM::Document&, DOM::QualifiedName); |
44 | | |
45 | | virtual void initialize(JS::Realm&) override; |
46 | | virtual void visit_edges(Cell::Visitor&) override; |
47 | | |
48 | | virtual void apply_presentational_hints(CSS::StyleProperties&) const override; |
49 | | |
50 | | virtual JS::GCPtr<Layout::Node> create_layout_node(NonnullRefPtr<CSS::StyleProperties>) override; |
51 | | |
52 | | enum class HasOrCreatedContext { |
53 | | No, |
54 | | Yes, |
55 | | }; |
56 | | |
57 | | HasOrCreatedContext create_2d_context(); |
58 | | JS::ThrowCompletionOr<HasOrCreatedContext> create_webgl_context(JS::Value options); |
59 | | void reset_context_to_default_state(); |
60 | | |
61 | | RefPtr<Gfx::Bitmap> m_bitmap; |
62 | | |
63 | | Variant<JS::NonnullGCPtr<HTML::CanvasRenderingContext2D>, JS::NonnullGCPtr<WebGL::WebGLRenderingContext>, Empty> m_context; |
64 | | }; |
65 | | |
66 | | } |