1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 14:27:35 +00:00

LibWeb: Implement CanvasRenderingContext2D.createPattern()

This is a first pass at implementing CRC2D.createPattern() and the
associated CanvasPattern object. This implementation only works for a
few of the required image sources [like CRC2D.drawImage()], and does
not yet support transforms. Other than that it supports everything
else (which is mainly the various repeat modes).
This commit is contained in:
MacDue 2023-02-02 20:47:46 +00:00 committed by Andreas Kling
parent 0c313c586b
commit f74e2da875
8 changed files with 229 additions and 8 deletions

View file

@ -12,6 +12,7 @@
#include <AK/DeprecatedString.h>
#include <LibWeb/HTML/Canvas/CanvasState.h>
#include <LibWeb/HTML/CanvasGradient.h>
#include <LibWeb/HTML/CanvasPattern.h>
namespace Web::HTML {
@ -20,7 +21,7 @@ template<typename IncludingClass>
class CanvasFillStrokeStyles {
public:
~CanvasFillStrokeStyles() = default;
using FillOrStrokeStyleVariant = Variant<DeprecatedString, JS::Handle<CanvasGradient>>;
using FillOrStrokeStyleVariant = Variant<DeprecatedString, JS::Handle<CanvasGradient>, JS::Handle<CanvasPattern>>;
static CanvasState::FillOrStrokeStyle to_canvas_state_fill_or_stroke_style(auto const& style)
{
@ -73,6 +74,12 @@ public:
return CanvasGradient::create_conic(realm, start_angle, x, y);
}
WebIDL::ExceptionOr<JS::GCPtr<CanvasPattern>> create_pattern(CanvasImageSource const& image, StringView repetition)
{
auto& realm = static_cast<IncludingClass&>(*this).realm();
return CanvasPattern::create(realm, image, repetition);
}
protected:
CanvasFillStrokeStyles() = default;

View file

@ -1,13 +1,15 @@
#import <HTML/CanvasGradient.idl>
#import <HTML/CanvasPattern.idl>
#import <HTML/HTMLCanvasElement.idl>
#import <HTML/HTMLImageElement.idl>
// https://html.spec.whatwg.org/multipage/canvas.html#canvasfillstrokestyles
interface mixin CanvasFillStrokeStyles {
// FIXME: Should be `(DOMString or CanvasGradient or CanvasPattern)`
attribute (DOMString or CanvasGradient) strokeStyle;
// FIXME: Should be `(DOMString or CanvasGradient or CanvasPattern)`
attribute (DOMString or CanvasGradient) fillStyle;
attribute (DOMString or CanvasGradient or CanvasPattern) strokeStyle;
attribute (DOMString or CanvasGradient or CanvasPattern) fillStyle;
CanvasGradient createLinearGradient(double x0, double y0, double x1, double y1);
CanvasGradient createRadialGradient(double x0, double y0, double r0, double x1, double y1, double r1);
CanvasGradient createConicGradient(double startAngle, double x, double y);
// FIXME: CanvasPattern? createPattern(CanvasImageSource image, [LegacyNullToEmptyString] DOMString repetition);
// FIXME: 'image' should be a CanvasImageSource
CanvasPattern? createPattern((HTMLImageElement or HTMLCanvasElement) image, [LegacyNullToEmptyString] DOMString repetition);
};

View file

@ -13,6 +13,7 @@
#include <LibGfx/Color.h>
#include <LibGfx/PaintStyle.h>
#include <LibWeb/HTML/CanvasGradient.h>
#include <LibWeb/HTML/CanvasPattern.h>
namespace Web::HTML {
@ -26,7 +27,7 @@ public:
void reset();
bool is_context_lost();
using FillOrStrokeVariant = Variant<Gfx::Color, JS::Handle<CanvasGradient>>;
using FillOrStrokeVariant = Variant<Gfx::Color, JS::Handle<CanvasGradient>, JS::Handle<CanvasPattern>>;
struct FillOrStrokeStyle {
FillOrStrokeStyle(Gfx::Color color)
@ -49,7 +50,7 @@ public:
Optional<Gfx::Color> as_color() const;
Gfx::Color to_color_but_fixme_should_accept_any_paint_style() const;
using JsFillOrStrokeStyle = Variant<DeprecatedString, JS::Handle<CanvasGradient>>;
using JsFillOrStrokeStyle = Variant<DeprecatedString, JS::Handle<CanvasGradient>, JS::Handle<CanvasPattern>>;
JsFillOrStrokeStyle to_js_fill_or_stroke_style() const
{