MCPcopy Create free account
hub / github.com/corbanbrook/dsp.js / buildPath

Function buildPath

examples/js/processing.js:10408–10501  ·  view source on GitHub ↗
(d)

Source from the content-addressed store, hash-verified

10406 };
10407
10408 var buildPath = function buildPath(d) {
10409 var c = regex("[A-Za-z][0-9\\- ]+|Z", d);
10410
10411 // Begin storing path object
10412 path = "var path={draw:function(){saveContext();curContext.beginPath();";
10413
10414 x = 0;
10415 y = 0;
10416 cx = 0;
10417 cy = 0;
10418 nx = 0;
10419 ny = 0;
10420 d = 0;
10421 a = 0;
10422 lastCom = "";
10423 lenC = c.length - 1;
10424
10425 // Loop through SVG commands translating to canvas eqivs functions in path object
10426 for (var j = 0; j < lenC; j++) {
10427 var com = c[j][0], xy = regex(getXY, com);
10428
10429 switch (com[0]) {
10430 case "M":
10431 //curContext.moveTo(x,-y);
10432 x = parseFloat(xy[0][0]);
10433 y = parseFloat(xy[1][0]);
10434 path += "curContext.moveTo(" + x + "," + (-y) + ");";
10435 break;
10436
10437 case "L":
10438 //curContext.lineTo(x,-y);
10439 x = parseFloat(xy[0][0]);
10440 y = parseFloat(xy[1][0]);
10441 path += "curContext.lineTo(" + x + "," + (-y) + ");";
10442 break;
10443
10444 case "H":
10445 //curContext.lineTo(x,-y)
10446 x = parseFloat(xy[0][0]);
10447 path += "curContext.lineTo(" + x + "," + (-y) + ");";
10448 break;
10449
10450 case "V":
10451 //curContext.lineTo(x,-y);
10452 y = parseFloat(xy[0][0]);
10453 path += "curContext.lineTo(" + x + "," + (-y) + ");";
10454 break;
10455
10456 case "T":
10457 //curContext.quadraticCurveTo(cx,-cy,nx,-ny);
10458 nx = parseFloat(xy[0][0]);
10459 ny = parseFloat(xy[1][0]);
10460
10461 if (lastCom === "Q" || lastCom === "T") {
10462 d = Math.sqrt(Math.pow(x - cx, 2) + Math.pow(cy - y, 2));
10463 a = Math.PI + Math.atan2(cx - x, cy - y);
10464 cx = x + (Math.sin(a) * (d));
10465 cy = y + (Math.cos(a) * (d));

Callers 1

parseSVGFontFunction · 0.85

Calls 1

regexFunction · 0.85

Tested by

no test coverage detected