5
I’m setting up an arena using HTML/JS
with the canvas
.
I have the following code:
function montarArena() {
var canvas;
var heightCanvas = 498;
var widthCanvas = 598;
var qtdLinhas = $('#larguraCampo').val();
var qtdColunas = $('#alturaCampo').val();
var tamanhoLinha = widthCanvas / qtdLinhas;
var tamanhoColuna = heightCanvas / qtdColunas;
canvas = document.getElementById('imageView');
context = canvas.getContext('2d');
context.strokeStyle = '#000000';
context.fillStyle = '#fff';
context.clearRect(1, 1, widthCanvas, heightCanvas);
console.log("tamColuna: " + tamanhoColuna + " tamLinha: " + tamanhoLinha + " QtdColunas: " + qtdColunas + " QtdLinhas: " + qtdLinhas);
for (i = 0; i <= qtdLinhas; i++) {
for (j = 0; j <= qtdColunas; j++) {
context.strokeRect(j, i, tamanhoLinha * i, tamanhoColuna * j);
}
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<input type="range" id="larguraCampo" value="1" min="1" max="25" onchange="montarArena()" />
<input type="range" id="alturaCampo" value="1" min="1" max="25" onchange="montarArena()" />
<br/><br/><br/><br/><br/><br/>
<canvas id="imageView" width="600" height="500"></canvas>
As the number of lines is increased, the arena gets crooked.... As follows::
I need it to fit, like a real table:
I’m not realizing where I’m going wrong.
Very good @Sergio, that’s right! What the lack of a table test does not rs. A single correction, Oce ended up reversing the order of
tamanhoLinha * j
andtamanhoColuna * i
, the correct would betamanhoLinha * i
andtamanhoColuna * j
,– MarceloBoni