Giter Site home page Giter Site logo

mate-h / supershape Goto Github PK

View Code? Open in Web Editor NEW
6.0 1.0 0.0 106 KB

https://observablehq.com/@mateh/bezier-continuous-curves

Home Page: https://supershapes.vercel.app

License: MIT License

HTML 1.58% TypeScript 9.10% JavaScript 53.61% GLSL 35.52% CSS 0.18%

supershape's Introduction

supershape

This codebase implements an approximate curve with a set of control points and parameters that has smooth transitions in the curvature.

Details: https://observablehq.com/@mateh/bezier-continuous-curves

Also see: https://observablehq.com/@mateh/continuous-curvature

Usage

npm install supershape
import { getShapeFunction } from 'supershape'

let points = [
  {x: 0, y: 0},
  {x: 1, y: 0},
  {x: 1, y: 1},
]

const rounding = 20;
const exponent = 5;
const shapeFunction = getShapeFunction(points[0], points[1], points[2], rounding, exponent);

// get points on the curve
const t = 0.5; // t is between 0 and 1
const {curve} = shapeFunction(t);
console.log("x: " + curve.x + ", y: " + curve.y);

Javascript implementation

https://github.com/mate-h/supershape/blob/master/src/main.js

This code is an implementation of a path input tool that allows users to create, edit, and visualize paths composed of line segments and bezier curves. The main components of the code are as follows:

  • getBezier(p0, p1, p2, p3): A function that returns a bezier curve given four control points. It uses the cubic Bezier equation to interpolate the coordinates.

  • error(shapeFn, bezierFn): A function that calculates the error between a shape function and a bezier curve function using a set number of sample points.

  • Helper functions: A collection of functions for performing basic vector operations like addition, subtraction, and multiplication.

  • Main loop: Iterates over groups of points in the path and calculates the bezier curve for each group. The code also includes logic to handle different cases based on the exponent and cross product of the vectors.

  • Path input component: A custom SVG-based user interface for drawing and editing paths. It contains event listeners for user interactions like adding, moving, and removing control points, and also includes functions for drawing paths and control points.

The main output of the code is an SVG element that displays the user-created path, which can be used in other applications or visualizations.

Todos

Remains to be implemented:

  • cornerCurveExpansionFactor: stroke width using adjusted inner radius and continuous curvature
  • uniform stroke width
  • pill shape, compare with SF icons for reference
  • performance improvements: 1 fragment per-vertex
  • adjust rounding per-vertex with gizmo, maxiumum rounding per-vertex
  • box select vertices
  • TypeScript and SVG implementation with bezier approximation

Papers

Optimal G2 Hermite interpolation https://www.sciencedirect.com/science/article/pii/S0010448518304305

supershape's People

Contributors

mate-h avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

supershape's Issues

Rounding features

This issues is to:

  • Support larger rounding parameter than half of the segment length
  • Support rounding per-point

License missing

Hello and thanks for making this! I make lots of SVG tools available on DrawingBots.net and looking at using this package.

This package is missing license info, could you please add it in the repo?

Thank you!

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.