Skip to content

Commit 045413b

Browse files
committed
Fixed with all files in test/example
1 parent 33ff423 commit 045413b

File tree

2 files changed

+48
-52
lines changed

2 files changed

+48
-52
lines changed

canvas2svg.js

Lines changed: 47 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -591,73 +591,54 @@
591591
ctx.prototype.beginPath = function () {
592592
// Note that there is only one current default path, it is not part of the drawing state.
593593
// See also: https://html.spec.whatwg.org/multipage/scripting.html#current-default-path
594-
this.__currentDefaultPath = "";
594+
this.__currentPath = "";
595595
this.__currentPosition = {};
596596
};
597597

598-
/**
599-
* Helper function to apply currentDefaultPath to current path element
600-
* @private
601-
*/
602-
ctx.prototype.__applyCurrentDefaultPath = function () {
603-
var path = this.__currentDefaultPath;
604-
if (!path) {
605-
return
606-
}
607-
608-
var element = this.__currentElement;
609-
// if (element.nodeName !== "path") {
610-
var group = this.__closestGroupOrSvg();
611-
element = this.__currentElement = this.__createElement("path", {}, true);
612-
group.appendChild(element);
613-
// }
614-
615-
element.setAttribute("d", path);
616-
};
617-
618598
/**
619599
* Adds the move command to the current path element,
620600
* if the currentPathElement is not empty create a new path element
621601
*/
622602
ctx.prototype.moveTo = function (x, y) {
623603
// creates a new subpath with the given point
624-
this.__currentPosition = {x, y};
625-
this.__currentDefaultPath += `M ${x} ${y}`;
604+
setCurrentpath.call(this, x, y, `M ${x} ${y}`);
626605
};
627606

