Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[geometry-1] propose new DOMMatrix.reset method (no way to reset DOMMatrix is2D back to true when returning to identity) #584

Open
trusktr opened this issue Dec 27, 2024 · 0 comments

Comments

@trusktr
Copy link

trusktr commented Dec 27, 2024

There is currently no way to reset a matrix back to initial state.

For example, run this in any browser console:

function toIdentity(mat) {
	mat.m11 = 1
	mat.m12 = 0
	mat.m13 = 0
	mat.m14 = 0

	mat.m21 = 0
	mat.m22 = 1
	mat.m23 = 0
	mat.m24 = 0

	mat.m31 = 0
	mat.m32 = 0
	mat.m33 = 1
	mat.m34 = 0

	mat.m41 = 0
	mat.m42 = 0
	mat.m43 = 0
	mat.m44 = 1
}

const mat  = new DOMMatrix()
console.assert(mat.is2D) // passes
mat.rotateSelf(10, 20, 30)
console.assert(!mat.is2D) // passes
toIdentity(mat)
console.assert(mat.is2D) // fail!

The last assertion fails:

Assertion failed: expect is2D to be true

This makes it tricky to use a single DOMMatrix instance as an optimized cached value when avoiding mutation (instead of always creating new instances for calculations). For example, when we set the matrix back to identity, is2D should go back to true, that way any optimizations that rely on if (mat.is2D) can kick in again.

If we can add more methods to DOMMatrix, for example as we've begun to ideate in

one of them could be mat.toIdentity() or mat.reset() which can set the internal is2D value to back to true as well as restore the matrix back to an identity matrix.

The hand-rolled toIdentity function in my example cannot set the private is2D state back to true from the outside.

Besides adding DOMMatrix.toIdentity or DOMMatrix.reset, another possibility is to expand the set steps for m11 to m44 such that if they receive 0 or 1, they should check if isIdentity is true, and if so set is2D back to true. However this might be more expensive than relying on new isIdentity/reset method which will unconditionally set is2D back to true without checking all values many times. With the setter idea, it could be checking all values up to 16 times (16 times 16) when setting all 16 values back to default state.

@trusktr trusktr changed the title [geometry-1] There is no way to reset DOMMatrix is2D back to true when returning to identity. [geometry-1] propose new DOMMatrix.reset method (no way to reset DOMMatrix is2D back to true when returning to identity) Dec 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant