SlimRunner

desmos-wasd.js

Aug 10th, 2020 (edited)
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*jshint esversion: 6 */
  2. // coded by SlimRunner
  3. // patchRegression made with the help of CyanideDuality
  4. // Desmos WASD variable manipulator
  5.  
  6. (function() {
  7.     'use strict';
  8.    
  9.     // you can change the name of your variables here
  10.     const X_VAR = 'P_{x}';
  11.     const Y_VAR = 'P_{y}';
  12.     const RESOL = 2; // decimal places
  13.    
  14.     // use destructred assignment to populate variables
  15.     let {
  16.         px_id,      // id of x location variable
  17.         py_id,      // id of y location variable
  18.         x_min_id,   // id of variable for minimum on x
  19.         x_max_id,   // id of variable for maximum on x
  20.         y_min_id,   // id of variable for minimum on y
  21.         y_max_id,   // id of variable for maximum on y
  22.         x_step,     // stepping value of x
  23.         y_step      // stepping value of y
  24.     } = captureVars(X_VAR, Y_VAR);
  25.    
  26.     let obs_x = Calc.HelperExpression({
  27.         latex: X_VAR
  28.     });
  29.     let obs_y = Calc.HelperExpression({
  30.         latex: Y_VAR
  31.     });
  32.    
  33.     obs_x.observe('numericValue', () => {
  34.         patchRegression();
  35.     });
  36.     obs_y.observe('numericValue', () => {
  37.         patchRegression();
  38.     });
  39.    
  40.     // listen to window keydown
  41.     window.addEventListener('keydown', (e) => {
  42.         // do something only when the target isn't a textinput
  43.         if (e.target.nodeName.toLowerCase() === 'body') {
  44.             // prevent caps lock from killing the code
  45.             let cKey = e.key.toLowerCase();
  46.             let evalExp = Calc.expressionAnalysis;
  47.            
  48.             switch (true) {
  49.                 case cKey === 'w': // up
  50.                     increaseVar({
  51.                         id: py_id,
  52.                         varName: Y_VAR,
  53.                         varValue: evalExp[py_id].evaluation.value,
  54.                         delta: y_step,
  55.                         min: evalExp[y_min_id].evaluation.value,
  56.                         max: evalExp[y_max_id].evaluation.value,
  57.                         resolution: RESOL
  58.                     });
  59.                     break;
  60.                    
  61.                 case cKey === 'a': // left
  62.                     increaseVar({
  63.                         id: px_id,
  64.                         varName: X_VAR,
  65.                         varValue: evalExp[px_id].evaluation.value,
  66.                         delta: -x_step,
  67.                         min: evalExp[x_min_id].evaluation.value,
  68.                         max: evalExp[x_max_id].evaluation.value,
  69.                         resolution: RESOL
  70.                     });
  71.                     break;
  72.                    
  73.                 case cKey === 's': // down
  74.                     increaseVar({
  75.                         id: py_id,
  76.                         varName: Y_VAR,
  77.                         varValue: evalExp[py_id].evaluation.value,
  78.                         delta: -y_step,
  79.                         min: evalExp[y_min_id].evaluation.value,
  80.                         max: evalExp[y_max_id].evaluation.value,
  81.                         resolution: RESOL
  82.                     });
  83.                     break;
  84.                    
  85.                 case cKey === 'd': // right
  86.                     increaseVar({
  87.                         id: px_id,
  88.                         varName: X_VAR,
  89.                         varValue: evalExp[px_id].evaluation.value,
  90.                         delta: x_step,
  91.                         min: evalExp[x_min_id].evaluation.value,
  92.                         max: evalExp[x_max_id].evaluation.value,
  93.                         resolution: RESOL
  94.                     });
  95.                     break;
  96.                    
  97.                 default:
  98.                 // huh? not wasd?
  99.             }
  100.            
  101.         }
  102.     });
  103.    
  104.    
  105.     function patchRegression() {
  106.         let evalExp = Calc.expressionAnalysis;
  107.         let x = evalExp[px_id].evaluation.value;
  108.         let y = evalExp[py_id].evaluation.value;
  109.        
  110.         x = Math.min(Math.max(x, evalExp[x_min_id].evaluation.value), evalExp[x_max_id].evaluation.value);
  111.         y = Math.min(Math.max(y, evalExp[y_min_id].evaluation.value), evalExp[y_max_id].evaluation.value);
  112.        
  113.         Calc.setExpression({
  114.             id: px_id,
  115.             latex: `${X_VAR}=${x}`
  116.         });
  117.        
  118.         Calc.setExpression({
  119.             id: py_id,
  120.             latex: `${Y_VAR}=${y}`
  121.         });
  122.     }
  123.    
  124.    
  125.     function increaseVar({
  126.         id,
  127.         varName,
  128.         varValue,
  129.         delta,
  130.         min, max,
  131.         resolution
  132.     }) {
  133.         const round = (val, scl) => Math.round(val * scl) / scl;
  134.        
  135.         let output = round(varValue + delta, Math.pow(10, resolution));
  136.         if (output < min || output > max) return false;
  137.        
  138.         Calc.setExpression({
  139.             id: id,
  140.             latex: `${varName}=${(output).toFixed(resolution)}`
  141.         });
  142.        
  143.         return true;
  144.     }
  145.    
  146.     function filterExprAssignment(exprs, varNames) {
  147.         let rxFilter = new RegExp(String.raw`^((?:${varNames.join('|')})=)(.+)$`, '');
  148.        
  149.         return exprs.filter((exp) => {
  150.             if (exp.hasOwnProperty('latex')) {
  151.                 return rxFilter.test(exp.latex);
  152.             }
  153.            
  154.             return false;
  155.         });
  156.     }
  157.    
  158.     function captureVars(x_name, y_name) {
  159.         let exprs = Calc.getExpressions();
  160.        
  161.         // get array with expressions containing sought variables
  162.         let xy_expr = filterExprAssignment(exprs, [x_name, y_name]);
  163.        
  164.         let x_idx = xy_expr[0].latex.indexOf('x') === -1 ? 1 : 0;
  165.         let y_idx = x_idx ? 0 : 1;
  166.        
  167.         // gets min and max expressions
  168.         let lim_bounds = xy_expr.map((exp) => {
  169.             return exp.sliderBounds;
  170.         });
  171.        
  172.         let x_lims = {
  173.             max: filterExprAssignment(exprs, [lim_bounds[x_idx].max])[0],
  174.             min: filterExprAssignment(exprs, [lim_bounds[x_idx].min])[0]
  175.         };
  176.        
  177.         let y_lims = {
  178.             max: filterExprAssignment(exprs, [lim_bounds[y_idx].max])[0],
  179.             min: filterExprAssignment(exprs, [lim_bounds[y_idx].min])[0]
  180.         };
  181.        
  182.         return {
  183.             px_id: xy_expr[x_idx].id,
  184.             py_id: xy_expr[y_idx].id,
  185.             x_min_id: x_lims.min.id,
  186.             x_max_id: x_lims.max.id,
  187.             y_min_id: y_lims.min.id,
  188.             y_max_id: y_lims.max.id,
  189.             x_step: Number(lim_bounds[x_idx].step),
  190.             y_step: Number(lim_bounds[y_idx].step)
  191.         };
  192.     }
  193. }());
  194.  
Advertisement
Add Comment
Please, Sign In to add comment