From fce4a30e998ee450d537a08e6e0836a18882abd6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aleks=20R=C5=ABti=C5=86=C5=A1?= Date: Wed, 26 Feb 2025 21:44:15 -0500 Subject: [PATCH] Don't move graph when replotting --- assets/math.js | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/assets/math.js b/assets/math.js index ff44f1e..c1c0c9e 100644 --- a/assets/math.js +++ b/assets/math.js @@ -29,18 +29,23 @@ function defineVars(vars) { function plot(target, fns, deps, [xDomain, yDomain] = [[-1, 9], [-1, 9]]) { const depsEl = deps.map(document.querySelector.bind(document)); + + const data = () => fns(...(depsEl.map(el => parseFloat(el.value)))).map(fn => ({ + fn, graphType: 'polyline' + })); + + const opts = { + target, + width: 600, + height: 400, + xAxis: { domain: xDomain }, + yAxis: { domain: yDomain }, + grid: true + }; + const plot = () => { - functionPlot({ - target, - width: 600, - height: 400, - xAxis: { domain: xDomain }, - yAxis: { domain: yDomain }, - grid: true, - data: fns(...(depsEl.map(el => parseFloat(el.value)))).map(fn => ({ - fn, graphType: 'polyline' - })) - }); + opts.data = data(); + functionPlot(opts); } depsEl.forEach(el => {