Giter VIP home page Giter VIP logo

Comments (14)

Pomax avatar Pomax commented on September 12, 2024 1

alright, added a Bezier.fromSVG(...) that you can use to build a curve off of an SVG string now, too. And for good measure the project's on npmjs.com now too as https://www.npmjs.com/package/bezier-js

from bezierjs.

RobertDaleSmith avatar RobertDaleSmith commented on September 12, 2024

I have been using this..

pointsToSVGPathString: function(points){
    var str = "";
    points.forEach(function(point, i){
        if(i==0) str+="M";
        else if(i==1 && points.length > 2) str+="C";
        else str+=" ";
        str = str + point.x + " " + point.y;
    });
    return str;
}

Hope this helps.

from bezierjs.

Pomax avatar Pomax commented on September 12, 2024

looks like the trailing space was missing in https://github.com/Pomax/bezierjs/blob/gh-pages/lib/bezier.js#L502 - note that the spaces around operators (like M 123 234 or ... 12 C 34 45 ... are considered perfectly fine according to the spec)

from bezierjs.

Pomax avatar Pomax commented on September 12, 2024

Updated the code to do something more sensible, by simply aggregating all the terms and joining them on a single space. Should work better now?

from bezierjs.

RobertDaleSmith avatar RobertDaleSmith commented on September 12, 2024

Thanks, all looks good now. 👍

from bezierjs.

RobertDaleSmith avatar RobertDaleSmith commented on September 12, 2024

With SVG string output, it would be awesome if I could do the reverse:

var bezier = new Bezier("M 1031 325 C 1031 500 1271 530 1250 417");

or

bezier.update("M 1031 325 C 1031 500 1271 530 1250 417");

from bezierjs.

Pomax avatar Pomax commented on September 12, 2024

hm, not a bad idea, although also something that you can do pretty easily outside of the library right now with:

// assuming absolute SVG:
var svgstring = "M 1031 325 C 1031 500 1271 530 1250 417";
var coords = svgstring.match(/(\d+) (\d+) /g).map(function(v) {
  return { x: v[0], y: v[1] };
});
return new Bezier(coords)

For relative coords things get trickier, but only a little (exploiting sort()'s ability to iterate over two elements rather than a single one to track and correct the relative position)

from bezierjs.

behdad avatar behdad commented on September 12, 2024

Really curious about what kind of abuse you are talking re sort() :).

from bezierjs.

Pomax avatar Pomax commented on September 12, 2024
var svgstring = "m 1031 325 c 1031 500 1271 530 1250 417";
var list = svgstring.match(/(\d+) (\d+) /g).map(function(v) {
  v = v.split(" ").map(function(v) { return parseFloat(v); });
  return { x: v[0], y: v[1] };
});
var coords = [ list[0] ];
list.sort(function(a,b) {
  b.x += a.x;
  b.y += a.y;
  coords.push({ x: b.x, y: b.y });
});

resolving relative by simply adding the new value to the old value

from bezierjs.

behdad avatar behdad commented on September 12, 2024

Does sort() in JS make any guarantees about in what order it compares things?!

from bezierjs.

Pomax avatar Pomax commented on September 12, 2024

officially? no. But I haven't seen a single case where it reorders based on "equal" in any browser... yet =) I was hoping to use .reduce for the same purposes, but that only tracks the running value, not the actual object in the list.

It's not too much more code to use a plain for loop, but this was a fun little hack =P

from bezierjs.

behdad avatar behdad commented on September 12, 2024

officially? no. But I haven't seen a single case where it reorders based on "equal" in any browser... yet =)

:)

I was hoping to use .reduce for the same purposes, but that only tracks the running value, not the actual object in the list.

I don't understand why you can't do the same with reduce. Like:

list.reduce(function(a,b) {
  b.x += a.x;
  b.y += a.y;
  coords.push({ x: b.x, y: b.y });
  return b;
});

It's not too much more code to use a plain for loop, but this was a fun little hack =P

from bezierjs.

Pomax avatar Pomax commented on September 12, 2024

oh right, you could just return b. "Derp".

from bezierjs.

RobertDaleSmith avatar RobertDaleSmith commented on September 12, 2024

👍 You rock Pomax!

from bezierjs.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.