Skip to content

Commit 5ef879a

Browse files
committed
Fixed and cleaned demo page code
1 parent 800555d commit 5ef879a

File tree

2 files changed

+39
-28
lines changed

2 files changed

+39
-28
lines changed

demo/index.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
</fieldset>
4242
<fieldset>
4343
<label><input type="range" name="points" min="2" max="10" value="5"> points</label><br/>
44-
<label><input type="range" name="intermediates" min="2" max="20" value="10"> intermediates</label><br/>
44+
<label><input type="range" name="intermediates" min="1" max="15" value="5"> intermediates</label><br/>
4545
<button>New Points</button>
4646
</fieldset>
4747

demo/main.js

+38-27
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import canvas from './draw.js';
22

33
const canvasSize = 1000;
4+
const domainMargin = 1;
45

56
function radioValue(name) {
67
return document.querySelector(`input[type=radio][name=${name}]:checked`).value;
78
}
8-
function value(name) {
9-
return document.querySelector(`input[name=${name}]`).value;
9+
function intValue(name) {
10+
return parseInt(document.querySelector(`input[name=${name}]`).value);
1011
}
1112

1213
function range(stop) {
@@ -25,17 +26,21 @@ let ip = []; // interpolated points
2526
// randomly generate control points
2627
function runGeneration() {
2728
const margin = 0.2;
28-
cp0 = range(10).map(i => [
29-
canvasSize*margin + Math.random()*canvasSize*(1-2*margin),
30-
canvasSize*margin + Math.random()*canvasSize*(1-2*margin)
31-
]);
32-
// console.log(cp);
29+
cp0 = range(10).map(i => {
30+
let p = [
31+
canvasSize*margin + Math.random()*canvasSize*(1-2*margin),
32+
canvasSize*margin + Math.random()*canvasSize*(1-2*margin)
33+
];
34+
p.param = i; // save parameter
35+
return p;
36+
});
37+
// console.log(cp0);
3338
}
3439

3540
// calculate intermediate points
3641
function runInterpolation() {
37-
let num = value('points');
38-
let res = value('intermediates');
42+
let num = intValue('points'); // number of control points
43+
let int = intValue('intermediates'); // number of intermediates (between two adjacent control points)
3944
let method = radioValue('method');
4045
let clip = radioValue('clip');
4146
let cubicTension = radioValue('cubicTension');
@@ -46,14 +51,24 @@ function runInterpolation() {
4651
ip = [];
4752
let s = Smooth(cp, { method, clip, cubicTension, sincFilterSize, sincWindow });
4853

49-
let pcs = cp.length-1; // number of curve pieces
50-
for (let p=-1; p<cp.length; p++) {
51-
for (let i=0; i<res; i++) {
52-
let u = p + i/res;
53-
ip.push( s(u) );
54-
}
54+
num += 2*domainMargin;
55+
let pieces = num-1; // number of curve pieces
56+
let total = num + pieces*int; // total number of points/values
57+
let step = pieces/(total-1);
58+
59+
// console.log({
60+
// points: num,
61+
// intermediates: int,
62+
// total
63+
// });
64+
65+
for (let i=0; i<total; i++) {
66+
let u = i*step - domainMargin; // parameter value
67+
let v = s(u); // interpolated value
68+
v.param = u; // save parameter
69+
ip.push(v);
5570
}
56-
// console.log(ip);
71+
console.log(ip);
5772
}
5873

5974
function drawCurve(scanvas, cp, ip) {
@@ -68,18 +83,14 @@ function drawMain() {
6883
}
6984

7085
function drawSide() {
71-
let du = cp.length - 1 + 2; // length of parameter domain
72-
let sidecp = cp.map((p, i) => [
73-
(canvasSize/du) * (i + 1),
74-
p[1]
75-
]);
76-
let sideip = ip.map((p, i) => [
77-
canvasSize/(ip.length-1) * i,
78-
p[1]
79-
]);
86+
let dlen = cp.length - 1 + 2*domainMargin; // length of parameter domain
87+
let p2x = p => canvasSize/dlen * (p+domainMargin); // parameter to x value
88+
89+
let sidecp = cp.map(p => [ p2x(p.param), p[1] ]);
90+
let sideip = ip.map(p => [ p2x(p.param), p[1] ]);
8091
drawCurve(side, sidecp, sideip);
81-
side.lines([ [canvasSize/du,0], [canvasSize/du,canvasSize] ], 'lightgray', 1);
82-
side.lines([ [canvasSize/du*(sidecp.length),0], [canvasSize/du*(sidecp.length),canvasSize] ], 'lightgray', 1);
92+
side.lines([ [p2x(0),0], [p2x(0),canvasSize] ], 'lightgray', 1);
93+
side.lines([ [p2x(cp.length-1),0], [p2x(cp.length-1),canvasSize] ], 'lightgray', 1);
8394
}
8495

8596
function generate() {

0 commit comments

Comments
 (0)