/src/serenity/Userland/Libraries/LibWeb/Painting/CanvasPaintable.cpp
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2022, Andreas Kling <kling@serenityos.org> |
3 | | * |
4 | | * SPDX-License-Identifier: BSD-2-Clause |
5 | | */ |
6 | | |
7 | | #include <LibWeb/Painting/CanvasPaintable.h> |
8 | | |
9 | | namespace Web::Painting { |
10 | | |
11 | | JS_DEFINE_ALLOCATOR(CanvasPaintable); |
12 | | |
13 | | JS::NonnullGCPtr<CanvasPaintable> CanvasPaintable::create(Layout::CanvasBox const& layout_box) |
14 | 0 | { |
15 | 0 | return layout_box.heap().allocate_without_realm<CanvasPaintable>(layout_box); |
16 | 0 | } |
17 | | |
18 | | CanvasPaintable::CanvasPaintable(Layout::CanvasBox const& layout_box) |
19 | 0 | : PaintableBox(layout_box) |
20 | 0 | { |
21 | 0 | } |
22 | | |
23 | | Layout::CanvasBox const& CanvasPaintable::layout_box() const |
24 | 0 | { |
25 | 0 | return static_cast<Layout::CanvasBox const&>(layout_node()); |
26 | 0 | } |
27 | | |
28 | | void CanvasPaintable::paint(PaintContext& context, PaintPhase phase) const |
29 | 0 | { |
30 | 0 | if (!is_visible()) |
31 | 0 | return; |
32 | | |
33 | 0 | PaintableBox::paint(context, phase); |
34 | |
|
35 | 0 | if (phase == PaintPhase::Foreground) { |
36 | 0 | auto canvas_rect = context.rounded_device_rect(absolute_rect()); |
37 | 0 | ScopedCornerRadiusClip corner_clip { context, canvas_rect, normalized_border_radii_data(ShrinkRadiiForBorders::Yes) }; |
38 | |
|
39 | 0 | if (layout_box().dom_node().bitmap()) { |
40 | | // FIXME: Remove this const_cast. |
41 | 0 | const_cast<HTML::HTMLCanvasElement&>(layout_box().dom_node()).present(); |
42 | 0 | auto scaling_mode = to_gfx_scaling_mode(computed_values().image_rendering(), layout_box().dom_node().bitmap()->rect(), canvas_rect.to_type<int>()); |
43 | 0 | context.display_list_recorder().draw_scaled_bitmap(canvas_rect.to_type<int>(), *layout_box().dom_node().bitmap(), layout_box().dom_node().bitmap()->rect(), scaling_mode); |
44 | 0 | } |
45 | 0 | } |
46 | 0 | } |
47 | | |
48 | | } |