LibWeb: Use an AffineTransform for CanvasRenderingContext2D :^)

This will allow us to support complex 2D transforms.
This commit is contained in:
Andreas Kling 2020-04-12 19:22:42 +02:00
parent 6f2c63000d
commit dd00175ae2
Notes: sideshowbarker 2024-07-19 07:40:12 +09:00
2 changed files with 9 additions and 26 deletions

View file

@ -56,7 +56,7 @@ void CanvasRenderingContext2D::fill_rect(float x, float y, float width, float he
if (!painter)
return;
Gfx::FloatRect rect = compute_rect(x, y, width, height);
auto rect = m_transform.map(Gfx::FloatRect(x, y, width, height));
painter->fill_rect(enclosing_int_rect(rect), m_fill_style);
did_draw(rect);
}
@ -77,35 +77,21 @@ void CanvasRenderingContext2D::stroke_rect(float x, float y, float width, float
if (!painter)
return;
Gfx::FloatRect rect = compute_rect(x, y, width, height);
auto rect = m_transform.map(Gfx::FloatRect(x, y, width, height));
painter->draw_rect(enclosing_int_rect(rect), m_stroke_style);
did_draw(rect);
}
void CanvasRenderingContext2D::scale(float sx, float sy)
{
// FIXME: Actually do something with the scale factor!
dbg() << "CanvasRenderingContext2D::scale(): " << String::format("%f", sx) << ", " << String::format("%f", sy);
m_scale_x = sx;
m_scale_y = sy;
dbg() << "CanvasRenderingContext2D::scale(): " << sx << ", " << sy;
m_transform.scale(sx, sy);
}
void CanvasRenderingContext2D::translate(float x, float y)
void CanvasRenderingContext2D::translate(float tx, float ty)
{
// FIXME: Actually do something with the translation!
dbg() << "CanvasRenderingContext2D::translate(): " << String::format("%f", x) << ", " << String::format("%f", y);
m_translate_x = x;
m_translate_y = y;
}
Gfx::FloatRect CanvasRenderingContext2D::compute_rect(float x, float y, float width, float height)
{
return {
(x + m_translate_x) * m_scale_x,
(y + m_translate_y) * m_scale_y,
width * m_scale_x,
height * m_scale_y
};
dbg() << "CanvasRenderingContext2D::translate(): " << tx << ", " << ty;
m_transform.translate(tx, ty);
}
void CanvasRenderingContext2D::did_draw(const Gfx::FloatRect&)

View file

@ -27,6 +27,7 @@
#pragma once
#include <AK/RefCounted.h>
#include <LibGfx/AffineTransform.h>
#include <LibGfx/Color.h>
#include <LibGfx/Forward.h>
#include <LibWeb/Bindings/Wrappable.h>
@ -60,17 +61,13 @@ public:
private:
explicit CanvasRenderingContext2D(HTMLCanvasElement&);
Gfx::FloatRect compute_rect(float x, float y, float width, float height);
void did_draw(const Gfx::FloatRect&);
OwnPtr<Gfx::Painter> painter();
WeakPtr<HTMLCanvasElement> m_element;
float m_scale_x { 1 };
float m_scale_y { 1 };
float m_translate_x { 0 };
float m_translate_y { 0 };
Gfx::AffineTransform m_transform;
Gfx::Color m_fill_style;
Gfx::Color m_stroke_style;
};