diff --git a/modes/canvas/path.js b/modes/canvas/path.js index f96e4b7..ae4d93b 100644 --- a/modes/canvas/path.js +++ b/modes/canvas/path.js @@ -1,6 +1,12 @@ var Class = require('../../core/class'); var Path = require('../../core/path'); +var MOVE = 0, + LINE = 1, + BEZIER_CURVE = 2, + ARC = 3, + CLOSE = 4; + var CanvasPath = Class(Path, { initialize: function(path){ @@ -20,36 +26,26 @@ var CanvasPath = Class(Path, { }, onMove: function(sx, sy, x, y){ - this.path.push(function(context){ - context.moveTo(x, y); - }); + this.path.push(MOVE, x, y); }, onLine: function(sx, sy, x, y){ - this.path.push(function(context){ - context.lineTo(x, y); - }); + this.path.push(LINE, x, y); }, onBezierCurve: function(sx, sy, p1x, p1y, p2x, p2y, x, y){ - this.path.push(function(context){ - context.bezierCurveTo(p1x, p1y, p2x, p2y, x, y); - }); + this.path.push(BEZIER_CURVE, p1x, p1y, p2x, p2y, x, y); }, _arcToBezier: Path.prototype.onArc, onArc: function(sx, sy, ex, ey, cx, cy, rx, ry, sa, ea, ccw, rotation){ if (rx != ry || rotation) return this._arcToBezier(sx, sy, ex, ey, cx, cy, rx, ry, sa, ea, ccw, rotation); - this.path.push(function(context){ - context.arc(cx, cy, rx, sa, ea, ccw); - }); + this.path.push(ARC, cx, cy, rx, sa, ea, ccw); }, onClose: function(){ - this.path.push(function(context){ - context.closePath(); - }); + this.path.push(CLOSE); }, toCommands: function(){ @@ -58,4 +54,10 @@ var CanvasPath = Class(Path, { }); -module.exports = CanvasPath; \ No newline at end of file +CanvasPath.MOVE = MOVE; +CanvasPath.LINE = LINE; +CanvasPath.BEZIER_CURVE = BEZIER_CURVE; +CanvasPath.ARC = ARC; +CanvasPath.CLOSE = CLOSE; + +module.exports = CanvasPath; diff --git a/modes/canvas/shape.js b/modes/canvas/shape.js index a896523..690926a 100644 --- a/modes/canvas/shape.js +++ b/modes/canvas/shape.js @@ -2,6 +2,40 @@ var Class = require('../../core/class'); var Base = require('./base'); var Path = require('./path'); +var MOVE = Path.MOVE; +var LINE = Path.LINE; +var BEZIER_CURVE = Path.BEZIER_CURVE; +var ARC = Path.ARC; +var CLOSE = Path.CLOSE; + +function executeCommands(context, commands) { + var i = 0, l = commands.length, cmd; + while (i < l) { + cmd = commands[i++]; + switch (cmd) { + case MOVE: + context.moveTo(commands[i++], commands[i++]); + break; + case LINE: + context.lineTo(commands[i++], commands[i++]); + break; + case BEZIER_CURVE: + context.bezierCurveTo( + commands[i++], commands[i++], commands[i++], + commands[i++], commands[i++], commands[i++]); + break; + case ARC: + context.arc( + commands[i++], commands[i++], commands[i++], + commands[i++], commands[i++], commands[i++]); + break; + case CLOSE: + context.closePath(); + break; + } + } +} + module.exports = Class(Base, { base_initialize: Base.prototype.initialize, @@ -28,8 +62,7 @@ module.exports = Class(Base, { var context = Base._genericContext, commands = this._commands; if (!commands) return null; context.beginPath(); - for (var i = 0, l = commands.length; i < l; i++) - commands[i](context); + executeCommands(context, commands); return context.isPointInPath(x, y) ? this : null; } if (x > 0 && y > 0 && x < this.width && y < this.height){ @@ -66,8 +99,7 @@ module.exports = Class(Base, { } } - for (var i = 0, l = commands.length; i < l; i++) - commands[i](context); + executeCommands(context, commands); if (fill){ var m = this._fillTransform;