Answer the question
In order to leave comments, you need to log in
How are line drawing coordinates set in this js canvas code?
I am learning from a book. There is an example of code for drawing lines on a canvas, in general, the code is clear except for one nuance in the loop:
"context.fillText(' cap:' + caps[j], 88 + j * 180, 45 + k * 120 )
...
context .lineTo(155 + j * 180, 20 + k * 120 )" .... I don't specifically understand what this expression 88 + j * 180
means in the fillText method and the same thing in the MoveTo and LineTo methods. Are these coordinates? and how are they counted? why through a multiplier? In general, there are more questions than answers (((I post the full code of the example below, I will be grateful for any help, I really want to figure it out.
<canvas id="mycanvas" width="535" height="360">Это элемент холст</canvas>
function O(i) {return typeof i == 'object' ? i : document.getElementById(i)}
function S(i) {return O(i).style}
canvas = O('mycanvas')
context = canvas.getContext('2d')
S(canvas).background = 'lightblue'
context.strokeStyle = 'green'
context.fillStyle = 'red'
context.font = 'bold 13pt Courier'
context.textBaseline = 'top'
context.textAlign = 'center'
context.lineWidth = 20
caps = [' butt', 'round', 'square']
joins = [' roud', ' bevel', ' miter']
for (j = 0; j < 3; ++j)
{
for (k = 0; k < 3; ++k)
{
context.lineCap = caps[j]
context.lineJoin = joins[k]
context.fillText(' cap:' + caps[j], 88 + j * 150, 45 + k * 120)
context.fillText(' join:' + joins[k], 88 + j * 180, 65 + k * 120)
context.beginPath()
context.moveTo(20 + j * 100, 100 + k * 100)
context.lineTo(20 + j * 100, 20 + k * 100)
context.lineTo(155 + j * 100, 20 + k * 100)
context.lineTo(155 + j * 100, 100 + k * 100)
context.stroke()
context.closePath()
}
}
Answer the question
In order to leave comments, you need to log in
It's just more convenient to count in a cycle.
for (let j = 0; j < 3; j++) {
const x = 88 + j * 100; // умножение выполнится раньше сложения
console.log(x); // 88, 188, 288
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question