Skip to content

Instantly share code, notes, and snippets.

@pushkine
Last active March 21, 2025 11:44
Show Gist options
  • Select an option

  • Save pushkine/fbc7cf18e0a40ffb02b3b3a20b74f4f1 to your computer and use it in GitHub Desktop.

Select an option

Save pushkine/fbc7cf18e0a40ffb02b3b3a20b74f4f1 to your computer and use it in GitHub Desktop.

Revisions

  1. pushkine revised this gist Sep 20, 2022. No changes.
  2. pushkine revised this gist Sep 20, 2022. No changes.
  3. pushkine revised this gist Aug 1, 2021. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions cubicBezier.ts
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@
    /** MIT License github.com/pushkine/ */
    function cubicBezier(x1: number, y1: number, x2: number, y2: number) {
    if (!(x1 >= 0 && x1 <= 1 && x2 >= 0 && x2 <= 1))
    throw new Error(`CubicBezier x1 & x2 values must be { 0 < x < 1 }, got { x1 : ${x1}, x2: ${x2} }`);
  4. pushkine revised this gist Apr 26, 2021. No changes.
  5. pushkine revised this gist Sep 2, 2020. No changes.
  6. pushkine revised this gist Sep 2, 2020. 1 changed file with 0 additions and 5 deletions.
    5 changes: 0 additions & 5 deletions LICENSE
    Original file line number Diff line number Diff line change
    @@ -1,5 +0,0 @@
    License MIT
    Copyright 2020 S. Belski
    Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
    The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  7. pushkine revised this gist Sep 2, 2020. No changes.
  8. pushkine revised this gist Sep 2, 2020. No changes.
  9. pushkine revised this gist Sep 2, 2020. 3 changed files with 24 additions and 30 deletions.
    5 changes: 5 additions & 0 deletions LICENSE
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,5 @@
    License MIT
    Copyright 2020 S. Belski
    Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
    The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
    30 changes: 0 additions & 30 deletions cubicBezier.js
    Original file line number Diff line number Diff line change
    @@ -1,30 +0,0 @@
    /**
    *
    * License MIT
    * Copyright 2020 S. Belski
    * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
    * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
    * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
    *
    * Cubic Bézier solving function
    *
    */
    const cubicBezier = (x1, y1, x2, y2) => {
    if (!(x1 >= 0 && x1 <= 1 && x2 >= 0 && x2 <= 1))
    throw new Error(`CubicBezier x1 & x2 values must be { 0 < x < 1 }, got { x1 : ${x1}, x2: ${x2} }`);
    const ax = 1.0 - (x2 = 3.0 * (x2 - x1) - (x1 *= 3.0)) - x1,
    ay = 1.0 - (y2 = 3.0 * (y2 - y1) - (y1 *= 3.0)) - y1;
    let i = 0, r = 0.0, s = 0.0, d = 0.0, x = 0.0;
    return (t) => {
    for (r = t, i = 0; 32 > i; i++)
    if (1e-5 > Math.abs((x = r * (r * (r * ax + x2) + x1) - t))) return r * (r * (r * ay + y2) + y1);
    else if (1e-5 > Math.abs((d = r * (r * ax * 3.0 + x2 * 2.0) + x1))) break;
    else r -= x / d;
    if ((s = 0.0) > (r = t)) return 0;
    else if ((d = 1.0) < r) return 1;
    while (d > s)
    if (1e-5 > Math.abs((x = r * (r * (r * ax + x2) + x1)) - t)) break;
    else t > x ? (s = r) : (d = r), (r = 0.5 * (d - s) + s);
    return r * (r * (r * ay + y2) + y1);
    };
    };
    19 changes: 19 additions & 0 deletions cubicBezier.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    function cubicBezier(x1: number, y1: number, x2: number, y2: number) {
    if (!(x1 >= 0 && x1 <= 1 && x2 >= 0 && x2 <= 1))
    throw new Error(`CubicBezier x1 & x2 values must be { 0 < x < 1 }, got { x1 : ${x1}, x2: ${x2} }`);
    const ax = 1.0 - (x2 = 3.0 * (x2 - x1) - (x1 *= 3.0)) - x1,
    ay = 1.0 - (y2 = 3.0 * (y2 - y1) - (y1 *= 3.0)) - y1;
    let i = 0, r = 0.0, s = 0.0, d = 0.0, x = 0.0;
    return (t: number) => {
    for (r = t, i = 0; 32 > i; i++)
    if (1e-5 > Math.abs((x = r * (r * (r * ax + x2) + x1) - t))) return r * (r * (r * ay + y2) + y1);
    else if (1e-5 > Math.abs((d = r * (r * ax * 3.0 + x2 * 2.0) + x1))) break;
    else r -= x / d;
    if ((s = 0.0) > (r = t)) return 0;
    else if ((d = 1.0) < r) return 1;
    while (d > s)
    if (1e-5 > Math.abs((x = r * (r * (r * ax + x2) + x1)) - t)) break;
    else t > x ? (s = r) : (d = r), (r = 0.5 * (d - s) + s);
    return r * (r * (r * ay + y2) + y1);
    };
    };
  10. pushkine revised this gist Aug 27, 2020. No changes.
  11. pushkine revised this gist Jun 2, 2020. 1 changed file with 12 additions and 1 deletion.
    13 changes: 12 additions & 1 deletion cubicBezier.js
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,15 @@
    export const cubicBezier = (x1, y1, x2, y2) => {
    /**
    *
    * License MIT
    * Copyright 2020 S. Belski
    * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
    * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
    * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
    *
    * Cubic Bézier solving function
    *
    */
    const cubicBezier = (x1, y1, x2, y2) => {
    if (!(x1 >= 0 && x1 <= 1 && x2 >= 0 && x2 <= 1))
    throw new Error(`CubicBezier x1 & x2 values must be { 0 < x < 1 }, got { x1 : ${x1}, x2: ${x2} }`);
    const ax = 1.0 - (x2 = 3.0 * (x2 - x1) - (x1 *= 3.0)) - x1,
  12. pushkine revised this gist May 19, 2020. 1 changed file with 1 addition and 5 deletions.
    6 changes: 1 addition & 5 deletions cubicBezier.js
    Original file line number Diff line number Diff line change
    @@ -3,11 +3,7 @@ export const cubicBezier = (x1, y1, x2, y2) => {
    throw new Error(`CubicBezier x1 & x2 values must be { 0 < x < 1 }, got { x1 : ${x1}, x2: ${x2} }`);
    const ax = 1.0 - (x2 = 3.0 * (x2 - x1) - (x1 *= 3.0)) - x1,
    ay = 1.0 - (y2 = 3.0 * (y2 - y1) - (y1 *= 3.0)) - y1;
    let i = 0,
    r = 0.0,
    s = 0.0,
    d = 0.0,
    x = 0.0;
    let i = 0, r = 0.0, s = 0.0, d = 0.0, x = 0.0;
    return (t) => {
    for (r = t, i = 0; 32 > i; i++)
    if (1e-5 > Math.abs((x = r * (r * (r * ax + x2) + x1) - t))) return r * (r * (r * ay + y2) + y1);
  13. pushkine created this gist May 19, 2020.
    23 changes: 23 additions & 0 deletions cubicBezier.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,23 @@
    export const cubicBezier = (x1, y1, x2, y2) => {
    if (!(x1 >= 0 && x1 <= 1 && x2 >= 0 && x2 <= 1))
    throw new Error(`CubicBezier x1 & x2 values must be { 0 < x < 1 }, got { x1 : ${x1}, x2: ${x2} }`);
    const ax = 1.0 - (x2 = 3.0 * (x2 - x1) - (x1 *= 3.0)) - x1,
    ay = 1.0 - (y2 = 3.0 * (y2 - y1) - (y1 *= 3.0)) - y1;
    let i = 0,
    r = 0.0,
    s = 0.0,
    d = 0.0,
    x = 0.0;
    return (t) => {
    for (r = t, i = 0; 32 > i; i++)
    if (1e-5 > Math.abs((x = r * (r * (r * ax + x2) + x1) - t))) return r * (r * (r * ay + y2) + y1);
    else if (1e-5 > Math.abs((d = r * (r * ax * 3.0 + x2 * 2.0) + x1))) break;
    else r -= x / d;
    if ((s = 0.0) > (r = t)) return 0;
    else if ((d = 1.0) < r) return 1;
    while (d > s)
    if (1e-5 > Math.abs((x = r * (r * (r * ax + x2) + x1)) - t)) break;
    else t > x ? (s = r) : (d = r), (r = 0.5 * (d - s) + s);
    return r * (r * (r * ay + y2) + y1);
    };
    };