1

Imagine I want to transform a canvas context coordinate space to contain a certain bounding box, and that I'm writing a test around it.

Would it be possible to actually 'use' the context's transformation, somewhat like this:

function toBoundingBox( context, upleft, botright ) {
   // ... 
}

// and the test function:
function test( canvaselement ) {

    var canvasbox = { 
       topleft: {x:0, y:0}, 
       botright: {x:canvaselement.width, y:canvaselement.height} };

    var ctx = canvaselement.getContext("2d");
    toBoundingBox( ctx, {x:-1,y:-1}, {x:2, y: -5} );

    var thetransform = ctx.getTransform();
    assert( thetransform( {x:-1,y:-1} ) == canvasbox.topleft );
    assert( thetransform( {x:2, y:-5} ) == canvasbox.botright );

}

Or is there any other way to write this test function?

xtofl
  • 40,723
  • 12
  • 105
  • 192

1 Answers1

2

Sadly there's no native way to access transformation. You'll need to implement this yourself. See HTML5 Canvas get transform matrix? for further info. Hopefully this helps!

Community
  • 1
  • 1
Juho Vepsäläinen
  • 26,573
  • 12
  • 79
  • 105
  • you were right, but... http://stackoverflow.com/a/7395910/6610, edit 2: `currentTransformation()` was added in the whatwg spec. (cfr. http://lists.w3.org/Archives/Public/public-whatwg-archive/2012Mar/0269.html) – xtofl Mar 05 '13 at 08:10
  • Yeah, it is great that they added that to the spec! That's for the newer browsers, though. But that could be fine depending on your target. :) – Juho Vepsäläinen Mar 05 '13 at 09:10