An implementation of Bézier curve rendering and manipulation, using the HTML5 Canvas API.

Overview

Bézier Curves

An implementation of Bézier curve rendering and manipulation, using the HTML5 Canvas API.

A quartic Bézier curve.

How does it work?

Bézier curves can be simply explained as nested linear interpolations.

What is Interpolation?

Interpolation is defined as "The process of finding, from the given values of a function for certain values of the variable, its approximate value for an intermediate value of the variable."

Simply put, it is a function where you can plug in a start point, end point, and a percentage to get a point in between the start and end.

Interpolation can follow virtually any function, such as a Sigmoid curve, spherical interpolation (a.k.a. 'slerp'), and many more.

The type of interpolation used to create Bézier curves is linear, where a change in that percentage is proportional to the outputted value. This is commonly referred to as 'lerp'.

So, for example, if you were trying to find the midpoint of 10 and 20:

// This will linearly interpolate between 'start' and 'end' with the percentage 't'.
function lerp(start, end, t) {
    return (1 - t) * start + t * end;
}

const midpoint = lerp(10, 20, 0.5); // 0.5 = 50%, a.k.a. halfway from 10 to 20.
console.log(midpoint); // This should print 15.

If you're still confused, see Interpolation.

What are Bézier Curves?

Bézier curves are constructed using at least 3 control points, up to an infinite amount of control points.

These curves are classified by their 'degree', which is found by number of control points - 1.

  • A curve with 3 control points has a degree of 2
  • A curve with 4 control points has a degree of 3
  • A curve with 5 control points has a degree of 4
  • etc. for higher degrees

Curves also have common names:

  • (degree 2) Quadratic
  • (degree 3) Cubic
  • (degree 4) Quartic
  • (degree 5) Quintic
  • etc. for higher degrees

These curves are aptly named; they form curves similar to the curves that polynomials of the same degree would form.

For example, a quadratic Bézier curve (degree 2) forms a curve that is comparable to a quadratic polynomial such as f(x) = 2x^2 + 3x - 4, and a cubic Bézier curve (degree 3) forms a curve that is comparable to a cubic polynomial such as f(x) = 4x^3 + x^2 - 3x + 7.

Bézier curves are calculated by recursively interpolating along the vectors that connect the control points.

For example, to interpolate between two points:

// This linearly interpolates between two 2D coordinates.
function lerpXY(x0, y0, x1, y1, t) {
    return [
        (1 - t) * x0 + t * x1,
        (1 - t) * y0 + t * y1
    ];
}

// 0.5 = 50%, a.k.a. halfway from (10, 10) to (20, 20)
const intermediatePoint = lerpXY(10, 10, 20, 20, 0.5);
console.log(intermediatePoint); // This should print [ 15, 15 ].

After finding the interpolated points, these can be connected to form new vectors. There will be one less interpolated vector after each iteration, so after a sufficient number of iterations for the curve's degree will condense the interpolated value into a single point. This point is then rendered to form that point of the curve.

And then the process repeats for every value of t, which forms the shape of the curve.

If you're still confused, see Bézier Curve.

You might also like...

Financial lightweight charts built with HTML5 canvas

Lightweight Charts Demos | Documentation | Discord community TradingView Lightweight Charts are one of the smallest and fastest financial HTML5 charts

Jan 9, 2023

Interactive visualizations of time series using JavaScript and the HTML canvas tag

Interactive visualizations of time series using JavaScript and the HTML canvas tag

dygraphs JavaScript charting library The dygraphs JavaScript library produces interactive, zoomable charts of time series: Learn more about it at dygr

Jan 3, 2023

🍞🎨 Full-featured photo image editor using canvas. It is really easy, and it comes with great filters.

🍞🎨 Full-featured photo image editor using canvas. It is really easy, and it comes with great filters.

Full featured image editor using HTML5 Canvas. It's easy to use and provides powerful filters. Packages toast-ui.image-editor - Plain JavaScript compo

Jan 6, 2023

Demonstration of liquid effect on HTML Canvas using Matter.js and SVG Filters (Blur + Contrast)

Demonstration of liquid effect on HTML Canvas using Matter.js and SVG Filters (Blur + Contrast)

Canvas Liquid Effect Demonstration of liquid (or gooey) effect on HTML Canvas using Matter.js and SVG Filters (feGaussianBlur and feColorMatrix). DEMO

