HTML5 Canvas get transform matrix?

前端 未结 4 1916
一向
一向 2020-12-14 06:57

Is there a way to get the current transformation matrix for a canvas? There is a context.setTransform() function, but there does not seem to be a getTransform() equivalent a

4条回答
  •  忘掉有多难
    2020-12-14 07:49

    No, there simply isn't. :(

    Most canavs libraries (such as cake.js) have instead implemented their own matrix class to keep track of the current transformation matrix.

    The creator of cake.js thought that there being no way to get the current matrix was ridiculous enough to warrant a bug report about it. Unfortunately that was back in 2007 and there has been no effort to include a getCurrentTransform in the spec.

    Edit: I have created a simple Transformation class that will allow you to easily make your own getCanvas() or keep track of the Canvas's matrix side-by-side. Here it is. I hope that helps!

    Edit June 2012: The new specification does include a way to get the current transformation matrix! context.currentTransform can be used to get or set the current transformation matrix. Unfortunately no browsers have implemented it yet, though Firefox does have the vendor-specific mozCurrentTransform property on its context. So you can't use it just yet, but it is in the spec, so soon!

提交回复
热议问题