Merge pull request #439 from rachel-fenichel/feature/cache_screen_ctm

Cache screen CTM
This commit is contained in:
rachel-fenichel
2016-06-21 15:58:17 -07:00
committed by GitHub
3 changed files with 38 additions and 7 deletions

View File

@@ -606,7 +606,8 @@ Blockly.Scrollbar.prototype.onMouseDownBar_ = function(e) {
e.stopPropagation();
return;
}
var mouseXY = Blockly.mouseToSvg(e, this.workspace_.getParentSvg());
var mouseXY = Blockly.mouseToSvg(e, this.workspace_.getParentSvg(),
this.workspace_.getInverseScreenCTM());
var mouseLocation = this.horizontal_ ? mouseXY.x : mouseXY.y;
var handleXY = Blockly.getSvgXY_(this.svgHandle_, this.workspace_);

View File

@@ -308,14 +308,17 @@ Blockly.isRightButton = function(e) {
* The origin (0,0) is the top-left corner of the Blockly svg.
* @param {!Event} e Mouse event.
* @param {!Element} svg SVG element.
* @param {SVGMatrix} matrix Inverted screen CTM to use.
* @return {!Object} Object with .x and .y properties.
*/
Blockly.mouseToSvg = function(e, svg) {
Blockly.mouseToSvg = function(e, svg, matrix) {
var svgPoint = svg.createSVGPoint();
svgPoint.x = e.clientX;
svgPoint.y = e.clientY;
var matrix = svg.getScreenCTM();
matrix = matrix.inverse();
if (!matrix) {
matrix = svg.getScreenCTM().inverse();
}
return svgPoint.matrixTransform(matrix);
};

View File

@@ -144,6 +144,28 @@ Blockly.WorkspaceSvg.prototype.scrollbar = null;
*/
Blockly.WorkspaceSvg.prototype.lastSound_ = null;
/**
* Inverted screen CTM, for use in mouseToSvg.
* @type {SVGMatrix}
* @private
*/
Blockly.WorkspaceSvg.prototype.inverseScreenCTM_ = null;
/**
* Getter for the inverted screen CTM.
* @return {SVGMatrix} The matrix to use in mouseToSvg
*/
Blockly.WorkspaceSvg.prototype.getInverseScreenCTM = function() {
return this.inverseScreenCTM_;
};
/**
* Update the inverted screen CTM.
*/
Blockly.WorkspaceSvg.prototype.updateInverseScreenCTM = function() {
this.inverseScreenCTM_ = this.getParentSvg().getScreenCTM().inverse();
};
/**
* Save resize handler data so we can delete it later in dispose.
* @param {!Array.<!Array>} handler Data that can be passed to unbindEvent_.
@@ -331,6 +353,7 @@ Blockly.WorkspaceSvg.prototype.resizeContents = function() {
// based on contents.
this.scrollbar.resize();
}
this.updateInverseScreenCTM();
};
/**
@@ -357,6 +380,7 @@ Blockly.WorkspaceSvg.prototype.resize = function() {
this.scrollbar.resize();
}
this.updateInverseScreenCTM();
this.recordDeleteAreas();
};
@@ -664,7 +688,8 @@ Blockly.WorkspaceSvg.prototype.onMouseDown_ = function(e) {
*/
Blockly.WorkspaceSvg.prototype.startDrag = function(e, xy) {
// Record the starting offset between the bubble's location and the mouse.
var point = Blockly.mouseToSvg(e, this.getParentSvg());
var point = Blockly.mouseToSvg(e, this.getParentSvg(),
this.getInverseScreenCTM());
// Fix scale of mouse event.
point.x /= this.scale;
point.y /= this.scale;
@@ -677,7 +702,8 @@ Blockly.WorkspaceSvg.prototype.startDrag = function(e, xy) {
* @return {!goog.math.Coordinate} New location of object.
*/
Blockly.WorkspaceSvg.prototype.moveDrag = function(e) {
var point = Blockly.mouseToSvg(e, this.getParentSvg());
var point = Blockly.mouseToSvg(e, this.getParentSvg(),
this.getInverseScreenCTM());
// Fix scale of mouse event.
point.x /= this.scale;
point.y /= this.scale;
@@ -693,7 +719,8 @@ Blockly.WorkspaceSvg.prototype.onMouseWheel_ = function(e) {
// TODO: Remove terminateDrag and compensate for coordinate skew during zoom.
Blockly.terminateDrag_();
var delta = e.deltaY > 0 ? -1 : 1;
var position = Blockly.mouseToSvg(e, this.getParentSvg());
var position = Blockly.mouseToSvg(e, this.getParentSvg(),
this.getInverseScreenCTM());
this.zoom(position.x, position.y, delta);
e.preventDefault();
};