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

Fix JSHint error. Fix PathKit support for web workers #17

Open
wants to merge 1 commit into
base: master
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
2 changes: 1 addition & 1 deletion src/libraries/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ var flatten = function (arg) {
}
}
return args;
}
};

exports.randomGenerator = randomGenerator;
exports.flatten = flatten;
31 changes: 19 additions & 12 deletions src/libraries/vg/commands/filters.js
Original file line number Diff line number Diff line change
Expand Up @@ -870,11 +870,10 @@ vg._compoundToPoints = function (shape) {
return l1;
};

function cmdToPathKit(cmd) {
if (!window.PathKit) {
function cmdToPathKit(PathKit, cmd) {
if (!PathKit) {
throw new Error('PathKit module not found.');
}
var PathKit = window.PathKit;
if (cmd.type === bezier.MOVETO) {
return [PathKit.MOVE_VERB, cmd.x, cmd.y];
} else if (cmd.type === bezier.LINETO) {
Expand All @@ -888,11 +887,11 @@ function cmdToPathKit(cmd) {

var compoundOpsPathKit;

vg._compoundPathKit = function (shape1, shape2, method) {
if (!window.PathKit) {
vg._compoundPathKit = function (shape1, shape2, method, PathKit) {
if (!PathKit) {
throw new Error('PathKit module not found.');
}
var PathKit = window.PathKit;

if (!compoundOpsPathKit) {
compoundOpsPathKit = {
'union': PathKit.PathOp.UNION,
Expand All @@ -902,8 +901,11 @@ vg._compoundPathKit = function (shape1, shape2, method) {
};
}

var cmds1 = shape1.commands.map(cmdToPathKit);
var cmds2 = shape2.commands.map(cmdToPathKit);
const cmdToPathKitCurried = function(cmd) {
return cmdToPathKit(PathKit, cmd);
};
var cmds1 = shape1.commands.map(cmdToPathKitCurried);
var cmds2 = shape2.commands.map(cmdToPathKitCurried);
var p1 = PathKit.FromCmds(cmds1);
var p2 = PathKit.FromCmds(cmds2);
p1.op(p2, compoundOpsPathKit[method]);
Expand All @@ -928,14 +930,19 @@ vg._compoundPathKit = function (shape1, shape2, method) {
vg.compound = function (shape1, shape2, method) {
if (!shape1.commands) { shape1 = Path.combine(shape1); }
if (!shape2.commands) { shape2 = Path.combine(shape2); }

// With curved input, try returning smooth curves instead of line segmented shapes.
// For this to work, the Skia PathKit webassembly needs to be included into the page and the page served through http or https.
// See https://skia.org/user/modules/pathkit


// XXX jshint doesn't know that self can exist in web workers
/* globals self: any */
if (typeof window !== 'undefined' && window.PathKit && window.PathKit.NewPath) {
return vg._compoundPathKit(shape1, shape2, method);
}
return vg._compoundPathKit(shape1, shape2, method, window.PathKit);
} else if (typeof self !== 'undefined' && self.PathKit && self.PathKit.NewPath) {
return vg._compoundPathKit(shape1, shape2, method, self.PathKit);
}

var contours1 = shape1.resampleByLength(1).contours();
var contours2 = shape2.resampleByLength(1).contours();

Expand Down