From a39af2b6d7c52844bd2a2973c41a3f8f4eb3cffc Mon Sep 17 00:00:00 2001 From: Jeff Schiller Date: Thu, 22 Oct 2009 03:18:54 +0000 Subject: [PATCH] Refinement of math from previous check-in (avoiding some DOM calls) git-svn-id: http://svg-edit.googlecode.com/svn/trunk@866 eee81c28-f429-11dd-99c0-75d572ba1ddd --- editor/svgcanvas.js | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/editor/svgcanvas.js b/editor/svgcanvas.js index bb094d38..00a72135 100644 --- a/editor/svgcanvas.js +++ b/editor/svgcanvas.js @@ -1532,6 +1532,16 @@ function BatchCommand(text) { call("selected", [grip[0]]); } + // Some global variables that we may need to refactor + var root_sctm = null; + + // A (hopefully) quicker function to transform a point by a matrix + // (this function avoids any DOM calls and just does the math) + // returns a tuple representing the transformed point + var transformPoint = function(x, y, m) { + return { x: m.a * x + m.c * y + m.e, y: m.b * x + m.d * y + m.f}; + }; + // in mouseDown : // - when we are in a create mode, the element is added to the canvas // but the action is not recorded until mousing up @@ -1539,10 +1549,8 @@ function BatchCommand(text) { // and do nothing else var mouseDown = function(evt) { - var sctm = svgroot.getScreenCTM().inverse(); - var pt = svgroot.createSVGPoint(); - pt.x = evt.pageX; pt.y = evt.pageY; - pt = pt.matrixTransform(sctm); + root_sctm = svgroot.getScreenCTM().inverse(); + var pt = transformPoint( evt.pageX, evt.pageY, root_sctm ); var mouse_x = pt.x; var mouse_y = pt.y; @@ -1847,10 +1855,7 @@ function BatchCommand(text) { { if (!started) return; var selected = selectedElements[0]; - var sctm = svgroot.getScreenCTM().inverse(); - var pt = svgroot.createSVGPoint(); - pt.x = evt.pageX; pt.y = evt.pageY; - pt = pt.matrixTransform(sctm); + var pt = transformPoint( evt.pageX, evt.pageY, root_sctm ); var mouse_x = pt.x; var mouse_y = pt.y; var shape = svgdoc.getElementById(getId()); @@ -2606,10 +2611,7 @@ function BatchCommand(text) { justSelected = null; if (!started) return; - var sctm = svgroot.getScreenCTM().inverse(); - var pt = svgroot.createSVGPoint(); - pt.x = evt.pageX; pt.y = evt.pageY; - pt = pt.matrixTransform(sctm); + var pt = transformPoint( evt.pageX, evt.pageY, root_sctm ); var mouse_x = pt.x; var mouse_y = pt.y; var x = mouse_x / current_zoom;