Dec 24, 2022

Babylon.js is a powerful, beautiful, simple, and open game and rendering engine packed into a friendly JavaScript framework.

Babylon.js Getting started? Play directly with the Babylon.js API using our playground. It also contains a lot of samples to learn how to use it. Any

Jan 4, 2023

🦍• [Work in Progress] React Renderer to build UI interfaces using canvas/WebGL

🦍• [Work in Progress] React Renderer to build UI interfaces using canvas/WebGL

React Ape React Ape is a react renderer to build UI interfaces using canvas/WebGL. React Ape was built to be an optional React-TV renderer. It's mainl

Jan 4, 2023

Lightweight, interactive planning tool that visualizes a series of tasks using an HTML canvas

Lightweight, interactive planning tool that visualizes a series of tasks using an HTML canvas

Planner Lightweight, interactive planning tool that visualizes a series of tasks using an HTML canvas. Try it yourself at plannerjs.dev Plans created

Jan 2, 2023

Bring data to life with SVG, Canvas and HTML. :bar_chart::chart_with_upwards_trend::tada:

D3: Data-Driven Documents D3 (or D3.js) is a JavaScript library for visualizing data using web standards. D3 helps you bring data to life using SVG, C

Jan 3, 2023

JavaScript SVG parser and renderer on Canvas

canvg JavaScript SVG parser and renderer on Canvas. It takes the URL to the SVG file or the text of the SVG file, parses it in JavaScript and renders

Jan 4, 2023
Owner
nethe550
nethe550
Canvas rendering library, Sprite manipulation of canvas

el-canvas Canvas rendering library, Sprite manipulation of canvas hello world <div id="app"><div></div></div> yarn add elem-canvas or npm i

null 15 Apr 13, 2022
Javascript Canvas Library, SVG-to-Canvas (& canvas-to-SVG) Parser

Fabric.js Fabric.js is a framework that makes it easy to work with HTML5 canvas element. It is an interactive object model on top of canvas element. I

Fabric.js 23.6k Jan 3, 2023
Javascript Canvas Library, SVG-to-Canvas (& canvas-to-SVG) Parser

Fabric.js Fabric.js is a framework that makes it easy to work with HTML5 canvas element. It is an interactive object model on top of canvas element. I

Fabric.js 23.6k Jan 3, 2023
Konva.js is an HTML5 Canvas JavaScript framework that extends the 2d context by enabling canvas interactivity for desktop and mobile applications.

Konva Konva is an HTML5 Canvas JavaScript framework that enables high performance animations, transitions, node nesting, layering, filtering, caching,

konva 8.7k Jan 8, 2023
An HTML5/Canvas implementation of 8-bit color cycling

Overview Here is the JavaScript and C++ source code to my color cycling engine, written in 2010. I am releasing it under the LGPL v3.0. The package co

Joseph Huckaby 8 Dec 1, 2022
React + Canvas = Love. JavaScript library for drawing complex canvas graphics using React.

React Konva React Konva is a JavaScript library for drawing complex canvas graphics using React. It provides declarative and reactive bindings to the

konva 4.9k Jan 9, 2023
The Swiss Army Knife of Vector Graphics Scripting – Scriptographer ported to JavaScript and the browser, using HTML5 Canvas. Created by @lehni & @puckey

Paper.js - The Swiss Army Knife of Vector Graphics Scripting If you want to work with Paper.js, simply download the latest "stable" version from http:

Paper.js 13.5k Dec 30, 2022
📸 Generate image using HTML5 canvas and SVG

egami → image README | 中文文档 Generate image using HTML5 canvas and SVG Fork from html-to-image Installation pnpm pnpm add egami npm npm i egami Usage i

weng 12 Jan 3, 2023
Simple HTML5 Charts using the tag

Simple yet flexible JavaScript charting for designers & developers Documentation Currently, there are two versions of the library (2.9.4 and 3.x.x). V

Chart.js 59.4k Jan 7, 2023
🔥 JavaScript Library for HTML5 canvas based heatmaps

heatmap.js Dynamic Heatmaps for the Web. How to get started The fastest way to get started is to install heatmap.js with bower. Just run the following

Patrick Wied 5.9k Jan 2, 2023