mirror of
https://github.com/scratchfoundation/scratchjr.git
synced 2025-02-16 08:29:51 -05:00
DrawPath module
This commit is contained in:
parent
8174793126
commit
fdd74a411c
1 changed files with 57 additions and 59 deletions
|
@ -1,65 +1,63 @@
|
|||
var DrawPath = function () {};
|
||||
let startx = 0;
|
||||
let starty = 0;
|
||||
let pathx = 0;
|
||||
let pathy = 0;
|
||||
|
||||
DrawPath.startx = 0;
|
||||
DrawPath.starty = 0;
|
||||
DrawPath.pathx = 0;
|
||||
DrawPath.pathy = 0;
|
||||
DrawPath.aCurve = false;
|
||||
|
||||
DrawPath.render = function (ctx, path) {
|
||||
DrawPath.pathx = 0;
|
||||
DrawPath.pathy = 0; // start top left
|
||||
DrawPath.aCurve = false;
|
||||
for (var i in path) {
|
||||
DrawPath.drawSection(path[i], ctx);
|
||||
export default class DrawPath {
|
||||
static render (ctx, path) {
|
||||
pathx = 0;
|
||||
pathy = 0; // start top left
|
||||
for (var i in path) {
|
||||
DrawPath.drawSection(path[i], ctx);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
DrawPath.drawSection = function (item, ctx) {
|
||||
var cx, cy, px, py;
|
||||
switch ((String(item[0])).toLowerCase()) {
|
||||
case 'm': DrawPath.absoluteMove(item[1], item[2]);
|
||||
ctx.moveTo(DrawPath.pathx, DrawPath.pathy);
|
||||
DrawPath.startx = item[1]; DrawPath.starty = item[2];
|
||||
break;
|
||||
case 'l': DrawPath.relativeMove(item[1], item[2]); ctx.lineTo(DrawPath.pathx, DrawPath.pathy);
|
||||
break;
|
||||
case 'h': DrawPath.pathx += item[1]; ctx.lineTo(DrawPath.pathx, DrawPath.pathy);
|
||||
break;
|
||||
case 'v': DrawPath.pathy += item[1]; ctx.lineTo(DrawPath.pathx, DrawPath.pathy);
|
||||
break;
|
||||
case 'q':
|
||||
cx = DrawPath.pathx + item[1];
|
||||
cy = DrawPath.pathy + item[2];
|
||||
px = DrawPath.pathx + item[3];
|
||||
py = DrawPath.pathy + item[4];
|
||||
ctx.quadraticCurveTo(cx, cy, px, py);
|
||||
DrawPath.relativeMove(item[3], item[4]);
|
||||
break;
|
||||
case 'c': // beziers
|
||||
cx = DrawPath.pathx + item[1];
|
||||
cy = DrawPath.pathy + item[2];
|
||||
var c2x = DrawPath.pathx + item[3];
|
||||
var c2y = DrawPath.pathy + item[4];
|
||||
px = DrawPath.pathx + item[5];
|
||||
py = DrawPath.pathy + item[6];
|
||||
ctx.bezierCurveTo(cx, cy, c2x, c2y, px, py);
|
||||
DrawPath.relativeMove(item[5], item[6]);
|
||||
break;
|
||||
case 'z': DrawPath.absoluteMove(DrawPath.startx, DrawPath.starty);
|
||||
ctx.lineTo(DrawPath.pathx, DrawPath.pathy);
|
||||
break;
|
||||
default: // Command not implemented
|
||||
break;
|
||||
static drawSection (item, ctx) {
|
||||
var cx, cy, px, py;
|
||||
switch ((String(item[0])).toLowerCase()) {
|
||||
case 'm': DrawPath.absoluteMove(item[1], item[2]);
|
||||
ctx.moveTo(pathx, pathy);
|
||||
startx = item[1]; starty = item[2];
|
||||
break;
|
||||
case 'l': DrawPath.relativeMove(item[1], item[2]); ctx.lineTo(pathx, pathy);
|
||||
break;
|
||||
case 'h': pathx += item[1]; ctx.lineTo(pathx, pathy);
|
||||
break;
|
||||
case 'v': pathy += item[1]; ctx.lineTo(pathx, pathy);
|
||||
break;
|
||||
case 'q':
|
||||
cx = pathx + item[1];
|
||||
cy = pathy + item[2];
|
||||
px = pathx + item[3];
|
||||
py = pathy + item[4];
|
||||
ctx.quadraticCurveTo(cx, cy, px, py);
|
||||
DrawPath.relativeMove(item[3], item[4]);
|
||||
break;
|
||||
case 'c': // beziers
|
||||
cx = pathx + item[1];
|
||||
cy = pathy + item[2];
|
||||
var c2x = pathx + item[3];
|
||||
var c2y = pathy + item[4];
|
||||
px = pathx + item[5];
|
||||
py = pathy + item[6];
|
||||
ctx.bezierCurveTo(cx, cy, c2x, c2y, px, py);
|
||||
DrawPath.relativeMove(item[5], item[6]);
|
||||
break;
|
||||
case 'z': DrawPath.absoluteMove(startx, starty);
|
||||
ctx.lineTo(pathx, pathy);
|
||||
break;
|
||||
default: // Command not implemented
|
||||
break;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
DrawPath.absoluteMove = function (dx, dy) {
|
||||
DrawPath.pathx = dx;
|
||||
DrawPath.pathy = dy;
|
||||
};
|
||||
static absoluteMove (dx, dy) {
|
||||
pathx = dx;
|
||||
pathy = dy;
|
||||
}
|
||||
|
||||
DrawPath.relativeMove = function (dx, dy) {
|
||||
DrawPath.pathx += dx;
|
||||
DrawPath.pathy += dy;
|
||||
};
|
||||
static relativeMove (dx, dy) {
|
||||
pathx += dx;
|
||||
pathy += dy;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue