1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 22:48:11 +00:00

LibWeb: Implement CanvasRenderingContext2D.drawImage() closer to spec

Also add support for HTMLCanvasElement for the image parameter, leading
to replacing HTMLImageElement with the CanvasImageSource Variant type.

Also stub out the 'check the usability of the image argument' and 'is
not origin-clean' operations, while taking into consideration that these
can throw (and require DOM::ExceptionOr).
This commit is contained in:
Linus Groh 2022-03-04 19:02:10 +01:00 committed by Andreas Kling
parent ed03f37ae9
commit 3f3326f1dc
3 changed files with 118 additions and 10 deletions

View file

@ -1,5 +1,6 @@
/*
* Copyright (c) 2020-2022, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2021-2022, Linus Groh <linusg@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -7,6 +8,7 @@
#pragma once
#include <AK/RefCounted.h>
#include <AK/Variant.h>
#include <LibGfx/AffineTransform.h>
#include <LibGfx/Color.h>
#include <LibGfx/Forward.h>
@ -20,6 +22,10 @@
namespace Web::HTML {
// https://html.spec.whatwg.org/multipage/canvas.html#canvasimagesource
// NOTE: This is the Variant created by the IDL wrapper generator, and needs to be updated accordingly.
using CanvasImageSource = Variant<NonnullRefPtr<HTMLImageElement>, NonnullRefPtr<HTMLCanvasElement>>;
class CanvasRenderingContext2D
: public RefCounted<CanvasRenderingContext2D>
, public Bindings::Wrappable {
@ -43,7 +49,7 @@ public:
void stroke_rect(float x, float y, float width, float height);
void clear_rect(float x, float y, float width, float height);
void draw_image(const HTMLImageElement&, float x, float y);
DOM::ExceptionOr<void> draw_image(CanvasImageSource const&, float x, float y);
void scale(float sx, float sy);
void translate(float x, float y);
@ -130,4 +136,12 @@ private:
Gfx::Path m_path;
};
enum class CanvasImageSourceUsability {
Bad,
Good,
};
DOM::ExceptionOr<CanvasImageSourceUsability> check_usability_of_image(CanvasImageSource const&);
bool image_is_not_origin_clean(CanvasImageSource const&);
}