Home > Mobile >  How do you make a loop for canvas function which has several arguments?
How do you make a loop for canvas function which has several arguments?

Time:11-03

<canvas id="chart" width="700" height="550"></canvas>

<script>
  const canvas = document.getElementById('chart');
  const context = canvas.getContext('2d');

  /* Draw a line from (fromX, fromY) to (toX, toY) */
  function drawLine(fromX, fromY, toX, toY) {
    context.beginPath();
    context.moveTo(toX, toY);
    context.lineTo(fromX, fromY);
    context.stroke();
  }

  /* Draw a text (string) on (x, y) */
  function drawText(text, x, y) {
    context.fillStyle = 'black';
    context.fillText(text, x, y);
  }

  /* Draw a text and with a line to its right */
  function drawLineWithText(text, fromX, fromY, toX, toY) {
    drawText(text, fromX - 50, fromY   3);
    drawLine(fromX, fromY, toX, toY);
  }

  for (var fromY = 50; fromY < 500; fromY  = 50, toY = 50 toY < 500; toY  = 50, fromX = 70, toX =700) {
    drawLineWithText(text, fromX, fromY, toX, toY);
  }
</script>
**strong text** I actually have no clue how to make a for statement for this, I tried to play around, but only managed to make it work when I did it manually, writing own arguments example; drawLineWithText(1000, 20, 30, 100, 30)

CodePudding user response:

You could simplify the for loop to just being

for(let i = 50; i < 500; i  ) {
   drawLineWithText(text, fromX, i, toX, i);
}

Since fromY and toY follow identical patterns and fromX and toX are constants, this should work.

CodePudding user response:

Try collecting your custom arguments into a single array that can be iterated over first. Possibly something like this:

// establish your starts, ends, and steps
const text = 'Some text';

const fromXStart = 70;
const fromXEnd = 700;
const fromXStep = 50;

const fromYStart = 50;
const fromYEnd = 500;
const fromYStep = 50;

const toXStart = 700;
const toXEnd = 1000;
const toXStep = 50;

const toYStart = 50;
const toYEnd = 500;
const toYStep = 50;

// create an object to keep track of where we are at each iteration of the loop
const currentArgs = {
  fromX: fromXStart,
  fromY: fromYStart,
  toX: toXStart,
  toY: toYStart
}

// create a single array of args to loop over with our intial values
const args = [
  [text, currentArgs.fromX, currentArgs.fromY, currentArgs.toX, currentArgs.toY]
]

// create a check function to see if we're done looping (if all ends have been met)
const isDone = () => {
  const fromXIsDone = currentArgs.fromX >= fromXEnd;
  const fromYIsDone = currentArgs.fromY >= fromYEnd;
  const toXIsDone = currentArgs.toX >= toXEnd;
  const toYIsDone = currentArgs.toY >= toYEnd;
  return fromXIsDone && fromYIsDone && toXIsDone && toYIsDone;
}

// loop until done
while (!isDone()) {
  // use Math.min to ensure we don't go past the max
  currentArgs.fromX = Math.min(fromXEnd, currentArgs.fromX   fromXStep);
  currentArgs.fromY = Math.min(fromYEnd, currentArgs.fromY   fromYStep);
  currentArgs.toX = Math.min(toXEnd, currentArgs.toX   toXStep);
  currentArgs.toY = Math.min(toYEnd, currentArgs.toY   toYStep);
  args.push([
    text, currentArgs.fromX, currentArgs.fromY, currentArgs.toX, currentArgs.toY
  ])
}

// now loop over our args array and use the spread syntax (...) to spread the args as args
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax
for (let i = 0; i < args.length; i  ) {
  drawLineWithText(...args[i]);
}
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

  • Related