1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 09:08:12 +00:00

LibWeb: Add DOMPoint matrixTransform and DOMMatrix transformPoint

This commit is contained in:
Bastiaan van der Plaat 2023-08-15 13:16:45 +02:00 committed by Jelle Raaijmakers
parent 38bc8836d6
commit b4ae719664
9 changed files with 106 additions and 2 deletions

View file

@ -7,6 +7,7 @@
#include <LibWeb/Bindings/Intrinsics.h>
#include <LibWeb/Geometry/DOMMatrix.h>
#include <LibWeb/Geometry/DOMMatrixReadOnly.h>
#include <LibWeb/Geometry/DOMPoint.h>
#include <LibWeb/WebIDL/ExceptionOr.h>
namespace Web::Geometry {
@ -240,6 +241,39 @@ JS::NonnullGCPtr<DOMMatrix> DOMMatrixReadOnly::inverse() const
return result->invert_self();
}
// https://drafts.fxtf.org/geometry/#dom-dommatrixreadonly-transformpoint
JS::NonnullGCPtr<DOMPoint> DOMMatrixReadOnly::transform_point(DOMPointInit const& point) const
{
// Let pointObject be the result of invoking create a DOMPoint from the dictionary point.
auto point_object = DOMPoint::from_point(realm().vm(), point);
// Return the result of invoking transform a point with a matrix, given pointObject and the current matrix. The passed argument does not get modified.
return transform_point(point_object);
}
// https://drafts.fxtf.org/geometry/#transform-a-point-with-a-matrix
JS::NonnullGCPtr<DOMPoint> DOMMatrixReadOnly::transform_point(DOMPointReadOnly const& point) const
{
// 1. Let x be points x coordinate.
// 2. Let y be points y coordinate.
// 3. Let z be points z coordinate.
// 4. Let w be points w perspective.
// 5. Let pointVector be a new column vector with the elements being x, y, z, and w, respectively.
Vector4<double> point_vector { point.x(), point.y(), point.z(), point.w() };
// 6. Set pointVector to pointVector pre-multiplied by matrix.
// This is really a post multiply because of the transposed m_matrix.
point_vector = m_matrix.transpose() * point_vector;
// 7. Let transformedPoint be a new DOMPoint object.
// 8. Set transformedPoints x coordinate to pointVectors first element.
// 9. Set transformedPoints y coordinate to pointVectors second element.
// 10. Set transformedPoints z coordinate to pointVectors third element.
// 11. Set transformedPoints w perspective to pointVectors fourth element.
// 12. Return transformedPoint.
return DOMPoint::construct_impl(realm(), point_vector.x(), point_vector.y(), point_vector.z(), point_vector.w());
}
// https://drafts.fxtf.org/geometry/#dommatrixreadonly-stringification-behavior
WebIDL::ExceptionOr<String> DOMMatrixReadOnly::to_string() const
{