0

I am unable to draw rectangles in canvas within for loop in JavaScript. Sometimes they are visible for a fraction of second, but they disappear instantly. No errors in developer console - tested on Firefox & Chrome, so problem isn't browser-related. Seems like only I encountered this issue within my class, despite having exactly the same (or slightly adjusted to my needs) code.

Code was tested on multiple browsers, the problem itself seems to occur only on my laptop/browser - rest of my colleagues haven't encountered this issue, apart from some typos etc.

The general idea is to receive a result similar to this one: enter image description here

function Draw() {
    var l = liczba.value; // user-input based loop control
    var xpos = ypos = 300; // Left side rectangles starting drawing position
    var xneg = 600, yneg = 300; // Right side rectangles starting drawing position
    var canvas = document.getElementById('image');
    var context = canvas.getContext('2d');
    context.fillStyle = 'red';
    for(var i = 0; i < l; i++) {
        context.fillRect(xpos, ypos, 100, 100);
        context.strokeRect(xpos, ypos, 100, 100);
        console.log("Left Canvas Painted!"); // Debug

        context.fillRect(xneg, yneg, 100, 100);
        context.strokeRect(xneg, yneg, 100, 100);
        console.log("Right Canvas Painted!"); // Debug
        xpos += 50; xneg -= 50; ypos += 50; yneg += 50; // change next canvas' starting position by...
    }
}
<body>
        <form method="post" id="f" onsubmit="Draw();">
            <label>Podaj liczbę figur do narysowania: </label>
            <input type="text" name="liczba" id="liczba">
        <input type="submit" name="send" id="send" value="Zatwierdź">
    </form>
    <canvas id="image" width="1200" height="800">Canvasy nie działają</canvas>
</body>
5
  • Try to draw when the DOM is ready, google it. Don't understand your HTML comments: <!-- --> Commented Apr 5, 2019 at 22:16
  • Comments aren't important in this case. Commented Apr 5, 2019 at 22:17
  • Why are you drawing the rectangles onsubmit? Submitting the form will cause it to post the data and reload. Commented Apr 5, 2019 at 22:21
  • @jcbdrn that's what I was worrying about. thanks for the heads up. Commented Apr 5, 2019 at 22:21
  • I've posted what I think is a simple solution Commented Apr 5, 2019 at 22:25

2 Answers 2

1

Try changing the button from a submit button to a normal button and call Draw() on a click event.

<input type="button" onclick="Draw()" name="send" id="send" value="Zatwierdź">
Sign up to request clarification or add additional context in comments.

Comments

0

The answer to this issue was pretty straight-forward, thanks to @jcbbdrn - a minor oversight on my part. Due to page reload on form submitting, whole canvas was flushed down the toilet. After implementing a regular button and assigning to it an onclick event, the problem has been solved.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.