How to visualize Fourier series / Fourier coefficients? - javascript
I'm currently having difficulties at visualizing Fourier series. I tried the same thing about three times in order to find errors but in vain.
Now I even don't know what is wrong with my code or understanding of Fourier series.
What I'm trying to make is a thing like shown in the following Youtube video: https://youtu.be/r6sGWTCMz2k
I think I know what is Fourier series a bit. I can prove this by showing my previous works:
(1) square wave approximation
(2) parameter
So now I would like to draw more complicated thing in a parametric way. Please let me show the process I've walked.
① From svg path, get coordinates. For example,
// svg path
const d = 'M 0 0 L 20 30 L 10 20 ... ... ... Z';
↓
↓ convert with some processing...
↓
const cx = [0, 20, 10, ...]; // function Fx(t)
const cy = [0, 30, 20, ...]; // function Fy(t)
② Get Fourier coefficients from Fx(t), Fy(t), respectively. After that, I can get approximated coordinates by calculating Fourier series respectively by using the coefficients I got. For example,
Let's say I have a0_x, an_x, bn_x, a0_y, an_y, bn_y.
Then, Fx(t) = a0_x + an_x[1] * cos(1wt) + bn_x[1] * cos(1wt)
+ an_x[2] * cos(2wt) + bn_x[2] * cos(2wt) + ...;
Fy(t) = a0_y + an_y[1] * cos(1wt) + bn_y[1] * cos(1wt)
+ an_y[2] * cos(2wt) + bn_y[2] * cos(2wt) + ...;
Therefore a set of points (Fx(t), Fy(t)) is an approximated path!
This is all! Only thing left is just drawing!
Meanwhile, I processed the data in the following way:
const d = [svg path data];
const split = d.split(/[, ]/);
const points = get_points(split);
const normalized = normalize(points);
const populated = populate(normalized, 8);
const cx = populated.x; // Fx(t)
const cy = populated.y; // Fy(t)
/**
* This function does the below job.
* populate([0,3,6], 2) => output 0 12 3 45 6
* populate([0,4,8], 3) => output 0 123 4 567 8
*/
function populate(data, n) {
if (data.x.length <= 1) throw new Error('NotEnoughData');
if (n < 1) throw new Error('InvalidNValue');
const arr_x = new Array(data.x.length + (data.x.length - 1) * n);
const arr_y = new Array(data.y.length + (data.y.length - 1) * n);
for (let i = 0; i < data.x.length; i++) {
arr_x[i * (n + 1)] = data.x[i];
arr_y[i * (n + 1)] = data.y[i];
}
for (let i = 0; i <= arr_x.length - n - 1 - 1; i += (n + 1)) {
const x_interpolation = (arr_x[i + n + 1] - arr_x[i]) / (n + 1);
const y_interpolation = (arr_y[i + n + 1] - arr_y[i]) / (n + 1);
for (let j = 1; j <= n; j++) {
arr_x[i + j] = arr_x[i] + x_interpolation * j;
arr_y[i + j] = arr_y[i] + y_interpolation * j;
}
}
return { x: arr_x, y: arr_y };
}
// This function makes all values are in range of [-1, 1].
// I just did it... because I don't want to deal with big numbers (and not want numbers having different magnitude depending on data).
function normalize(obj) {
const _x = [];
const _y = [];
const biggest_x = Math.max(...obj.x);
const smallest_x = Math.min(...obj.x);
const final_x = Math.max(Math.abs(biggest_x), Math.abs(smallest_x));
const biggest_y = Math.max(...obj.y);
const smallest_y = Math.min(...obj.y);
const final_y = Math.max(Math.abs(biggest_y), Math.abs(smallest_y));
for (let i = 0; i < obj.x.length; i++) {
_x[i] = obj.x[i] / final_x;
_y[i] = obj.y[i] / final_y;
}
return { x: _x, y: _y };
}
// returns Fx(t) and Fy(t) from svg path data
function get_points(arr) {
const x = [];
const y = [];
let i = 0;
while (i < arr.length) {
const path_command = arr[i];
if (path_command === "M") {
x.push(Number(arr[i + 1]));
y.push(Number(arr[i + 2]));
i += 3;
} else if (path_command === 'm') {
if (i === 0) {
x.push(Number(arr[i + 1]));
y.push(Number(arr[i + 2]));
i += 3;
} else {
x.push(x.at(-1) + Number(arr[i + 1]));
y.push(y.at(-1) + Number(arr[i + 2]));
i += 3;
}
} else if (path_command === 'L') {
x.push(Number(arr[i + 1]));
y.push(Number(arr[i + 2]));
i += 3;
} else if (path_command === 'l') {
x.push(x.at(-1) + Number(arr[i + 1]));
y.push(y.at(-1) + Number(arr[i + 2]));
i += 3;
} else if (path_command === 'H') {
x.push(Number(arr[i + 1]));
y.push(y.at(-1));
i += 2;
} else if (path_command === 'h') {
x.push(x.at(-1) + Number(arr[i + 1]));
y.push(y.at(-1));
i += 2;
} else if (path_command === 'V') {
x.push(x.at(-1));
y.push(Number(arr[i + 1]));
i += 2;
} else if (path_command === 'v') {
x.push(x.at(-1));
y.push(y.at(-1) + Number(arr[i + 1]));
i += 2;
} else if (path_command === 'Z' || path_command === 'z') {
i++;
console.log('reached to z/Z, getting points done');
} else if (path_command === 'C' || path_command === 'c' || path_command === 'S' || path_command === 's' || path_command === 'Q' || path_command === 'q' || path_command === 'T' || path_command === 't' || path_command === 'A' || path_command === 'a') {
throw new Error('unsupported path command, getting points aborted');
} else {
x.push(x.at(-1) + Number(arr[i]));
y.push(y.at(-1) + Number(arr[i + 1]));
i += 2;
}
}
return { x, y };
}
Meanwhile, in order to calculate Fourier coefficients, I used numerical integration. This is the code.
/**
* This function calculates Riemann sum (area approximation using rectangles).
* #param {Number} div division number (= number of rectangles to be used)
* #param {Array | Function} subject subject of integration
* #param {Number} start where to start integration
* #param {Number} end where to end integration
* #param {Number} nth this parameter will be passed to 'subject'
* #param {Function} paramFn this parameter will be passed to 'subject'
* #returns {Number} numerical-integrated value
*/
function numerical_integration(div, subject, start, end, nth = null, paramFn = null) {
if (div < 1) throw new Error(`invalid div; it can't be 0 or 0.x`);
let sum = 0;
const STEP = 1 / div;
const isSubjectArray = Array.isArray(subject);
if (isSubjectArray) {
for (let t = start; t < end; t++) {
for (let u = 0; u < div; u++) {
sum += subject[t + 1] * STEP;
}
}
} else {
for (let t = start; t < end; t++) {
for (let u = 0; u < div; u++) {
const period = end - start;
const isParamFnArray = Array.isArray(paramFn);
if (isParamFnArray) sum += subject((t + 1), period, nth, paramFn) * STEP;
else sum += subject(((t + STEP) + STEP * u), period, nth, paramFn) * STEP;
}
}
}
return sum;
// console.log(numerical_integration(10, (x) => x ** 3, 0, 2));
}
The approximation is near. For (x) => x, division 10, from 0 to 2, the approximation is 2.1 while actual answer is 2. For (x) => x ** 2, division 10, from 0 to 2, the approximation is 2.87, while actual answer is 2.67. For (x) => x ** 3, division 10, from 0 to 2, the approximation is 4.41, while actual answer is 4.
And I found a0, an, bn by the following: (※ You can find Fourier coefficients formulas in my previous question)
/**
* This function will be passed to 'getAn' function.
* #param {Number} t this function is a function of time
* #param {Number} period period of a function to be integrated
* #param {Number} nth integer multiple
* #param {Array | Function} paramFn
* #returns {Number} computed value
*/
function fc(t, period, nth, paramFn) {
const isParamFnArray = Array.isArray(paramFn);
const w = 2 * Math.PI / period;
if (isParamFnArray) return paramFn[t] * Math.cos(nth * w * t);
else return paramFn(t) * Math.cos(nth * w * t);
}
// This function will be passed to 'getBn' function.
function fs(t, period, nth, paramFn) {
const isParamFnArray = Array.isArray(paramFn);
const w = 2 * Math.PI / period;
if (isParamFnArray) return paramFn[t] * Math.sin(nth * w * t);
else return paramFn(t) * Math.sin(nth * w * t);
}
/**
* This function returns a0 value.
* #param {Number} period period of a function to be integrated
* #param {Array | Function} intgFn function to be intergrated
* #param {Number} div number of rectangles to use
* #returns {Number} a0 value
*/
// Why * 30? in order to scale up
// Why - 1? because arr[arr.length] is undefined.
function getA0(period, intgFn, div) {
return 30 * numerical_integration(div, intgFn, 0, period - 1) / period;
}
/**
* This function returns an values.
* #param {Number} period period of a function to be integrated
* #param {Number} div number of rectangles to use
* #param {Number} howMany number of an values to be calculated
* #param {Array | Function} paramFn function to be integrated
* #returns {Array} an values
*/
function getAn(period, div, howMany, paramFn) {
const an = [];
for (let n = 1; n <= howMany; n++) {
const value = 30 * numerical_integration(div, fc, 0, period - 1, n, paramFn) * 2 / period;
an.push(value);
}
return an;
}
// This function returns bn values.
function getBn(period, div, howMany, paramFn) {
const bn = [];
for (let n = 1; n <= howMany; n++) {
const value = 30 * numerical_integration(div, fs, 0, period - 1, n, paramFn) * 2 / period;
bn.push(value);
}
return bn;
}
const xa0 = getA0(cx.length, cx, 10);
const xan = getAn(cx.length, 10, 100, cx);
const xbn = getBn(cx.length, 10, 100, cx);
const ya0 = getA0(cy.length, cy, 10);
const yan = getAn(cy.length, 10, 100, cy);
const ybn = getBn(cy.length, 10, 100, cy);
However, the result was not a thing I wanted... It was a weird shape... Maybe this is life...
The below is the canvas drawing code:
const $cvs = document.createElement('canvas');
const cctx = $cvs.getContext('2d');
$cvs.setAttribute('width', 1000);
$cvs.setAttribute('height', 800);
$cvs.setAttribute('style', 'border: 1px solid black;');
document.body.appendChild($cvs);
window.requestAnimationFrame(draw_tick);
// offset
const xoo = { x: 200, y: 600 }; // x oscillator offset
const yoo = { x: 600, y: 200 }; // y ~
// path
const path = [];
// drawing function
let deg = 0;
function draw_tick() {
const rAF = window.requestAnimationFrame(draw_tick);
// initialize
cctx.clearRect(0, 0, 1000, 800);
// y oscillator
const py = { x: 0, y: 0 };
// a0
// a0 circle
cctx.beginPath();
cctx.strokeStyle = 'black';
cctx.arc(yoo.x + py.x, yoo.y + py.y, Math.abs(ya0), 0, 2 * Math.PI);
cctx.stroke();
// a0 line
cctx.beginPath();
cctx.strokeStyle = 'black';
cctx.moveTo(yoo.x + py.x, yoo.y + py.y);
py.x += ya0 * Math.cos(0 * deg * Math.PI / 180);
py.y += ya0 * Math.sin(0 * deg * Math.PI / 180);
cctx.lineTo(yoo.x + py.x, yoo.y + py.y);
cctx.stroke();
// an
for (let i = 0; i < yan.length; i++) {
const radius = yan[i];
// an circles
cctx.beginPath();
cctx.strokeStyle = 'black';
cctx.arc(yoo.x + py.x, yoo.y + py.y, Math.abs(radius), 0, 2 * Math.PI);
cctx.stroke();
// an lines
cctx.beginPath();
cctx.strokeStyle = 'black';
cctx.moveTo(yoo.x + py.x, yoo.y + py.y);
py.x += radius * Math.cos((i+1) * deg * Math.PI / 180);
py.y += radius * Math.sin((i+1) * deg * Math.PI / 180);
cctx.lineTo(yoo.x + py.x, yoo.y + py.y);
cctx.stroke();
}
// bn
for (let i = 0; i < ybn.length; i++) {
const radius = ybn[i];
// bn circles
cctx.beginPath();
cctx.strokeStyle = 'black';
cctx.arc(yoo.x + py.x, yoo.y + py.y, Math.abs(radius), 0, 2 * Math.PI);
cctx.stroke();
// bn lines
cctx.beginPath();
cctx.strokeStyle = 'black';
cctx.moveTo(yoo.x + py.x, yoo.y + py.y);
py.x += radius * Math.cos((i+1) * deg * Math.PI / 180);
py.y += radius * Math.sin((i+1) * deg * Math.PI / 180);
cctx.lineTo(yoo.x + py.x, yoo.y + py.y);
cctx.stroke();
}
// x oscillator
const px = { x: 0, y: 0 };
// a0
// a0 circle
cctx.beginPath();
cctx.strokeStyle = 'black';
cctx.arc(yoo.x + py.x, yoo.y + py.y, Math.abs(xa0), 0, 2 * Math.PI);
cctx.stroke();
// a0 line
cctx.beginPath();
cctx.strokeStyle = 'black';
cctx.moveTo(yoo.x + py.x, yoo.y + py.y);
py.x += xa0 * Math.cos(0 * deg * Math.PI / 180);
py.y += xa0 * Math.sin(0 * deg * Math.PI / 180);
cctx.lineTo(yoo.x + py.x, yoo.y + py.y);
cctx.stroke();
// an
for (let i = 0; i < xan.length; i++) {
const radius = xan[i];
// an circles
cctx.beginPath();
cctx.strokeStyle = 'black';
cctx.arc(xoo.x + px.x, xoo.y + px.y, Math.abs(radius), 0, 2 * Math.PI);
cctx.stroke();
// an lines
cctx.beginPath();
cctx.strokeStyle = 'black';
cctx.moveTo(xoo.x + px.x, xoo.y + px.y);
px.x += radius * Math.cos((i+1) * deg * Math.PI / 180);
px.y += radius * Math.sin((i+1) * deg * Math.PI / 180);
cctx.lineTo(xoo.x + px.x, xoo.y + px.y);
cctx.stroke();
}
// bn
for (let i = 0; i < xbn.length; i++) {
const radius = xbn[i];
// bn circles
cctx.beginPath();
cctx.strokeStyle = 'black';
cctx.arc(xoo.x + px.x, xoo.y + px.y, Math.abs(radius), 0, 2 * Math.PI);
cctx.stroke();
// bn lines
cctx.beginPath();
cctx.strokeStyle = 'black';
cctx.moveTo(xoo.x + px.x, xoo.y + px.y);
px.x += radius * Math.cos((i+1) * deg * Math.PI / 180);
px.y += radius * Math.sin((i+1) * deg * Math.PI / 180);
cctx.lineTo(xoo.x + px.x, xoo.y + px.y);
cctx.stroke();
}
// y oscillator line
cctx.strokeStyle = 'black';
cctx.beginPath();
cctx.moveTo(yoo.x + py.x, yoo.y + py.y);
cctx.lineTo(xoo.x + px.x, yoo.y + py.y);
cctx.stroke();
// x oscillator line
cctx.strokeStyle = 'black';
cctx.beginPath();
cctx.moveTo(xoo.x + px.x, xoo.y + px.y);
cctx.lineTo(xoo.x + px.x, yoo.y + py.y);
cctx.stroke();
// path
path.push({ x: px.x, y: py.y });
cctx.beginPath();
cctx.strokeStyle = 'black';
cctx.moveTo(200 + path[0].x, 200 + path[0].y);
for (let i = 0; i < path.length; i++) {
cctx.lineTo(200 + path[i].x, 200 + path[i].y);
}
cctx.stroke();
// degree update
if (deg === 359) {
window.cancelAnimationFrame(rAF);
} else {
deg++;
}
}
So! I decided to be logical. First, I checked whether the converted path data is correct by drawing it at canvas. The below is the canvas code and the data.
let count = 0;
function draw_tick2() {
const rAF = window.requestAnimationFrame(draw_tick2);
const s = 100; // scale up
// initialize
cctx.clearRect(0, 0, 1000, 800);
cctx.beginPath();
// 200 has no meaning I just added it to move the path.
for (let i = 0; i < count; i++) {
if (i === 0) cctx.moveTo(200 + s * cx[i], 200 + s * cy[i]);
else cctx.lineTo(200 + s * cx[i], 200 + s * cy[i]);
}
cctx.stroke();
if (count < cx.length - 1) {
count++;
} else {
window.cancelAnimationFrame(rAF);
}
}
const paimon = 'm 0,0 -2.38235,-2.87867 -1.58823,-1.29045 -1.9853,-0.893384 -3.17647,-0.39706 1.58824,-1.98529 1.09191,-2.08456 v -2.38235 l -0.79412,-2.87868 1.88603,2.18383 1.6875,1.88602 1.78677,0.99265 1.78676,0.39706 1.78676,-0.19853 -1.6875,1.58824 -0.69485,1.68749 -0.0993,2.084564 0.39706,2.18383 9.62867,3.87132 2.77941,1.9853 4.66544,-1.09192 3.07721,-1.88603 1.9853,-2.58088 -3.97059,0.49633 -3.375,-0.79412 -2.87868,-2.58088 -2.08456,-3.077214 2.38235,1.48897 2.08456,0.19853 3.57353,-0.89338 2.58089,-2.48162 -3.07721,0.39706 -3.87132,-1.88603 -2.97794,-2.08456 -2.48162,-2.87868 -3.87133,-4.06985 -4.06985,-2.68015 -5.95588,-2.58088 -5.85662,-0.79412 -5.45956,0.99265 0.59559,1.6875 -0.99265,1.09191 -0.79412,3.47427 -1.29044,-2.97794 -0.89338,-1.19118 0.79412,-1.48897 1.6875,-0.79412 0.39706,-3.772057 1.48897,1.290441 1.78676,0.09926 -2.08456,-1.985293 1.78677,-0.893382 4.36765,-0.19853 4.86397,0.992648 1.19117,1.091912 -2.38235,1.985301 3.17647,-0.49633 2.87868,-2.680149 -3.57353,-2.580881 -5.45956,-1.488972 h -4.46691 l -3.6728,-3.176471 -0.79412,1.389706 -0.79411,-1.488969 0.69485,-0.595588 -1.58824,-3.871325 -0.39706,3.672795 -0.69485,0.297794 0.89338,1.091911 v 1.091912 h -1.19113 l -0.59559,-0.992648 -1.98529,2.878677 -4.06986,1.588236 -4.26838,1.985293 3.27574,3.871329 2.87867,1.88603 2.58088,0.29779 -2.58088,-1.58823 -0.89338,-2.084566 4.86397,-0.992645 -1.19118,2.382351 h 1.58824 l 1.48897,-1.88603 0.29779,2.77942 -2.38235,2.38235 -3.57353,2.87868 -3.97059,4.86397 -2.08456,3.67279 -2.58088,2.58088 -2.68015,1.09192 -3.17647,0.0993 -1.3897,-0.69485 1.09191,3.17647 2.18382,3.573534 3.375,2.38235 -1.78676,5.85662 -1.38971,6.05514 0.39706,4.36765 1.38971,4.66544 3.87132,4.46691 -0.79412,-3.57352 -0.49632,-4.06986 v -2.48162 l 1.78676,5.85662 3.07721,3.17647 3.07721,1.29044 3.37499,0.79412 2.28309,-0.89338 0.69486,-1.48897 -1.19118,0.49632 -2.48162,-1.98529 -2.28309,-2.87868 2.28309,2.48162 h 0.99265 l 0.69485,-0.49632 0.2978,-1.19118 0.0993,-0.79412 -0.89339,0.59559 -1.58823,-0.99265 -1.29044,-1.3897 -1.19118,-2.38236 -0.89338,-4.86397 -0.0993,-4.56617 0.29779,-4.96324 0.39706,0.89338 1.19118,-0.44669 0.0496,-0.89338 1.09191,0.69485 1.48897,0.2978 1.53861,0.89338 0.99264,0.64522 h -0.79411 l 0.49632,2.43199 -0.44669,1.58823 -1.78676,0.39706 -1.24081,-1.24081 -0.24817,-1.43934 0.84375,-0.94301 1.19118,-0.49633 1.14154,0.94302 0.24816,1.14154 -0.0993,1.48897 -1.83639,0.64523 -1.58824,-1.53861 -0.44669,-1.48897 -0.24816,-2.18382 -1.43934,0.99264 0.0496,-0.99264 -0.44669,1.78676 0.69485,3.12684 1.09192,4.26838 1.78676,1.78677 6.89889,3.02757 -2.53124,0.99265 -3.17647,1.3897 -0.79412,0.39706 0.59559,0.39706 1.34007,-0.69485 0.0496,1.19117 1.98529,-0.39705 2.68015,-0.44669 -0.2978,-1.93567 0.79412,1.58824 2.82905,-0.44669 4.06985,-1.34008 1.04229,-0.59559 -0.2978,-1.78676 -0.34743,-1.73713 -4.9136,2.48162 -2.58088,0.94301 -3.17648,-4.81434 1.53861,0.49633 1.3897,0.0496 1.43935,-0.24816 -1.34008,0.24816 h -1.58824 l -1.41452,-0.54596 3.12684,4.78953 2.63052,-0.89339 4.86397,-2.4568 2.65533,-2.08456 0.39706,-5.90625 -0.84375,1.5386 -1.14155,0.54596 -1.5386,0.19853 -1.29044,-0.89338 -0.59559,-1.09191 -0.24816,-1.73714 0.24816,-1.3897 -2.08456,0.54595 -0.29779,-0.34742 0.34743,-0.49633 0.64522,-0.39706 1.5386,-0.39705 2.18382,-0.19853 1.24081,0.0993 1.14154,0.54596 0.4467,1.43934 -0.19853,1.63786 -0.59559,1.29044 -1.24081,0.89339 -1.43934,-0.39706 -0.99264,-1.09191 -0.0496,-1.19118 0.79412,-0.89338 0.89338,-0.44669 1.19118,-0.0496 0.64522,1.04228 0.34742,0.79412 -0.14889,1.14155 0.99265,-0.4467 0.29779,-1.34007 -0.19853,-4.06985 -1.93566,-0.44669 -2.53125,-1.6875 -2.23346,-1.88603 -2.23345,-4.069864 -0.44669,3.920964 0.64522,4.21875 1.5386,3.92096 0.74448,0.44669 h -1.73713 l -2.18383,-0.54596 -3.12684,-2.08456 -1.58823,-2.28309 -1.14154,-2.08456 -1.29044,-3.871324 -1.38971,2.481624 -1.48897,2.63051 -0.94302,1.9853 3.8217,-6.948534 1.29044,3.672794 2.33272,3.92096 2.9283,2.13419 0.49633,0.44669 2.28309,0.49632 h 1.63787 l -0.69485,-0.69485 -0.84375,-1.93566 -1.34008,-5.80698 0.44669,-3.970594 2.33273,4.069854 4.56617,3.47426 2.08456,0.59559 0.19853,2.82905 -0.0496,3.97058 -0.0993,6.00552 -0.54595,3.02757 -1.58824,2.77941 -1.5386,0.89339 -1.19118,0.24816 -1.48897,-0.69485 -0.69485,-0.1489 0.69485,1.24081 1.43934,1.6875 2.68015,1.19117 3.17647,0.2978 3.77206,-2.23346 1.3897,-2.77941 0.89339,-3.82169 0.0496,-3.375 0.14889,6.25368 -1.14154,5.11213 -2.08456,3.27573 -2.08456,1.6875 -1.88603,0.59559 -2.28308,-0.79412 1.78676,1.6875 4.9136,1.88603 2.43199,0.2978 2.68015,-0.39706 2.72977,-1.09191 3.62317,-3.27574 0.89338,-3.97059 0.49632,-3.57353 -0.0993,-2.87867 -0.39706,-3.17647 -0.49632,-3.07721 1.98529,3.47427 1.19117,2.18382 0.39706,1.29044 0.39706,-2.28309 -0.39706,-3.0772 -1.29044,-3.77206 -1.29044,-2.87868 -1.6875,-3.27573 -10.125,-4.16912 z';
This is ★Paimon chan★ from a computer game 'Genshin Impact'. Thus it is proved that there are no flaws at the data, since all the data is plotted correctly.
Next, I plotted the approximated (Fx(t), Fy(t)) points so that I can check whether there is a problem. And It turned out that there was a problem. But I don't understand what is the problem. At the same time this path is interesting; The beginning part of the path seems like the hairpin.
This is the drawing code:
function approxFn(t) {
let x = xa0;
let y = ya0;
for (let i = 0; i < xan.length; i++) {
x += xan[i] * Math.cos(2 * Math.PI * i * t / cx.length);
x += xbn[i] * Math.sin(2 * Math.PI * i * t / cx.length);
y += yan[i] * Math.cos(2 * Math.PI * i * t / cx.length);
y += ybn[i] * Math.sin(2 * Math.PI * i * t / cx.length);
}
return { x, y };
}
function draw_tick3() {
const rAF = window.requestAnimationFrame(draw_tick3);
const s = 5;
// initialize
cctx.clearRect(0, 0, 1000, 800);
cctx.beginPath();
for (let t = 0; t < count; t++) {
if (count === 0) cctx.moveTo(200 + s * approxFn(t).x, 200 + s * approxFn(t).y);
else cctx.lineTo(200 + s * approxFn(t).x, 200 + s * approxFn(t).y);
}
cctx.stroke();
if (count < cx.length - 1) {
count++;
} else {
window.cancelAnimationFrame(rAF);
}
}
The above is all the code in my js file. In where I made a mistake? It's a mystery! I know this question is exceptionally seriously long question. But please help me! I want to realize Paimon chan! ㅠwㅠ
※ (This section is irrelevant with the question) Meanwhile I made a success to draw the path in a complex number plane. If you're interested, please see my work... I would like to add circle things to this but I have no idea what is 'radius' in this case.
// You can see that I used real part for x and imaginary part for y.
for (let i = 0; i <= count; i++) {
if (i === 0) {
cctx.moveTo(coords[i].real * scaler + paimonPosition, coords[i].imag * scaler + paimonPosition);
} else {
cctx.lineTo(coords[i].real * scaler + paimonPosition, coords[i].imag * scaler + paimonPosition);
}
}
And this is the result. But what makes me confused is a case of cn = -5000 ~ 5000. As far as I understand, more cn, more accurate as original wave. But why it crashes when cn is so big?
Anyways, thank you very much for reading this long question!
(the character shown: Paimon from Genshin Impact)
Hello myself!
First, errors in your code...
You did not consider a case where sequence of values come after drawing command. For example, your get_points function can't handle a case like h 0 1 2.
Current get_points function can't handle second m drawing command. You need to manually join strings if you have multiple paths.
You need to manually set m x y to m 0 0. Otherwise you can't see canvas drawing. (Maybe values are too too small to draw)
Second, in brief, you can't draw a shape with rotating vectors having fixed magnitude, if you approximate f(t) in a xy plane. It's because what you approximated is not a shape itself, but shape's coordinates.
Third, the reason you got weird shape when you tried to plot approximated data is at your approxFn() function.
x += xan[i] * Math.cos(2 * Math.PI * i * t / cx.length);
x += xbn[i] * Math.sin(2 * Math.PI * i * t / cx.length);
y += yan[i] * Math.cos(2 * Math.PI * i * t / cx.length);
y += ybn[i] * Math.sin(2 * Math.PI * i * t / cx.length);
not t, (t + 1) is correct. Your approximated data has no problem.
Fourth, so you need to take a complex plane approach if you want rotating vectors. In this case, the radius of circles are the magnitude of a sum vector of a real part vector and an imaginary part vector (Pythagorean theorem).
Fifth, In Cn formula, you missed 1 / T.
Sixth, The reason it crashed is... I don't know the exact reason but I think numerical integration and/or finding Cn is wrong. The new code I wrote don't crash at high Cn.
p.s. I wrote some writings about Fourier series. Please see if you are interested: https://logic-finder.github.io/memory/FourierSeriesExploration/opening/opening-en.html
Related
p5.js - Low FPS for some basic animations
I'm having really bad performance on a project i wrote in Javascript (with the p5.js library) Here is the code: const fps = 60; const _width = 400; const _height = 300; const firePixelChance = 1; const coolingRate = 1; const heatSourceSize = 10; const noiseIncrement = 0.02; const fireColor = [255, 100, 0, 255]; const bufferWidth = _width; const bufferHeight = _height; let buffer1; let buffer2; let coolingBuffer; let ystart = 0.0; function setup() { createCanvas(_width, _height); frameRate(fps); buffer1 = createGraphics(bufferWidth, bufferHeight); buffer2 = createGraphics(bufferWidth, bufferHeight); coolingBuffer = createGraphics(bufferWidth, bufferHeight); } // Draw a line at the bottom function heatSource(buffer, rows, _color) { const start = bufferHeight - rows; for (let x = 0; x < bufferWidth; x++) { for (let y = start; y < bufferHeight; y++) { if(Math.random() >= firePixelChance) continue; buffer.pixels[(x + (y * bufferWidth)) * 4] = _color[0]; // Red buffer.pixels[(x + (y * bufferWidth)) * 4 +1] = _color[1]; // Green buffer.pixels[(x + (y * bufferWidth)) * 4 +2] = _color[2]; // Blue buffer.pixels[(x + (y * bufferWidth)) * 4 +3] = 255; // Alpha } } } // Produces the 'smoke' function coolingMap(buffer){ let xoff = 0.0; for(x = 0; x < bufferWidth; x++){ xoff += noiseIncrement; yoff = ystart; for(y = 0; y < bufferHeight; y++){ yoff += noiseIncrement; n = noise(xoff, yoff); bright = pow(n, 3) * 20; buffer.pixels[(x + (y * bufferWidth)) * 4] = bright; buffer.pixels[(x + (y * bufferWidth)) * 4 +1] = bright; buffer.pixels[(x + (y * bufferWidth)) * 4 +2] = bright; buffer.pixels[(x + (y * bufferWidth)) * 4 +3] = bright; } } ystart += noiseIncrement; } // Change color of a pixel so it looks like its smooth function smoothing(buffer, _buffer2, _coolingBuffer) { for (let x = 0; x < bufferWidth; x++) { for (let y = 0; y < bufferHeight; y++) { // Get all 4 neighbouring pixels const left = getColorFromPixelPosition(x+1,y,buffer.pixels); const right = getColorFromPixelPosition(x-1,y,buffer.pixels); const bottom = getColorFromPixelPosition(x,y+1,buffer.pixels); const top = getColorFromPixelPosition(x,y-1,buffer.pixels); // Set this pixel to the average of those neighbours let sumRed = left[0] + right[0] + bottom[0] + top[0]; let sumGreen = left[1] + right[1] + bottom[1] + top[1]; let sumBlue = left[2] + right[2] + bottom[2] + top[2]; let sumAlpha = left[3] + right[3] + bottom[3] + top[3]; // "Cool down" color const coolingMapColor = getColorFromPixelPosition(x,y,_coolingBuffer.pixels) sumRed = (sumRed / 4) - (Math.random() * coolingRate) - coolingMapColor[0]; sumGreen = (sumGreen / 4) - (Math.random() * coolingRate) - coolingMapColor[1]; sumBlue = (sumBlue / 4) - (Math.random() * coolingRate) - coolingMapColor[2]; sumAlpha = (sumAlpha / 4) - (Math.random() * coolingRate) - coolingMapColor[3]; // Make sure we dont get negative numbers sumRed = sumRed > 0 ? sumRed : 0; sumGreen = sumGreen > 0 ? sumGreen : 0; sumBlue = sumBlue > 0 ? sumBlue : 0; sumAlpha = sumAlpha > 0 ? sumAlpha : 0; // Update this pixel _buffer2.pixels[(x + ((y-1) * bufferWidth)) * 4] = sumRed; // Red _buffer2.pixels[(x + ((y-1) * bufferWidth)) * 4 +1] = sumGreen; // Green _buffer2.pixels[(x + ((y-1) * bufferWidth)) * 4 +2] = sumBlue; // Blue _buffer2.pixels[(x + ((y-1) * bufferWidth)) * 4 +3] = sumAlpha; // Alpha } } } function draw() { background(0); text("FPS: "+Math.floor(frameRate()), 10, 20); fill(0,255,0,255); buffer1.loadPixels(); buffer2.loadPixels(); coolingBuffer.loadPixels(); heatSource(buffer1, heatSourceSize, fireColor); coolingMap(coolingBuffer); smoothing(buffer1, buffer2, coolingBuffer); buffer1.updatePixels(); buffer2.updatePixels(); coolingBuffer.updatePixels(); let temp = buffer1; buffer1 = buffer2; buffer2 = temp; image(buffer2, 0, 0); // Draw buffer to screen // image(coolingBuffer, 0, bufferHeight); // Draw buffer to screen } function mousePressed() { buffer1.fill(fireColor); buffer1.noStroke(); buffer1.ellipse(mouseX, mouseY, 100, 100); } function getColorFromPixelPosition(x, y, pixels) { let _color = []; for (let i = 0; i < 4; i++) _color[i] = pixels[(x + (y * bufferWidth)) * 4 + i]; return _color; } function getRandomColorValue() { return Math.floor(Math.random() * 255); } I'm getting ~12 FPS on chrome and ~1 FPS on any other browser and i cant figure out why.. Resizing my canvas to make it bigger also impacts the fps negatively... In the devtools performance tab i noticed that both my smoothing and coolingMap functions are the things slowing it down, but i cant figure out what part of them are so heavy..
You've pretty much answered this for yourself already: i'm starting to think this is normal and i should work on caching stuff and maybe use pixel groups instead of single pixels Like you're discovering, doing some calculation for every single pixel is pretty slow. Computers only have finite resources, and there's going to be a limit to what you can throw at them. In your case, you might consider drawing the whole thing to a canvas once at startup, and then moving the canvas up over the life of the program.
Canvas problems. Not able to reproduce design
I need to build canvas animation like design requires. I spend almost 3 days but I'm not able to do anything like in design. Here a REQUESTED design!. And here - what I've got for now: current implementation which definitely not what requested from design .I need only animation of planet from particles at background (also whole process of animation changes in time, it starts from few particles but then amount growing and movings directions of particles changes) here my current code: export class CanvasComponent implements OnInit { sphereRad = 280; radius_sp = 1; distance = 600; particle_size = 0.7; constructor() { } ngOnInit() { this.canvasApp(); } canvasApp () { const canvas = document.querySelector('canvas'); const context = canvas.getContext('2d'); canvas.width = window.innerWidth; canvas.height = window.innerHeight; let displayWidth; let displayHeight; let wait; let count; let numToAddEachFrame; let particleList; let recycleBin; let particleAlpha; let r, g, b; let fLen; let m; let projCenterX; let projCenterY; let zMax; let turnAngle; let turnSpeed; let sphereCenterX, sphereCenterY, sphereCenterZ; let particleRad; let zeroAlphaDepth; let randAccelX, randAccelY, randAccelZ; let gravity; let rgbString; // we are defining a lot of letiables used in the screen update functions globally so that they don't have to be redefined every frame. let p; let outsideTest; let nextParticle; let sinAngle; let cosAngle; let rotX, rotZ; let depthAlphaFactor; let i; let theta, phi; let x0, y0, z0; // INITIALLI const init = () => { wait = 1; count = wait - 1; numToAddEachFrame = 30; // particle color r = 255; g = 255; b = 255; rgbString = 'rgba(' + r + ',' + g + ',' + b + ','; // partial string for color which will be completed by appending alpha value. particleAlpha = 1; // maximum alpha displayWidth = canvas.width; displayHeight = canvas.height; fLen = this.distance; // represents the distance from the viewer to z=0 depth. // projection center coordinates sets location of origin projCenterX = displayWidth / 2; projCenterY = displayHeight / 2; // we will not draw coordinates if they have too large of a z-coordinate (which means they are very close to the observer). zMax = fLen - 2; particleList = {}; recycleBin = {}; // random acceleration factors - causes some random motion randAccelX = 0.1; randAccelY = 0.1; randAccelZ = 0.1; gravity = -0; // try changing to a positive number (not too large, for example 0.3), or negative for floating upwards. particleRad = this.particle_size; sphereCenterX = 0; sphereCenterY = 0; sphereCenterZ = -3 - this.sphereRad; // alpha values will lessen as particles move further back, causing depth-based darkening: zeroAlphaDepth = 0; turnSpeed = 2 * Math.PI / 1200; // the sphere will rotate at this speed (one complete rotation every 1600 frames). turnAngle = 0; // initial angle // timer = setInterval(onTimer, 10 / 24); onTimer(); } const onTimer = () => { // if enough time has elapsed, we will add new particles. count++; if (count >= wait) { count = 0; for (i = 0; i < numToAddEachFrame; i++) { theta = Math.random() * 2 * Math.PI; phi = Math.acos(Math.random() * 2 - 1); x0 = this.sphereRad * Math.sin(phi) * Math.cos(theta); y0 = this.sphereRad * Math.sin(phi) * Math.sin(theta); z0 = this.sphereRad * Math.cos(phi); // We use the addParticle function to add a new particle. The parameters set the position and velocity components. // Note that the velocity parameters will cause the particle to initially fly outwards away from the sphere center (after // it becomes unstuck). const p = addParticle(x0, sphereCenterY + y0, sphereCenterZ + z0, 0.002 * x0, 0.002 * y0, 0.002 * z0); // we set some 'envelope' parameters which will control the evolving alpha of the particles. p.attack = 50; p.hold = 50; p.decay = 100; p.initValue = 0; p.holdValue = particleAlpha; p.lastValue = 0; // the particle will be stuck in one place until this time has elapsed: p.stuckTime = 90 + Math.random() * 20; p.accelX = 0; p.accelY = gravity; p.accelZ = 0; } } // update viewing angle turnAngle = (turnAngle + turnSpeed) % (2 * Math.PI); sinAngle = Math.sin(turnAngle); cosAngle = Math.cos(turnAngle); // background fill context.fillStyle = '#000000'; context.fillRect(0, 0, displayWidth, displayHeight); // update and draw particles p = particleList.first; while (p != null) { // before list is altered record next particle nextParticle = p.next; // update age p.age++; // if the particle is past its 'stuck' time, it will begin to move. if (p.age > p.stuckTime) { p.velX += p.accelX + randAccelX * (Math.random() * 2 - 1); p.velY += p.accelY + randAccelY * (Math.random() * 2 - 1); p.velZ += p.accelZ + randAccelZ * (Math.random() * 2 - 1); p.x += p.velX; p.y += p.velY; p.z += p.velZ; } /* We are doing two things here to calculate display coordinates. The whole display is being rotated around a vertical axis, so we first calculate rotated coordinates for x and z (but the y coordinate will not change). Then, we take the new coordinates (rotX, y, rotZ), and project these onto the 2D view plane. */ rotX = cosAngle * p.x + sinAngle * (p.z - sphereCenterZ); rotZ = -sinAngle * p.x + cosAngle * (p.z - sphereCenterZ) + sphereCenterZ; // m = this.radius_sp * fLen / (fLen - rotZ); m = this.radius_sp; p.projX = rotX * m + projCenterX; p.projY = p.y * m + projCenterY; p.projZ = rotZ * m + projCenterX; // update alpha according to envelope parameters. if (p.age < p.attack + p.hold + p.decay) { if (p.age < p.attack) { p.alpha = (p.holdValue - p.initValue) / p.attack * p.age + p.initValue; } else if (p.age < p.attack + p.hold) { p.alpha = p.holdValue; } else if (p.age < p.attack + p.hold + p.decay) { p.alpha = (p.lastValue - p.holdValue) / p.decay * (p.age - p.attack - p.hold) + p.holdValue; } } else { p.dead = true; } // see if the particle is still within the viewable range. if ((p.projX > displayWidth) || (p.projX < 0) || (p.projY < 0) || (p.projY > displayHeight) || (rotZ > zMax)) { outsideTest = true; } else { outsideTest = false; } if (outsideTest || p.dead || (p.projX > displayWidth / (2 + (1 - Math.random())) && p.projZ + displayWidth * 0.1 > displayWidth / 2) || (p.projX < displayWidth / (2 - (1 - Math.random())) && p.projZ + displayWidth * 0.25 < displayWidth / 2) ) { recycle(p); } else { // depth-dependent darkening // console.log(turnAngle, rotZ) depthAlphaFactor = 1; // depthAlphaFactor = (1 - (1.5 + rotZ / 100)); depthAlphaFactor = (depthAlphaFactor > 1) ? 1 : ((depthAlphaFactor < 0) ? 0 : depthAlphaFactor); context.fillStyle = rgbString + depthAlphaFactor * p.alpha + ')'; // draw context.beginPath(); context.arc(p.projX, p.projY, m * particleRad, 0, 2 * Math.PI, false); context.closePath(); context.fill(); } p = nextParticle; } window.requestAnimationFrame(onTimer); } const addParticle = (x0, y0, z0, vx0, vy0, vz0) => { let newParticle; // const color; // check recycle bin for available drop: if (recycleBin.first != null) { newParticle = recycleBin.first; // remove from bin if (newParticle.next != null) { recycleBin.first = newParticle.next; newParticle.next.prev = null; } else { recycleBin.first = null; } } else { newParticle = {}; } // if the recycle bin is empty, create a new particle (a new empty object): // add to beginning of particle list if (particleList.first == null) { particleList.first = newParticle; newParticle.prev = null; newParticle.next = null; } else { newParticle.next = particleList.first; particleList.first.prev = newParticle; particleList.first = newParticle; newParticle.prev = null; } // initialize newParticle.x = x0; newParticle.y = y0; newParticle.z = z0; newParticle.velX = vx0; newParticle.velY = vy0; newParticle.velZ = vz0; newParticle.age = 0; newParticle.dead = false; if (Math.random() < 0.5) { newParticle.right = true; } else { newParticle.right = false; } return newParticle; } const recycle = (p) => { // remove from particleList if (particleList.first === p) { if (p.next != null) { p.next.prev = null; particleList.first = p.next; } else { particleList.first = null; } } else { if (p.next == null) { p.prev.next = null; } else { p.prev.next = p.next; p.next.prev = p.prev; } } // add to recycle bin if (recycleBin.first == null) { recycleBin.first = p; p.prev = null; p.next = null; } else { p.next = recycleBin.first; recycleBin.first.prev = p; recycleBin.first = p; p.prev = null; } }; init(); } } So I will be happy with any help also REWARD(for full implementation) is possible (ETH, BTC any currency you wish).
Why are my stars drawing twice as large as they should on HTML5 canvas?
As the final part of my flag, I need to loop a star 50 times for my USA flag to save me the hassle of creating new 50 new stars. Iv'e worked out how to create 50 stars in a loop, the only problem is, they all come out twice as big as the other. Can anyone spot the problem in my code? class Star extends drawable{ constructor(x = 0,y = 0, outerRadius = 0, innerRadius = 0, numberOfPoints = 0, lineWidth = 0,fillStyle = "#000",strokeStyle = "transparent",context){ super(context,x,y,fillStyle,strokeStyle,lineWidth); this.outerRadius = outerRadius; this.innerRadius = innerRadius; this.numberOfPoints = numberOfPoints; } loopDraw(){ super.draw(); this.context.beginPath(); this.numberOfPoints = this.numberOfPoints * 2; // let pointOffSetRadians = (2 * Math.PI) / this.numberOfPoints; // for(var p = 0; p < 4; p++){ this.context.moveTo((this.x + 40) * p,this.y - this.outerRadius); for(let i = 0; i < this.numberOfPoints; i++){ let radX,radY; if(i % 2 === 0){ radX = this.outerRadius * Math.sin(i * pointOffSetRadians); radY = this.outerRadius * Math.cos(i * pointOffSetRadians) * -1; } else { radX = this.innerRadius * Math.sin(i * pointOffSetRadians); radY = this.innerRadius * Math.cos(i * pointOffSetRadians) * -1; } this.context.lineTo((radX + this.x + 40)*p, radY + this.y); } } this.context.closePath(); this.context.stroke(); this.context.fill(); super.afterDraw(); } } const star1 = new Star(24,130,18,7,5,0,"white","green",context4); star1.loopDraw();
2d collision weird freeze on collide
I have this weird problem. I'm trying to code some 2d collision and stumbled across a collision detection program. I decided to try translate it into javascript and maybe learn something along the way. The thing is that when I run it in my browser, all circles freeze in place as soon as two of them collide, but the program don't crash and I get no errors in the console. I've tried to debug it and I think the problem lays within the first if-statement in the checkForCollision-function. Like it's always false. Here's a link to the original version (Scroll down to "Listing 3" for complete code): http://compsci.ca/v3/viewtopic.php?t=14897&postdays=0&postorder=asc&start=0 And here is my translation: var canvas = document.getElementById('canvas01'); var drawFps = document.getElementById('fps'); var context = canvas.getContext('2d'); // The amount of delay between frames var delay = 50; // The maximum distance two circles can be apart and still be considered colliding var epsilon = 10^-9; // The number of circles var numCircles = 5; // We anticipate many circles so we create an array var circles = new Array(); // Stores the amount of time untill a collision occurs var t; // Initialize the circles createCircle(); function createCircle() { for(var i = 0; i < numCircles; i++) { var velX = Math.floor(Math.random() * 5) + 1; // this will get a number between 1 and 5; velX *= Math.floor(Math.random() * 2) == 1 ? 1 : -1; // this will add minus sign in 50% of cases var velY = Math.floor(Math.random() * 5) + 1; velY *= Math.floor(Math.random() * 2) == 1 ? 1 : -1; var radius = Math.floor(Math.random() * 30) + 15; var mass = Math.PI * Math.pow(radius, 2); circleData = { x : Math.floor(Math.random() * canvas.width), y : Math.floor(Math.random() * canvas.height), vx : velX, vy : velY, vxp : velX, vyp : velY, r : radius, m : mass } circles.push(circleData); } } setInterval(loop, 17); // Returns the amount of frames untill a collision will occur function timeToCollision() { var t = Number.MAX_VALUE; var A; var B; var C; var D; var DISC; // Loop through every pair of circles and calculate when they will collide for(var i = 0; i < circles.length; i++) { for(var j = 0; j < circles.length; j++) { if(movingToCircle (circles[i], circles[j])) { // Breaking down the formula for t A = Math.pow(circles[i].vx, 2) + Math.pow(circles[i].vy, 2) - 2 * circles[i].vx * circles[j].vx + Math.pow(circles[j].vx, 2) - 2 * circles[i].vy * circles[j].vy + Math.pow(circles[j].vy, 2); B = -circles[i].x * circles[i].vx - circles[i].y * circles[i].vy + circles[i].vx * circles[j].x + circles[i].vy * circles[j].y + circles[i].x * circles[j].vx - circles[j].x * circles[j].vx + circles[i].y * circles[j].vy - circles[j].y * circles[j].vy; C = Math.pow(circles[i].vx, 2) + Math.pow(circles[i].vy, 2) - 2 * circles[i].vx * circles[j].vx + Math.pow(circles[j].vx, 2) - 2 * circles[i].vy * circles[j].vy + Math.pow(circles[j].vy, 2); D = Math.pow(circles[i].x, 2) + Math.pow(circles[i].y, 2) - Math.pow(circles[i].r, 2) - 2 * circles[i].x * circles[j].x + Math.pow(circles[j].x, 2) - 2 * circles[i].y * circles[j].y + Math.pow(circles[j].y, 2) - 2 * circles[i].r * circles[j].r - Math.pow(circles[j].r, 2); DISC = Math.pow((-2 * B), 2) - 4 * C * D; // If the discriminent if non negative, a collision will occur and // we must compare the time to our current time of collision. We // udate the time if we find a collision that has occurd earlier // than the previous one. if(DISC >= 0) { // We want the smallest time t = Math.min(Math.min(t, 0.5 * (2 * B - Math.sqrt(DISC)) / A), 0.5 * (2 * B + Math.sqrt(DISC)) / A) } } } } return t; } // Draws all the circles to the screen function drawCircles() { for(var i = 0; i < circles.length; i++) { context.fillStyle = '#000000'; context.beginPath(); context.arc(circles[i].x, circles[i].y, circles[i].r, 0, 2 * Math.PI, true); context.closePath(); context.fill(); } } // Updates all the circles attributes. If a collision Occures in between frames, // the circles will be updated to the point of the collision. We return when the // collision occurs so that we can adjust the delay in the main loop. function updateCircles() { // We want to increment by at most one frame var t = Math.min(1, timeToCollision()); for(var i = 0; i < circles.length; i++) { circles[i].x += circles[i].vx * t; circles[i].y += circles[i].vy * t; } return t; } // Collision reaction function function collide(c1, c2) { var nx = (c1.x - c2.x) / (c1.r + c2.r); var ny = (c1.y - c2.y) / (c1.r + c2.r); var a1 = c1.vx * nx + c1.vy * ny; var a2 = c2.vx * nx + c2.vy * ny; var p = 2 * (a1 - a2) / (c1.m + c2.m); c1.vxp = c1.vx - p * nx * c2.m; c1.vyp = c1.vy - p * ny * c2.m; c2.vxp = c2.vx + p * nx * c1.m; c2.vyp = c2.vy + p * ny * c1.m; } // Checks if a collision has occured between any of the circles function checkForCollision() { for(var i = 0; i < circles.length; i++) { for(var j = 0; j < circles.length; j++) { if(movingToCircle(circles[i], circles[j]) && Math.pow((circles[j].x - circles[i].x), 2) + Math.pow((circles[j].y - circles[i].y), 2) <= Math.pow((circles[i].r + circles[j].r + epsilon), 2)) { collide(circles[i], circles[j]); } } if(circles[i].x < 1 || circles[i].x > canvas.width) { circles[i].vxp *= -1; } if(circles[i].y < 1 || circles[i].y > canvas.height) { circles[i].vyp *= -1; } } for(var i = 0; i < circles.length; i++) { circles[i].vx = circles[i].vxp; circles[i].vy = circles[i].vyp; } } // Tells us if two circles are moving towards each other function movingToCircle(c1, c2) { // Position Vector dotted with the Relative Velocity Vector return (c2.x - c1.x) * (c1.vx - c2.vx) + (c2.y - c1.y) * (c1.vy - c2.vy) > 0; } // Main animation loop function loop() { // Clear Canvas context.fillStyle = '#ffffff'; context.fillRect( 0, 0, canvas.width, canvas.height ); drawCircles(); checkForCollision(); t = updateCircles(); } Note that I've changed balls to circles just because I find it fits better for 2d. Thank you in advance.
how to draw smooth curve through N points using javascript HTML5 canvas?
For a drawing application, I'm saving the mouse movement coordinates to an array then drawing them with lineTo. The resulting line is not smooth. How can I produce a single curve between all the gathered points? I've googled but I have only found 3 functions for drawing lines: For 2 sample points, simply use lineTo. For 3 sample points quadraticCurveTo, for 4 sample points, bezierCurveTo. (I tried drawing a bezierCurveTo for every 4 points in the array, but this leads to kinks every 4 sample points, instead of a continuous smooth curve.) How do I write a function to draw a smooth curve with 5 sample points and beyond?
The problem with joining subsequent sample points together with disjoint "curveTo" type functions, is that where the curves meet is not smooth. This is because the two curves share an end point but are influenced by completely disjoint control points. One solution is to "curve to" the midpoints between the next 2 subsequent sample points. Joining the curves using these new interpolated points gives a smooth transition at the end points (what is an end point for one iteration becomes a control point for the next iteration.) In other words the two disjointed curves have much more in common now. This solution was extracted out of the book "Foundation ActionScript 3.0 Animation: Making things move". p.95 - rendering techniques: creating multiple curves. Note: this solution does not actually draw through each of the points, which was the title of my question (rather it approximates the curve through the sample points but never goes through the sample points), but for my purposes (a drawing application), it's good enough for me and visually you can't tell the difference. There is a solution to go through all the sample points, but it is much more complicated (see http://www.cartogrammar.com/blog/actionscript-curves-update/) Here is the the drawing code for the approximation method: // move to the first point ctx.moveTo(points[0].x, points[0].y); for (i = 1; i < points.length - 2; i ++) { var xc = (points[i].x + points[i + 1].x) / 2; var yc = (points[i].y + points[i + 1].y) / 2; ctx.quadraticCurveTo(points[i].x, points[i].y, xc, yc); } // curve through the last two points ctx.quadraticCurveTo(points[i].x, points[i].y, points[i+1].x,points[i+1].y);
A bit late, but for the record. You can achieve smooth lines by using cardinal splines (aka canonical spline) to draw smooth curves that goes through the points. I made this function for canvas - it's split into three function to increase versatility. The main wrapper function looks like this: function drawCurve(ctx, ptsa, tension, isClosed, numOfSegments, showPoints) { showPoints = showPoints ? showPoints : false; ctx.beginPath(); drawLines(ctx, getCurvePoints(ptsa, tension, isClosed, numOfSegments)); if (showPoints) { ctx.stroke(); ctx.beginPath(); for(var i=0;i<ptsa.length-1;i+=2) ctx.rect(ptsa[i] - 2, ptsa[i+1] - 2, 4, 4); } } To draw a curve have an array with x, y points in the order: x1,y1, x2,y2, ...xn,yn. Use it like this: var myPoints = [10,10, 40,30, 100,10]; //minimum two points var tension = 1; drawCurve(ctx, myPoints); //default tension=0.5 drawCurve(ctx, myPoints, tension); The function above calls two sub-functions, one to calculate the smoothed points. This returns an array with new points - this is the core function which calculates the smoothed points: function getCurvePoints(pts, tension, isClosed, numOfSegments) { // use input value if provided, or use a default value tension = (typeof tension != 'undefined') ? tension : 0.5; isClosed = isClosed ? isClosed : false; numOfSegments = numOfSegments ? numOfSegments : 16; var _pts = [], res = [], // clone array x, y, // our x,y coords t1x, t2x, t1y, t2y, // tension vectors c1, c2, c3, c4, // cardinal points st, t, i; // steps based on num. of segments // clone array so we don't change the original // _pts = pts.slice(0); // The algorithm require a previous and next point to the actual point array. // Check if we will draw closed or open curve. // If closed, copy end points to beginning and first points to end // If open, duplicate first points to befinning, end points to end if (isClosed) { _pts.unshift(pts[pts.length - 1]); _pts.unshift(pts[pts.length - 2]); _pts.unshift(pts[pts.length - 1]); _pts.unshift(pts[pts.length - 2]); _pts.push(pts[0]); _pts.push(pts[1]); } else { _pts.unshift(pts[1]); //copy 1. point and insert at beginning _pts.unshift(pts[0]); _pts.push(pts[pts.length - 2]); //copy last point and append _pts.push(pts[pts.length - 1]); } // ok, lets start.. // 1. loop goes through point array // 2. loop goes through each segment between the 2 pts + 1e point before and after for (i=2; i < (_pts.length - 4); i+=2) { for (t=0; t <= numOfSegments; t++) { // calc tension vectors t1x = (_pts[i+2] - _pts[i-2]) * tension; t2x = (_pts[i+4] - _pts[i]) * tension; t1y = (_pts[i+3] - _pts[i-1]) * tension; t2y = (_pts[i+5] - _pts[i+1]) * tension; // calc step st = t / numOfSegments; // calc cardinals c1 = 2 * Math.pow(st, 3) - 3 * Math.pow(st, 2) + 1; c2 = -(2 * Math.pow(st, 3)) + 3 * Math.pow(st, 2); c3 = Math.pow(st, 3) - 2 * Math.pow(st, 2) + st; c4 = Math.pow(st, 3) - Math.pow(st, 2); // calc x and y cords with common control vectors x = c1 * _pts[i] + c2 * _pts[i+2] + c3 * t1x + c4 * t2x; y = c1 * _pts[i+1] + c2 * _pts[i+3] + c3 * t1y + c4 * t2y; //store points in array res.push(x); res.push(y); } } return res; } And to actually draw the points as a smoothed curve (or any other segmented lines as long as you have an x,y array): function drawLines(ctx, pts) { ctx.moveTo(pts[0], pts[1]); for(i=2;i<pts.length-1;i+=2) ctx.lineTo(pts[i], pts[i+1]); } var ctx = document.getElementById("c").getContext("2d"); function drawCurve(ctx, ptsa, tension, isClosed, numOfSegments, showPoints) { ctx.beginPath(); drawLines(ctx, getCurvePoints(ptsa, tension, isClosed, numOfSegments)); if (showPoints) { ctx.beginPath(); for(var i=0;i<ptsa.length-1;i+=2) ctx.rect(ptsa[i] - 2, ptsa[i+1] - 2, 4, 4); } ctx.stroke(); } var myPoints = [10,10, 40,30, 100,10, 200, 100, 200, 50, 250, 120]; //minimum two points var tension = 1; drawCurve(ctx, myPoints); //default tension=0.5 drawCurve(ctx, myPoints, tension); function getCurvePoints(pts, tension, isClosed, numOfSegments) { // use input value if provided, or use a default value tension = (typeof tension != 'undefined') ? tension : 0.5; isClosed = isClosed ? isClosed : false; numOfSegments = numOfSegments ? numOfSegments : 16; var _pts = [], res = [], // clone array x, y, // our x,y coords t1x, t2x, t1y, t2y, // tension vectors c1, c2, c3, c4, // cardinal points st, t, i; // steps based on num. of segments // clone array so we don't change the original // _pts = pts.slice(0); // The algorithm require a previous and next point to the actual point array. // Check if we will draw closed or open curve. // If closed, copy end points to beginning and first points to end // If open, duplicate first points to befinning, end points to end if (isClosed) { _pts.unshift(pts[pts.length - 1]); _pts.unshift(pts[pts.length - 2]); _pts.unshift(pts[pts.length - 1]); _pts.unshift(pts[pts.length - 2]); _pts.push(pts[0]); _pts.push(pts[1]); } else { _pts.unshift(pts[1]); //copy 1. point and insert at beginning _pts.unshift(pts[0]); _pts.push(pts[pts.length - 2]); //copy last point and append _pts.push(pts[pts.length - 1]); } // ok, lets start.. // 1. loop goes through point array // 2. loop goes through each segment between the 2 pts + 1e point before and after for (i=2; i < (_pts.length - 4); i+=2) { for (t=0; t <= numOfSegments; t++) { // calc tension vectors t1x = (_pts[i+2] - _pts[i-2]) * tension; t2x = (_pts[i+4] - _pts[i]) * tension; t1y = (_pts[i+3] - _pts[i-1]) * tension; t2y = (_pts[i+5] - _pts[i+1]) * tension; // calc step st = t / numOfSegments; // calc cardinals c1 = 2 * Math.pow(st, 3) - 3 * Math.pow(st, 2) + 1; c2 = -(2 * Math.pow(st, 3)) + 3 * Math.pow(st, 2); c3 = Math.pow(st, 3) - 2 * Math.pow(st, 2) + st; c4 = Math.pow(st, 3) - Math.pow(st, 2); // calc x and y cords with common control vectors x = c1 * _pts[i] + c2 * _pts[i+2] + c3 * t1x + c4 * t2x; y = c1 * _pts[i+1] + c2 * _pts[i+3] + c3 * t1y + c4 * t2y; //store points in array res.push(x); res.push(y); } } return res; } function drawLines(ctx, pts) { ctx.moveTo(pts[0], pts[1]); for(i=2;i<pts.length-1;i+=2) ctx.lineTo(pts[i], pts[i+1]); } canvas { border: 1px solid red; } <canvas id="c"><canvas> This results in this: You can easily extend the canvas so you can call it like this instead: ctx.drawCurve(myPoints); Add the following to the javascript: if (CanvasRenderingContext2D != 'undefined') { CanvasRenderingContext2D.prototype.drawCurve = function(pts, tension, isClosed, numOfSegments, showPoints) { drawCurve(this, pts, tension, isClosed, numOfSegments, showPoints)} } You can find a more optimized version of this on NPM (npm i cardinal-spline-js) or on GitLab.
The first answer will not pass through all the points. This graph will exactly pass through all the points and will be a perfect curve with the points as [{x:,y:}] n such points. var points = [{x:1,y:1},{x:2,y:3},{x:3,y:4},{x:4,y:2},{x:5,y:6}] //took 5 example points ctx.moveTo((points[0].x), points[0].y); for(var i = 0; i < points.length-1; i ++) { var x_mid = (points[i].x + points[i+1].x) / 2; var y_mid = (points[i].y + points[i+1].y) / 2; var cp_x1 = (x_mid + points[i].x) / 2; var cp_x2 = (x_mid + points[i+1].x) / 2; ctx.quadraticCurveTo(cp_x1,points[i].y ,x_mid, y_mid); ctx.quadraticCurveTo(cp_x2,points[i+1].y ,points[i+1].x,points[i+1].y); }
I decide to add on, rather than posting my solution to another post. Below are the solution that I build, may not be perfect, but so far the output are good. Important: it will pass through all the points! If you have any idea, to make it better, please share to me. Thanks. Here are the comparison of before after: Save this code to HTML to test it out. <!DOCTYPE html> <html> <body> <canvas id="myCanvas" width="1200" height="700" style="border:1px solid #d3d3d3;">Your browser does not support the HTML5 canvas tag.</canvas> <script> var cv = document.getElementById("myCanvas"); var ctx = cv.getContext("2d"); function gradient(a, b) { return (b.y-a.y)/(b.x-a.x); } function bzCurve(points, f, t) { //f = 0, will be straight line //t suppose to be 1, but changing the value can control the smoothness too if (typeof(f) == 'undefined') f = 0.3; if (typeof(t) == 'undefined') t = 0.6; ctx.beginPath(); ctx.moveTo(points[0].x, points[0].y); var m = 0; var dx1 = 0; var dy1 = 0; var preP = points[0]; for (var i = 1; i < points.length; i++) { var curP = points[i]; nexP = points[i + 1]; if (nexP) { m = gradient(preP, nexP); dx2 = (nexP.x - curP.x) * -f; dy2 = dx2 * m * t; } else { dx2 = 0; dy2 = 0; } ctx.bezierCurveTo(preP.x - dx1, preP.y - dy1, curP.x + dx2, curP.y + dy2, curP.x, curP.y); dx1 = dx2; dy1 = dy2; preP = curP; } ctx.stroke(); } // Generate random data var lines = []; var X = 10; var t = 40; //to control width of X for (var i = 0; i < 100; i++ ) { Y = Math.floor((Math.random() * 300) + 50); p = { x: X, y: Y }; lines.push(p); X = X + t; } //draw straight line ctx.beginPath(); ctx.setLineDash([5]); ctx.lineWidth = 1; bzCurve(lines, 0, 1); //draw smooth line ctx.setLineDash([0]); ctx.lineWidth = 2; ctx.strokeStyle = "blue"; bzCurve(lines, 0.3, 1); </script> </body> </html>
As Daniel Howard points out, Rob Spencer describes what you want at http://scaledinnovation.com/analytics/splines/aboutSplines.html. Here's an interactive demo: http://jsbin.com/ApitIxo/2/ Here it is as a snippet in case jsbin is down. <!DOCTYPE html> <html> <head> <meta charset=utf-8 /> <title>Demo smooth connection</title> </head> <body> <div id="display"> Click to build a smooth path. (See Rob Spencer's article) <br><label><input type="checkbox" id="showPoints" checked> Show points</label> <br><label><input type="checkbox" id="showControlLines" checked> Show control lines</label> <br> <label> <input type="range" id="tension" min="-1" max="2" step=".1" value=".5" > Tension <span id="tensionvalue">(0.5)</span> </label> <div id="mouse"></div> </div> <canvas id="canvas"></canvas> <style> html { position: relative; height: 100%; width: 100%; } body { position: absolute; left: 0; right: 0; top: 0; bottom: 0; } canvas { outline: 1px solid red; } #display { position: fixed; margin: 8px; background: white; z-index: 1; } </style> <script> function update() { $("tensionvalue").innerHTML="("+$("tension").value+")"; drawSplines(); } $("showPoints").onchange = $("showControlLines").onchange = $("tension").onchange = update; // utility function function $(id){ return document.getElementById(id); } var canvas=$("canvas"), ctx=canvas.getContext("2d"); function setCanvasSize() { canvas.width = parseInt(window.getComputedStyle(document.body).width); canvas.height = parseInt(window.getComputedStyle(document.body).height); } window.onload = window.onresize = setCanvasSize(); function mousePositionOnCanvas(e) { var el=e.target, c=el; var scaleX = c.width/c.offsetWidth || 1; var scaleY = c.height/c.offsetHeight || 1; if (!isNaN(e.offsetX)) return { x:e.offsetX*scaleX, y:e.offsetY*scaleY }; var x=e.pageX, y=e.pageY; do { x -= el.offsetLeft; y -= el.offsetTop; el = el.offsetParent; } while (el); return { x: x*scaleX, y: y*scaleY }; } canvas.onclick = function(e){ var p = mousePositionOnCanvas(e); addSplinePoint(p.x, p.y); }; function drawPoint(x,y,color){ ctx.save(); ctx.fillStyle=color; ctx.beginPath(); ctx.arc(x,y,3,0,2*Math.PI); ctx.fill() ctx.restore(); } canvas.onmousemove = function(e) { var p = mousePositionOnCanvas(e); $("mouse").innerHTML = p.x+","+p.y; }; var pts=[]; // a list of x and ys // given an array of x,y's, return distance between any two, // note that i and j are indexes to the points, not directly into the array. function dista(arr, i, j) { return Math.sqrt(Math.pow(arr[2*i]-arr[2*j], 2) + Math.pow(arr[2*i+1]-arr[2*j+1], 2)); } // return vector from i to j where i and j are indexes pointing into an array of points. function va(arr, i, j){ return [arr[2*j]-arr[2*i], arr[2*j+1]-arr[2*i+1]] } function ctlpts(x1,y1,x2,y2,x3,y3) { var t = $("tension").value; var v = va(arguments, 0, 2); var d01 = dista(arguments, 0, 1); var d12 = dista(arguments, 1, 2); var d012 = d01 + d12; return [x2 - v[0] * t * d01 / d012, y2 - v[1] * t * d01 / d012, x2 + v[0] * t * d12 / d012, y2 + v[1] * t * d12 / d012 ]; } function addSplinePoint(x, y){ pts.push(x); pts.push(y); drawSplines(); } function drawSplines() { clear(); cps = []; // There will be two control points for each "middle" point, 1 ... len-2e for (var i = 0; i < pts.length - 2; i += 1) { cps = cps.concat(ctlpts(pts[2*i], pts[2*i+1], pts[2*i+2], pts[2*i+3], pts[2*i+4], pts[2*i+5])); } if ($("showControlLines").checked) drawControlPoints(cps); if ($("showPoints").checked) drawPoints(pts); drawCurvedPath(cps, pts); } function drawControlPoints(cps) { for (var i = 0; i < cps.length; i += 4) { showPt(cps[i], cps[i+1], "pink"); showPt(cps[i+2], cps[i+3], "pink"); drawLine(cps[i], cps[i+1], cps[i+2], cps[i+3], "pink"); } } function drawPoints(pts) { for (var i = 0; i < pts.length; i += 2) { showPt(pts[i], pts[i+1], "black"); } } function drawCurvedPath(cps, pts){ var len = pts.length / 2; // number of points if (len < 2) return; if (len == 2) { ctx.beginPath(); ctx.moveTo(pts[0], pts[1]); ctx.lineTo(pts[2], pts[3]); ctx.stroke(); } else { ctx.beginPath(); ctx.moveTo(pts[0], pts[1]); // from point 0 to point 1 is a quadratic ctx.quadraticCurveTo(cps[0], cps[1], pts[2], pts[3]); // for all middle points, connect with bezier for (var i = 2; i < len-1; i += 1) { // console.log("to", pts[2*i], pts[2*i+1]); ctx.bezierCurveTo( cps[(2*(i-1)-1)*2], cps[(2*(i-1)-1)*2+1], cps[(2*(i-1))*2], cps[(2*(i-1))*2+1], pts[i*2], pts[i*2+1]); } ctx.quadraticCurveTo( cps[(2*(i-1)-1)*2], cps[(2*(i-1)-1)*2+1], pts[i*2], pts[i*2+1]); ctx.stroke(); } } function clear() { ctx.save(); // use alpha to fade out ctx.fillStyle = "rgba(255,255,255,.7)"; // clear screen ctx.fillRect(0,0,canvas.width,canvas.height); ctx.restore(); } function showPt(x,y,fillStyle) { ctx.save(); ctx.beginPath(); if (fillStyle) { ctx.fillStyle = fillStyle; } ctx.arc(x, y, 5, 0, 2*Math.PI); ctx.fill(); ctx.restore(); } function drawLine(x1, y1, x2, y2, strokeStyle){ ctx.beginPath(); ctx.moveTo(x1, y1); ctx.lineTo(x2, y2); if (strokeStyle) { ctx.save(); ctx.strokeStyle = strokeStyle; ctx.stroke(); ctx.restore(); } else { ctx.save(); ctx.strokeStyle = "pink"; ctx.stroke(); ctx.restore(); } } </script> </body> </html>
I found this to work nicely function drawCurve(points, tension) { ctx.beginPath(); ctx.moveTo(points[0].x, points[0].y); var t = (tension != null) ? tension : 1; for (var i = 0; i < points.length - 1; i++) { var p0 = (i > 0) ? points[i - 1] : points[0]; var p1 = points[i]; var p2 = points[i + 1]; var p3 = (i != points.length - 2) ? points[i + 2] : p2; var cp1x = p1.x + (p2.x - p0.x) / 6 * t; var cp1y = p1.y + (p2.y - p0.y) / 6 * t; var cp2x = p2.x - (p3.x - p1.x) / 6 * t; var cp2y = p2.y - (p3.y - p1.y) / 6 * t; ctx.bezierCurveTo(cp1x, cp1y, cp2x, cp2y, p2.x, p2.y); } ctx.stroke(); }
Give KineticJS a try - you can define a Spline with an array of points. Here's an example: Old url: http://www.html5canvastutorials.com/kineticjs/html5-canvas-kineticjs-spline-tutorial/ See archive url: https://web.archive.org/web/20141204030628/http://www.html5canvastutorials.com/kineticjs/html5-canvas-kineticjs-spline-tutorial/
Bonjour I appreciate the solution of user1693593 : Hermite polynomials seems the best way to control what will be drawn, and the most satisfying from a mathematical point of view. The subject seems to be closed for a long time but may be some latecomers like me are still interested in it. I've looked for a free interactive plot builder which could allow me to store the curve and reuse it anywhere else, but didn't find this kind of thing on the web : so I made it on my own way, from the wikipedia source mentionned by user1693593. It's difficult to explain how it works here, and the best way to know if it is worth while is to look at https://sites.google.com/view/divertissements/accueil/splines.
Incredibly late but inspired by Homan's brilliantly simple answer, allow me to post a more general solution (general in the sense that Homan's solution crashes on arrays of points with less than 3 vertices): function smooth(ctx, points) { if(points == undefined || points.length == 0) { return true; } if(points.length == 1) { ctx.moveTo(points[0].x, points[0].y); ctx.lineTo(points[0].x, points[0].y); return true; } if(points.length == 2) { ctx.moveTo(points[0].x, points[0].y); ctx.lineTo(points[1].x, points[1].y); return true; } ctx.moveTo(points[0].x, points[0].y); for (var i = 1; i < points.length - 2; i ++) { var xc = (points[i].x + points[i + 1].x) / 2; var yc = (points[i].y + points[i + 1].y) / 2; ctx.quadraticCurveTo(points[i].x, points[i].y, xc, yc); } ctx.quadraticCurveTo(points[i].x, points[i].y, points[i+1].x, points[i+1].y); }
This code is perfect for me: this.context.beginPath(); this.context.moveTo(data[0].x, data[0].y); for (let i = 1; i < data.length; i++) { this.context.bezierCurveTo( data[i - 1].x + (data[i].x - data[i - 1].x) / 2, data[i - 1].y, data[i - 1].x + (data[i].x - data[i - 1].x) / 2, data[i].y, data[i].x, data[i].y); } you have correct smooth line and correct endPoints NOTICE! (y = "canvas height" - y);
A slightly different answer to the original question; If anyone is desiring to draw a shape: that is described by a series of points where the line has a small curve at the points the line doesn't necessarily have to pass through the points (i.e. passes slightly "inside", of them) Then hopefully the below function of mine could help <!DOCTYPE html> <html> <body> <canvas id="myCanvas" width="1200" height="700" style="border: 1px solid #d3d3d3">Your browser does not support the HTML5 canvas tag.</canvas> <script> var cv = document.getElementById("myCanvas"); var ctx = cv.getContext("2d"); const drawPointsWithCurvedCorners = (points, ctx) => { for (let n = 0; n <= points.length - 1; n++) { let pointA = points[n]; let pointB = points[(n + 1) % points.length]; let pointC = points[(n + 2) % points.length]; const midPointAB = { x: pointA.x + (pointB.x - pointA.x) / 2, y: pointA.y + (pointB.y - pointA.y) / 2, }; const midPointBC = { x: pointB.x + (pointC.x - pointB.x) / 2, y: pointB.y + (pointC.y - pointB.y) / 2, }; ctx.moveTo(midPointAB.x, midPointAB.y); ctx.arcTo( pointB.x, pointB.y, midPointBC.x, midPointBC.y, radii[pointB.r] ); ctx.lineTo(midPointBC.x, midPointBC.y); } }; const shapeWidth = 200; const shapeHeight = 150; const topInsetDepth = 35; const topInsetSideWidth = 20; const topInsetHorizOffset = shapeWidth * 0.25; const radii = { small: 15, large: 30, }; const points = [ { // TOP-LEFT x: 0, y: 0, r: "large", }, { x: topInsetHorizOffset, y: 0, r: "small", }, { x: topInsetHorizOffset + topInsetSideWidth, y: topInsetDepth, r: "small", }, { x: shapeWidth - (topInsetHorizOffset + topInsetSideWidth), y: topInsetDepth, r: "small", }, { x: shapeWidth - topInsetHorizOffset, y: 0, r: "small", }, { // TOP-RIGHT x: shapeWidth, y: 0, r: "large", }, { // BOTTOM-RIGHT x: shapeWidth, y: shapeHeight, r: "large", }, { // BOTTOM-LEFT x: 0, y: shapeHeight, r: "large", }, ]; // ACTUAL DRAWING OF POINTS ctx.beginPath(); drawPointsWithCurvedCorners(points, ctx); ctx.stroke(); </script> </body> </html>
To add to K3N's cardinal splines method and perhaps address T. J. Crowder's concerns about curves 'dipping' in misleading places, I inserted the following code in the getCurvePoints() function, just before res.push(x); if ((y < _pts[i+1] && y < _pts[i+3]) || (y > _pts[i+1] && y > _pts[i+3])) { y = (_pts[i+1] + _pts[i+3]) / 2; } if ((x < _pts[i] && x < _pts[i+2]) || (x > _pts[i] && x > _pts[i+2])) { x = (_pts[i] + _pts[i+2]) / 2; } This effectively creates a (invisible) bounding box between each pair of successive points and ensures the curve stays within this bounding box - ie. if a point on the curve is above/below/left/right of both points, it alters its position to be within the box. Here the midpoint is used, but this could be improved upon, perhaps using linear interpolation.
If you want to determine the equation of the curve through n points then the following code will give you the coefficients of the polynomial of degree n-1 and save these coefficients to the coefficients[] array (starting from the constant term). The x coordinates do not have to be in order. This is an example of a Lagrange polynomial. var xPoints=[2,4,3,6,7,10]; //example coordinates var yPoints=[2,5,-2,0,2,8]; var coefficients=[]; for (var m=0; m<xPoints.length; m++) coefficients[m]=0; for (var m=0; m<xPoints.length; m++) { var newCoefficients=[]; for (var nc=0; nc<xPoints.length; nc++) newCoefficients[nc]=0; if (m>0) { newCoefficients[0]=-xPoints[0]/(xPoints[m]-xPoints[0]); newCoefficients[1]=1/(xPoints[m]-xPoints[0]); } else { newCoefficients[0]=-xPoints[1]/(xPoints[m]-xPoints[1]); newCoefficients[1]=1/(xPoints[m]-xPoints[1]); } var startIndex=1; if (m==0) startIndex=2; for (var n=startIndex; n<xPoints.length; n++) { if (m==n) continue; for (var nc=xPoints.length-1; nc>=1; nc--) { newCoefficients[nc]=newCoefficients[nc]*(-xPoints[n]/(xPoints[m]-xPoints[n]))+newCoefficients[nc-1]/(xPoints[m]-xPoints[n]); } newCoefficients[0]=newCoefficients[0]*(-xPoints[n]/(xPoints[m]-xPoints[n])); } for (var nc=0; nc<xPoints.length; nc++) coefficients[nc]+=yPoints[m]*newCoefficients[nc]; }
I somehow need a way that uses only quadratic bezier. This is my method and can be extended to 3d: The formula for the quad bezier curve is b(t) = (1-t)^2A + 2(1-t)tB + t^2*C When t = 0 or 1, the curve can pass through point A or C but is not guaranteed to pass through B. Its first-order derivative is b'(t) = 2(t-1)A + 2(1-2t)B + 2tC To construct a curve passing through points P0,P1,P2 with two quad bezier curves, the slopes of the two bezier curves at p1 should be equal b'α(t) = 2(t-1)P0 + 2(1-2t)M1 + 2tP1 b'β(t) = 2(t-1)P1 + 2(1-2t)M2 + 2tP2 b'α(1) = b'β(0) This gives (M1 + M2) / 2 = P1 So a curve through 3 points can be drawn like this bezier(p0, m1, p1); bezier(p1, m2, p2); Where m1p1 = p1m2. The direction of m1m2 is not matter, can be found by p2 - p1. For curves passing through 4 or more points bezier(p0, m1, p1); bezier(p1, m2, (m2 + m3) / 2); bezier((m2 + m3) / 2, m3, p2); bezier(p2, m4, p3); Where m1p1 = p1m2 and m3p2 = p2m4. function drawCurve(ctx: CanvasRenderingContext2D, points: { x: number, y: number }[], tension = 2) { if (points.length < 2) { return; } ctx.beginPath(); if (points.length === 2) { ctx.moveTo(points[0].x, points[0].y); ctx.lineTo(points[1].x, points[1].y); ctx.stroke(); return; } let prevM2x = 0; let prevM2y = 0; for (let i = 1, len = points.length; i < len - 1; ++i) { const p0 = points[i - 1]; const p1 = points[i]; const p2 = points[i + 1]; let tx = p2.x - (i === 1 ? p0.x : prevM2x); let ty = p2.y - (i === 1 ? p0.y : prevM2y); const tLen = Math.sqrt(tx ** 2 + ty ** 2); if (tLen > 1e-8) { const inv = 1 / tLen; tx *= inv; ty *= inv; } else { tx = 0; ty = 0; } const det = Math.sqrt(Math.min( (p0.x - p1.x) ** 2 + (p0.y - p1.y) ** 2, (p2.x - p1.x) ** 2 + (p2.y - p1.y) ** 2 )) / (2 * tension); const m1x = p1.x - tx * det; const m1y = p1.y - ty * det; const m2x = p1.x + tx * det; const m2y = p1.y + ty * det; if (i === 1) { ctx.moveTo(p0.x, p0.y); ctx.quadraticCurveTo(m1x, m1y, p1.x, p1.y); } else { const mx = (prevM2x + m1x) / 2; const my = (prevM2y + m1y) / 2; ctx.quadraticCurveTo(prevM2x, prevM2y, mx, my); ctx.quadraticCurveTo(m1x, m1y, p1.x, p1.y); } if (i === len - 2) { ctx.quadraticCurveTo(m2x, m2y, p2.x, p2.y); } prevM2x = m2x; prevM2y = m2y; } ctx.stroke(); }