628607
/**
629608
* Closes the current path
630609
*/
631610
ctx.prototype.closePath = function () {
632-
if (this.__currentDefaultPath) {
633-
this.__currentDefaultPath += "Z";
611+
if (this.__currentPath) {
612+
this.__currentPath += "Z";
634613
}
635614
};
636615

637616
/**
638617
* Adds a line to command
639618
*/
640619
ctx.prototype.lineTo = function (x, y) {
641-
this.__currentPosition = {x, y};
642-
this.__currentDefaultPath += `L ${x} ${y}`;
620+
setCurrentpath.call(this, x, y, `L ${x} ${y}`);
643621
};
644622

645623
/**
646624
* Add a bezier command
647625
*/
648626
ctx.prototype.bezierCurveTo = function (cp1x, cp1y, cp2x, cp2y, x, y) {
649-
this.__currentPosition = {x, y};
650-
this.__currentDefaultPath += `C ${cp1x} ${cp1y} ${cp2x} ${cp2y} ${x} ${y}`;
627+
setCurrentpath.call(this, x, y, `C ${cp1x} ${cp1y} ${cp2x} ${cp2y} ${x} ${y}`);
651628
};
652629

653630
/**
654631
* Adds a quadratic curve to command
655632
*/
656633
ctx.prototype.quadraticCurveTo = function (cpx, cpy, x, y) {
657-
this.__currentPosition = {x, y};
658-
this.__currentDefaultPath += `Q ${cpx} ${cpy} ${x} ${y}`;
634+
setCurrentpath.call(this, x, y, `Q ${cpx} ${cpy} ${x} ${y}`);
659635
};
660636

637+
function setCurrentpath(x, y, value) {
638+
this.__currentPosition = {x, y};
639+
this.__currentPath || (this.__currentPath = "");
640+
this.__currentPath += value;
641+
}
661642

662643
/**
663644
* Adds the arcTo to the current path
@@ -744,8 +725,8 @@
744725
var endAngle = getAngle(unit_vec_origin_end_tangent);
745726

746727
// Connect the point (x0, y0) to the start tangent point by a straight line
747-
moveOrLineTo(x + unit_vec_origin_start_tangent[0] * radius,
748-
y + unit_vec_origin_start_tangent[1] * radius);
728+
moveOrLineTo.call(this, x + unit_vec_origin_start_tangent[0] * radius,
729+
y + unit_vec_origin_start_tangent[1] * radius);
749730

750731
// Connect the start tangent point to the end tangent point by arc
751732
// and adding the end tangent point to the subpath.
@@ -776,49 +757,65 @@
776757
* Sets the stroke property on the current element
777758
*/
778759
ctx.prototype.stroke = function () {
779-
var element = getOrCreateElementToApplyStyleTo.call(this, "stroke", "fill");
760+
getOrCreateElementToApplyStyleTo.call(this, "stroke", "fill");
780761
};
781762

782763
function getOrCreateElementToApplyStyleTo(paintMethod1, paintMethod2) {
783-
var currentPath = this.__currentDefaultPath;
764+
var currentPath = this.__currentPath;
784765
if (!currentPath) {
785766
return
786767
}
787768

788769
var element = this.__currentElement;
789770
var group = this.__closestGroupOrSvg();
790771
var matrixString = this.__currentMatrix.toString();
791-
var extras = group.__extras || (group.__extras = {})
792-
if (extras[paintMethod1] || extras[paintMethod2] && extras.matrixString !== matrixString) {
793-
var pathHasNoChange = currentPath === extras.currentPath;
772+
var state = group.__state || (group.__state = {})
773+
if (state[paintMethod1] || state[paintMethod2] && state.matrixString !== matrixString) {
774+
var pathHasNoChange = currentPath === state.currentPath;
794775
if (pathHasNoChange) {
795776
/** Convert <path> to <def> **/
796777
if (element.nodeName === "path") {
797778
convertPathToDef.call(this, group);
798779
}
799780

800781
/** Append <use> element references <def> **/
801-
element = appendUseElement.call(this, group, extras.id);
782+
element = appendUseElement.call(this, group, state.id);
802783
} else {
803-
this.__applyCurrentDefaultPath();
784+
applyCurrentDefaultPath.call(this, true);
804785
}
805786
} else {
806-
this.__applyCurrentDefaultPath();
787+
applyCurrentDefaultPath.call(this, false);
807788
}
808789

809790
element.setAttribute("paint-order", `${paintMethod2} ${paintMethod1} markers`);
810791

811-
extras[paintMethod1] = true;
812-
extras.currentPath = currentPath;
813-
extras.matrixString = matrixString;
792+
state[paintMethod1] = true;
793+
state.currentPath = currentPath;
794+
state.matrixString = matrixString;
814795

815796
this.__applyStyleToCurrentElement(paintMethod1);
816797
this.__applyTransform(element, paintMethod1);
817798

818799
return element;
819800
};
820801

821-
function hashString(string) {
802+
function applyCurrentDefaultPath(needsNewPath) {
803+
var element = this.__currentElement;
804+
var path = this.__currentPath;
805+
if (!path) {
806+
return
807+
}
808+
809+
if (needsNewPath || element.nodeName !== "path") {
810+
var group = this.__closestGroupOrSvg();
811+
element = this.__currentElement = this.__createElement("path", {}, true);
812+
group.appendChild(element);
813+
}
814+
815+
element.setAttribute("d", path);
816+
};
817+
818+
function hashString(string) {
822819
/** https://github.com/darkskyapp/string-hash **/
823820
let hash = 5381;
824821
let i = string.length;
@@ -830,7 +827,7 @@
830827
var element = this.__currentElement;
831828

832829
/** Create <path> in <defs> **/
833-
var extras = group.__extras
830+
var extras = group.__state
834831
var id = extras.id
835832
if (!id) {
836833
id = extras.id = `path-${hashString(extras.currentPath)}`;
@@ -1150,12 +1147,12 @@
11501147
ry = radius,
11511148
xAxisRotation = 0;
11521149

1153-
this.__currentDefaultPath += `A ${rx} ${ry} ${xAxisRotation} ${largeArcFlag} ${sweepFlag} ${endX} ${endY}`;
1150+
this.__currentPath += `A ${rx} ${ry} ${xAxisRotation} ${largeArcFlag} ${sweepFlag} ${endX} ${endY}`;
11541151
this.__currentPosition = {x: endX, y: endY};
11551152
};
11561153

11571154
function moveOrLineTo(x, y) {
1158-
if (this.__currentDefaultPath) {
1155+
if (this.__currentPath) {
11591156
this.lineTo(x, y);
11601157
} else {
11611158
this.moveTo(x, y);
@@ -1166,11 +1163,11 @@
11661163
* Generates a ClipPath from the clip command.
11671164
*/
11681165
ctx.prototype.clip = function () {
1169-
this.__applyCurrentDefaultPath();
1166+
applyCurrentDefaultPath.call(this, false);
11701167

11711168
var group = this.__closestGroupOrSvg(),
11721169
clipPath = this.__createElement("clipPath"),
1173-
id = randomString(this.__ids),
1170+
id = randomString(this.__ids),
11741171
newGroup = this.__createElement("g");
11751172

11761173
this.__currentElement.remove();

test/playground.html

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,7 @@ <h5>Or try your own!</h5>
152152
eval(`window.fn = function (ctx) {${atob(id)}}`);
153153
fn = window.fn;
154154
} catch (err) {
155-
console.error(err);
156-
fn = undefined;
155+
fn = C2S_EXAMPLES['arc'];
157156
}
158157
}
159158

0 commit comments

Comments
 (0)