Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Arc-related fix for NaN arguments #30

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
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
51 changes: 31 additions & 20 deletions context.js
Original file line number Diff line number Diff line change
Expand Up @@ -396,20 +396,24 @@ export default (function () {
value = this[keys[i]];
if (style.apply) {
//is this a gradient or pattern?
if (value instanceof CanvasPattern) {
//pattern
if (value.__ctx) {
//copy over defs
for(nodeIndex = 0; nodeIndex < value.__ctx.__defs.childNodes.length; nodeIndex++){
node = value.__ctx.__defs.childNodes[nodeIndex];
id = node.getAttribute("id");
this.__ids[id] = id;
this.__defs.appendChild(node);
if (value instanceof CanvasPattern || value.constructor.name === 'CanvasPattern') {
if (value.image) { //MODIFIED
value = this.createPattern(value.image, null); //HINT pulls in original image ref from Konva
//pattern
if (value.__ctx) {
//copy over defs
for(nodeIndex = 0; nodeIndex < value.__ctx.__defs.childNodes.length; nodeIndex++){
node = value.__ctx.__defs.childNodes[nodeIndex];
id = node.getAttribute("id");
this.__ids[id] = id;
this.__defs.appendChild(node);
}
}
currentElement.setAttribute(style.apply, format("url(#{id})", {id:value.__root.getAttribute("id")}));
}
currentElement.setAttribute(style.apply, format("url(#{id})", {id:value.__root.getAttribute("id")}));
}
else if (value instanceof CanvasGradient) {
else if (value instanceof CanvasGradient || value.constructor.name === 'CanvasGradient') {
console.log(value);
//gradient
currentElement.setAttribute(style.apply, format("url(#{id})", {id:value.__root.getAttribute("id")}));
} else if (style.apply.indexOf(type)!==-1 && style.svg !== value) {
Expand Down Expand Up @@ -989,6 +993,13 @@ export default (function () {
* Arc command!
*/
Context.prototype.arc = function (x, y, radius, startAngle, endAngle, counterClockwise) {
x = x || 0;
y = y || 0;
radius = radius || 0;
startAngle = startAngle || 0;
endAngle = endAngle || 0;
counterClockwise = counterClockwise || 0;

// in canvas no circle is drawn if no angle is provided.
if (startAngle === endAngle) {
return;
Expand Down Expand Up @@ -1021,7 +1032,7 @@ export default (function () {
var scaleX = Math.hypot(this.__transformMatrix.a, this.__transformMatrix.b);
var scaleY = Math.hypot(this.__transformMatrix.c, this.__transformMatrix.d);

this.lineTo(startX, startY);
this.lineTo(startX || 0, startY || 0);
this.__addPathCommand(format("A {rx} {ry} {xAxisRotation} {largeArcFlag} {sweepFlag} {endX} {endY}",
{
rx:radius * scaleX,
Expand Down Expand Up @@ -1088,11 +1099,11 @@ export default (function () {

this.__addPathCommand(format("A {rx} {ry} {xAxisRotation} {largeArcFlag} {sweepFlag} {endX} {endY}",
{
rx:radiusX,
ry:radiusY,
xAxisRotation:rotation*(180/Math.PI),
largeArcFlag:largeArcFlag,
sweepFlag:sweepFlag,
rx:radiusX,
ry:radiusY,
xAxisRotation:rotation*(180/Math.PI),
largeArcFlag:largeArcFlag,
sweepFlag:sweepFlag,
endX:endX,
endY:endY
}));
Expand Down Expand Up @@ -1326,7 +1337,7 @@ export default (function () {
* @see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/translate
*/
Context.prototype.translate = function (x, y) {
const matrix = this.getTransform().translate(x, y);
const matrix = this.getTransform().translate(x || 0, y || 0);
this.setTransform(matrix);
};

Expand All @@ -1346,7 +1357,7 @@ export default (function () {
}

/**
*
*
* @returns The scale component of the transform matrix as {x,y}.
*/
Context.prototype.__getTransformScale = function() {
Expand All @@ -1357,7 +1368,7 @@ export default (function () {
}

/**
*
*
* @returns The rotation component of the transform matrix in radians.
*/
Context.prototype.__getTransformRotation = function() {
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 3 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,24 +1,19 @@
{
"name": "svgcanvas",
"version": "2.5.0",
"description": "svgcanvas",
"name": "@symbolic/svgcanvas",
"version": "2.5.1",
"description": "@symbolic/svgcanvas",
"main": "dist/svgcanvas.js",
"scripts": {
"watch": "rollup -c -w",
"build": "rollup -c",
"prepublishOnly": "npm run build",
"test": "karma start"
},
"repository": {
"type": "git",
"url": "https://github.com/zenozeng/svgcanvas.git"
},
"keywords": [
"canvas",
"svg",
"canvas2svg"
],
"author": "Zeno Zeng",
"license": "MIT",
"devDependencies": {
"@rollup/plugin-commonjs": "^21.0.2",
Expand Down