In Firefox Canvas 2D contexts, there are (non-standard) mozCurrentTransform and mozCurrentTransformInverse properties.
Now WhatWG has defined currentTransform and currentTransformInverse properties (the first of which is even writable). Here is the relevant part of the specification:
http://www.whatwg.org/specs/web-apps/current-work/multipage/the-canvas-element.html#transformations
However, they probably will not be universally implemented in browsers for some time, so if you want portability, you will have to return to tracking the matrix manually, as @Dave and @James say.
It seems that every man and his dog wrote such a matrix tracker Canvas-transform-matrix-tracker. I just looked at @Dave Lawrence; I think mine is better in several ways, although I am sure that it is also inferior to others.
- Mine does not require any changes in the user's JS code - it modifies the Canvas and context prototypes, so you just add a script tag and you're good to go.
- It intercepts the setting of the currentTransform property.
- He is trying to do only what is needed.
It works in the latest Chrome and Firefox, but I have not tested it in IE yet.
I put mine in jsfiddle with a simple demo: http://jsfiddle.net/XmYqL/1/
Here is a code block to settle stackoverflow, so it allows me to reference jsfiddle (??):
code, code, wonderful code
I finally got to loading my polyfill on GitHub:
https://github.com/supermattydomain/canvas.currentTransform.js
I know that this is not ideal, but I would really like for us all to work on implementing One True Solution to solve this problem. I don’t care whether it is mine or someone else’s. This JavaScript / HTML5 / Canvas corner is too similar to the Balkans: a sea of partial solutions. Please, all, my plug, add your changes and send me pull requests or send me your url so that I can combine your code or replace my wholesale with yours or something else. This is a stupid problem that I just want to nail. If we work together, we can do it.
AnotherSmellyGeek
source share