Use a switch statement, rather than ifs, in ol.render.canvas.Replay

This commit is contained in:
Tom Payne
2014-01-13 18:17:38 +01:00
parent 54e1fd3a41
commit 474b4a444b
+114 -102
View File
@@ -185,110 +185,122 @@ ol.render.canvas.Replay.prototype.replay_ =
var instruction = instructions[i]; var instruction = instructions[i];
var type = /** @type {ol.render.canvas.Instruction} */ (instruction[0]); var type = /** @type {ol.render.canvas.Instruction} */ (instruction[0]);
var geometry; var geometry;
if (type == ol.render.canvas.Instruction.BEGIN_GEOMETRY) { switch (type) {
geometry = /** @type {ol.geom.Geometry} */ (instruction[1]); case ol.render.canvas.Instruction.BEGIN_GEOMETRY:
if (renderGeometryFunction(geometry)) {
++i;
} else {
i = /** @type {number} */ (instruction[2]);
}
} else if (type == ol.render.canvas.Instruction.BEGIN_PATH) {
context.beginPath();
++i;
} else if (type == ol.render.canvas.Instruction.CLOSE_PATH) {
context.closePath();
++i;
} else if (type == ol.render.canvas.Instruction.DRAW_IMAGE) {
goog.asserts.assert(goog.isNumber(instruction[1]));
d = /** @type {number} */ (instruction[1]);
goog.asserts.assert(goog.isNumber(instruction[2]));
dd = /** @type {number} */ (instruction[2]);
var image = /** @type {HTMLCanvasElement|HTMLVideoElement|Image} */
(instruction[3]);
// Remaining arguments in DRAW_IMAGE are in alphabetical order
var anchorX = /** @type {number} */ (instruction[4]) * pixelRatio;
var anchorY = /** @type {number} */ (instruction[5]) * pixelRatio;
var height = /** @type {number} */ (instruction[6]) * pixelRatio;
var rotation = /** @type {number} */ (instruction[7]);
var scale = /** @type {number} */ (instruction[8]);
var snapToPixel = /** @type {boolean|undefined} */ (instruction[9]);
var width = /** @type {number} */ (instruction[10]) * pixelRatio;
for (; d < dd; d += 2) {
var x = pixelCoordinates[d] - anchorX;
var y = pixelCoordinates[d + 1] - anchorY;
if (snapToPixel) {
x = (x + 0.5) | 0;
y = (y + 0.5) | 0;
}
if (scale != 1 || rotation !== 0) {
var centerX = x + anchorX;
var centerY = y + anchorY;
ol.vec.Mat4.makeTransform2D(
localTransform, centerX, centerY, scale, scale,
rotation, -centerX, -centerY);
context.setTransform(
goog.vec.Mat4.getElement(localTransform, 0, 0),
goog.vec.Mat4.getElement(localTransform, 1, 0),
goog.vec.Mat4.getElement(localTransform, 0, 1),
goog.vec.Mat4.getElement(localTransform, 1, 1),
goog.vec.Mat4.getElement(localTransform, 0, 3),
goog.vec.Mat4.getElement(localTransform, 1, 3));
}
context.drawImage(image, x, y, width, height);
if (scale != 1 || rotation !== 0) {
context.setTransform(1, 0, 0, 1, 0, 0);
}
}
++i;
} else if (type == ol.render.canvas.Instruction.END_GEOMETRY) {
if (goog.isDef(geometryCallback)) {
geometry = /** @type {ol.geom.Geometry} */ (instruction[1]); geometry = /** @type {ol.geom.Geometry} */ (instruction[1]);
var data = /** @type {Object} */ (instruction[2]); if (renderGeometryFunction(geometry)) {
var result = geometryCallback(geometry, data); ++i;
if (result) { } else {
return result; i = /** @type {number} */ (instruction[2]);
} }
} break;
++i; case ol.render.canvas.Instruction.BEGIN_PATH:
} else if (type == ol.render.canvas.Instruction.FILL) { context.beginPath();
context.fill(); ++i;
++i; break;
} else if (type == ol.render.canvas.Instruction.MOVE_TO_LINE_TO) { case ol.render.canvas.Instruction.CLOSE_PATH:
goog.asserts.assert(goog.isNumber(instruction[1])); context.closePath();
d = /** @type {number} */ (instruction[1]); ++i;
goog.asserts.assert(goog.isNumber(instruction[2])); break;
dd = /** @type {number} */ (instruction[2]); case ol.render.canvas.Instruction.DRAW_IMAGE:
context.moveTo(pixelCoordinates[d], pixelCoordinates[d + 1]); goog.asserts.assert(goog.isNumber(instruction[1]));
for (d += 2; d < dd; d += 2) { d = /** @type {number} */ (instruction[1]);
context.lineTo(pixelCoordinates[d], pixelCoordinates[d + 1]); goog.asserts.assert(goog.isNumber(instruction[2]));
} dd = /** @type {number} */ (instruction[2]);
++i; var image = /** @type {HTMLCanvasElement|HTMLVideoElement|Image} */
} else if (type == ol.render.canvas.Instruction.SET_FILL_STYLE) { (instruction[3]);
goog.asserts.assert(goog.isString(instruction[1])); // Remaining arguments in DRAW_IMAGE are in alphabetical order
context.fillStyle = /** @type {string} */ (instruction[1]); var anchorX = /** @type {number} */ (instruction[4]) * pixelRatio;
++i; var anchorY = /** @type {number} */ (instruction[5]) * pixelRatio;
} else if (type == ol.render.canvas.Instruction.SET_STROKE_STYLE) { var height = /** @type {number} */ (instruction[6]) * pixelRatio;
goog.asserts.assert(goog.isString(instruction[1])); var rotation = /** @type {number} */ (instruction[7]);
goog.asserts.assert(goog.isNumber(instruction[2])); var scale = /** @type {number} */ (instruction[8]);
goog.asserts.assert(goog.isString(instruction[3])); var snapToPixel = /** @type {boolean|undefined} */ (instruction[9]);
goog.asserts.assert(goog.isString(instruction[4])); var width = /** @type {number} */ (instruction[10]) * pixelRatio;
goog.asserts.assert(goog.isNumber(instruction[5])); for (; d < dd; d += 2) {
goog.asserts.assert(!goog.isNull(instruction[6])); var x = pixelCoordinates[d] - anchorX;
context.strokeStyle = /** @type {string} */ (instruction[1]); var y = pixelCoordinates[d + 1] - anchorY;
context.lineWidth = /** @type {number} */ (instruction[2]) * pixelRatio; if (snapToPixel) {
context.lineCap = /** @type {string} */ (instruction[3]); x = (x + 0.5) | 0;
context.lineJoin = /** @type {string} */ (instruction[4]); y = (y + 0.5) | 0;
context.miterLimit = /** @type {number} */ (instruction[5]); }
if (goog.isDef(context.setLineDash)) { if (scale != 1 || rotation !== 0) {
context.setLineDash(/** @type {Array.<number>} */ (instruction[6])); var centerX = x + anchorX;
} var centerY = y + anchorY;
++i; ol.vec.Mat4.makeTransform2D(
} else if (type == ol.render.canvas.Instruction.STROKE) { localTransform, centerX, centerY, scale, scale,
context.stroke(); rotation, -centerX, -centerY);
++i; context.setTransform(
} else { goog.vec.Mat4.getElement(localTransform, 0, 0),
goog.asserts.fail(); goog.vec.Mat4.getElement(localTransform, 1, 0),
++i; // consume the instruction anyway, to avoid an infinite loop goog.vec.Mat4.getElement(localTransform, 0, 1),
goog.vec.Mat4.getElement(localTransform, 1, 1),
goog.vec.Mat4.getElement(localTransform, 0, 3),
goog.vec.Mat4.getElement(localTransform, 1, 3));
}
context.drawImage(image, x, y, width, height);
if (scale != 1 || rotation !== 0) {
context.setTransform(1, 0, 0, 1, 0, 0);
}
}
++i;
break;
case ol.render.canvas.Instruction.END_GEOMETRY:
if (goog.isDef(geometryCallback)) {
geometry = /** @type {ol.geom.Geometry} */ (instruction[1]);
var data = /** @type {Object} */ (instruction[2]);
var result = geometryCallback(geometry, data);
if (result) {
return result;
}
}
++i;
break;
case ol.render.canvas.Instruction.FILL:
context.fill();
++i;
break;
case ol.render.canvas.Instruction.MOVE_TO_LINE_TO:
goog.asserts.assert(goog.isNumber(instruction[1]));
d = /** @type {number} */ (instruction[1]);
goog.asserts.assert(goog.isNumber(instruction[2]));
dd = /** @type {number} */ (instruction[2]);
context.moveTo(pixelCoordinates[d], pixelCoordinates[d + 1]);
for (d += 2; d < dd; d += 2) {
context.lineTo(pixelCoordinates[d], pixelCoordinates[d + 1]);
}
++i;
break;
case ol.render.canvas.Instruction.SET_FILL_STYLE:
goog.asserts.assert(goog.isString(instruction[1]));
context.fillStyle = /** @type {string} */ (instruction[1]);
++i;
break;
case ol.render.canvas.Instruction.SET_STROKE_STYLE:
goog.asserts.assert(goog.isString(instruction[1]));
goog.asserts.assert(goog.isNumber(instruction[2]));
goog.asserts.assert(goog.isString(instruction[3]));
goog.asserts.assert(goog.isString(instruction[4]));
goog.asserts.assert(goog.isNumber(instruction[5]));
goog.asserts.assert(!goog.isNull(instruction[6]));
context.strokeStyle = /** @type {string} */ (instruction[1]);
context.lineWidth = /** @type {number} */ (instruction[2]) * pixelRatio;
context.lineCap = /** @type {string} */ (instruction[3]);
context.lineJoin = /** @type {string} */ (instruction[4]);
context.miterLimit = /** @type {number} */ (instruction[5]);
if (goog.isDef(context.setLineDash)) {
context.setLineDash(/** @type {Array.<number>} */ (instruction[6]));
}
++i;
break;
case ol.render.canvas.Instruction.STROKE:
context.stroke();
++i;
break;
default:
goog.asserts.fail();
++i; // consume the instruction anyway, to avoid an infinite loop
break;
} }
} }
// assert that all instructions were consumed // assert that all instructions were consumed