Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 18 additions & 16 deletions modes/canvas/path.js
Original file line number Diff line number Diff line change
@@ -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){
Expand All @@ -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(){
Expand All @@ -58,4 +54,10 @@ var CanvasPath = Class(Path, {

});

module.exports = CanvasPath;
CanvasPath.MOVE = MOVE;
CanvasPath.LINE = LINE;
CanvasPath.BEZIER_CURVE = BEZIER_CURVE;
CanvasPath.ARC = ARC;
CanvasPath.CLOSE = CLOSE;

module.exports = CanvasPath;
40 changes: 36 additions & 4 deletions modes/canvas/shape.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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){
Expand Down Expand Up @@ -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;
Expand Down