mirror of
https://github.com/RGBCube/serenity
synced 2025-07-23 19:37:34 +00:00
LibWeb: Use an AffineTransform for CanvasRenderingContext2D :^)
This will allow us to support complex 2D transforms.
This commit is contained in:
parent
6f2c63000d
commit
dd00175ae2
2 changed files with 9 additions and 26 deletions
|
@ -56,7 +56,7 @@ void CanvasRenderingContext2D::fill_rect(float x, float y, float width, float he
|
||||||
if (!painter)
|
if (!painter)
|
||||||
return;
|
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);
|
painter->fill_rect(enclosing_int_rect(rect), m_fill_style);
|
||||||
did_draw(rect);
|
did_draw(rect);
|
||||||
}
|
}
|
||||||
|
@ -77,35 +77,21 @@ void CanvasRenderingContext2D::stroke_rect(float x, float y, float width, float
|
||||||
if (!painter)
|
if (!painter)
|
||||||
return;
|
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);
|
painter->draw_rect(enclosing_int_rect(rect), m_stroke_style);
|
||||||
did_draw(rect);
|
did_draw(rect);
|
||||||
}
|
}
|
||||||
|
|
||||||
void CanvasRenderingContext2D::scale(float sx, float sy)
|
void CanvasRenderingContext2D::scale(float sx, float sy)
|
||||||
{
|
{
|
||||||
// FIXME: Actually do something with the scale factor!
|
dbg() << "CanvasRenderingContext2D::scale(): " << sx << ", " << sy;
|
||||||
dbg() << "CanvasRenderingContext2D::scale(): " << String::format("%f", sx) << ", " << String::format("%f", sy);
|
m_transform.scale(sx, sy);
|
||||||
m_scale_x = sx;
|
|
||||||
m_scale_y = 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(): " << tx << ", " << ty;
|
||||||
dbg() << "CanvasRenderingContext2D::translate(): " << String::format("%f", x) << ", " << String::format("%f", y);
|
m_transform.translate(tx, ty);
|
||||||
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
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void CanvasRenderingContext2D::did_draw(const Gfx::FloatRect&)
|
void CanvasRenderingContext2D::did_draw(const Gfx::FloatRect&)
|
||||||
|
|
|
@ -27,6 +27,7 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <AK/RefCounted.h>
|
#include <AK/RefCounted.h>
|
||||||
|
#include <LibGfx/AffineTransform.h>
|
||||||
#include <LibGfx/Color.h>
|
#include <LibGfx/Color.h>
|
||||||
#include <LibGfx/Forward.h>
|
#include <LibGfx/Forward.h>
|
||||||
#include <LibWeb/Bindings/Wrappable.h>
|
#include <LibWeb/Bindings/Wrappable.h>
|
||||||
|
@ -60,17 +61,13 @@ public:
|
||||||
private:
|
private:
|
||||||
explicit CanvasRenderingContext2D(HTMLCanvasElement&);
|
explicit CanvasRenderingContext2D(HTMLCanvasElement&);
|
||||||
|
|
||||||
Gfx::FloatRect compute_rect(float x, float y, float width, float height);
|
|
||||||
void did_draw(const Gfx::FloatRect&);
|
void did_draw(const Gfx::FloatRect&);
|
||||||
|
|
||||||
OwnPtr<Gfx::Painter> painter();
|
OwnPtr<Gfx::Painter> painter();
|
||||||
|
|
||||||
WeakPtr<HTMLCanvasElement> m_element;
|
WeakPtr<HTMLCanvasElement> m_element;
|
||||||
|
|
||||||
float m_scale_x { 1 };
|
Gfx::AffineTransform m_transform;
|
||||||
float m_scale_y { 1 };
|
|
||||||
float m_translate_x { 0 };
|
|
||||||
float m_translate_y { 0 };
|
|
||||||
Gfx::Color m_fill_style;
|
Gfx::Color m_fill_style;
|
||||||
Gfx::Color m_stroke_style;
|
Gfx::Color m_stroke_style;
|
||||||
};
|
};
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue