mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 09:37:34 +00:00
LibWeb: Add DOMPoint matrixTransform and DOMMatrix transformPoint
This commit is contained in:
parent
38bc8836d6
commit
b4ae719664
9 changed files with 106 additions and 2 deletions
|
@ -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 point’s x coordinate.
|
||||
// 2. Let y be point’s y coordinate.
|
||||
// 3. Let z be point’s z coordinate.
|
||||
// 4. Let w be point’s 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 transformedPoint’s x coordinate to pointVector’s first element.
|
||||
// 9. Set transformedPoint’s y coordinate to pointVector’s second element.
|
||||
// 10. Set transformedPoint’s z coordinate to pointVector’s third element.
|
||||
// 11. Set transformedPoint’s w perspective to pointVector’s 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
|
||||
{
|
||||
|
|
|
@ -83,6 +83,10 @@ public:
|
|||
|
||||
JS::NonnullGCPtr<DOMMatrix> inverse() const;
|
||||
|
||||
JS::NonnullGCPtr<DOMPoint> transform_point(DOMPointInit const&) const;
|
||||
|
||||
JS::NonnullGCPtr<DOMPoint> transform_point(DOMPointReadOnly const&) const;
|
||||
|
||||
WebIDL::ExceptionOr<String> to_string() const;
|
||||
|
||||
protected:
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
#import <Geometry/DOMMatrix.idl>
|
||||
#import <Geometry/DOMPoint.idl>
|
||||
|
||||
// https://drafts.fxtf.org/geometry/#dommatrixreadonly
|
||||
[Exposed=(Window,Worker), Serializable, UseNewAKString]
|
||||
|
@ -52,7 +53,7 @@ interface DOMMatrixReadOnly {
|
|||
// FIXME: [NewObject] DOMMatrix flipY();
|
||||
[NewObject] DOMMatrix inverse();
|
||||
|
||||
// FIXME: [NewObject] DOMPoint transformPoint(optional DOMPointInit point = {});
|
||||
[NewObject] DOMPoint transformPoint(optional DOMPointInit point = {});
|
||||
// FIXME: [NewObject] Float32Array toFloat32Array();
|
||||
// FIXME: [NewObject] Float64Array toFloat64Array();
|
||||
|
||||
|
|
|
@ -6,6 +6,7 @@
|
|||
*/
|
||||
|
||||
#include <LibWeb/Bindings/Intrinsics.h>
|
||||
#include <LibWeb/Geometry/DOMMatrix.h>
|
||||
#include <LibWeb/Geometry/DOMPointReadOnly.h>
|
||||
#include <LibWeb/WebIDL/ExceptionOr.h>
|
||||
|
||||
|
@ -34,6 +35,19 @@ JS::NonnullGCPtr<DOMPointReadOnly> DOMPointReadOnly::from_point(JS::VM& vm, DOMP
|
|||
|
||||
DOMPointReadOnly::~DOMPointReadOnly() = default;
|
||||
|
||||
// https://drafts.fxtf.org/geometry/#dom-dompointreadonly-matrixtransform
|
||||
WebIDL::ExceptionOr<JS::NonnullGCPtr<DOMPoint>> DOMPointReadOnly::matrix_transform(DOMMatrixInit& matrix) const
|
||||
{
|
||||
// 1. Let matrixObject be the result of invoking create a DOMMatrix from the dictionary matrix.
|
||||
auto maybe_matrix_object = DOMMatrix::create_from_dom_matrix_2d_init(realm(), matrix);
|
||||
if (maybe_matrix_object.is_exception())
|
||||
return maybe_matrix_object.exception();
|
||||
auto matrix_object = maybe_matrix_object.release_value();
|
||||
|
||||
// 2. Return the result of invoking transform a point with a matrix, given the current point and matrixObject. The current point does not get modified.
|
||||
return matrix_object->transform_point(*this);
|
||||
}
|
||||
|
||||
void DOMPointReadOnly::initialize(JS::Realm& realm)
|
||||
{
|
||||
Base::initialize(realm);
|
||||
|
|
|
@ -36,6 +36,8 @@ public:
|
|||
double z() const { return m_z; }
|
||||
double w() const { return m_w; }
|
||||
|
||||
WebIDL::ExceptionOr<JS::NonnullGCPtr<DOMPoint>> matrix_transform(DOMMatrixInit&) const;
|
||||
|
||||
protected:
|
||||
DOMPointReadOnly(JS::Realm&, double x, double y, double z, double w);
|
||||
|
||||
|
|
|
@ -1,3 +1,6 @@
|
|||
#import <Geometry/DOMMatrixReadOnly.idl>
|
||||
#import <Geometry/DOMPoint.idl>
|
||||
|
||||
// https://drafts.fxtf.org/geometry/#dompointreadonly
|
||||
[Exposed=(Window,Worker), Serializable]
|
||||
interface DOMPointReadOnly {
|
||||
|
@ -12,7 +15,7 @@ interface DOMPointReadOnly {
|
|||
readonly attribute unrestricted double z;
|
||||
readonly attribute unrestricted double w;
|
||||
|
||||
// FIXME: DOMPoint matrixTransform(optional DOMMatrixInit matrix = {});
|
||||
[NewObject] DOMPoint matrixTransform(optional DOMMatrixInit matrix = {});
|
||||
|
||||
[Default] object toJSON();
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue