A
A
agromov912019-11-23 23:19:10
JavaScript
agromov91, 2019-11-23 23:19:10

How does canvas work?

Why in this example:
1) I can't draw a line using lineTo below the initially specified moveTo?
2) Again ctx.fillText doesn't appear below moveTo?

var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');

ctx.beginPath();
ctx.moveTo(1, 150);
ctx.lineTo(100, 150);
ctx.lineTo(100, 130);
ctx.closePath();
ctx.stroke();
ctx.fillStyle = 'black';
ctx.fill();

ctx.fillText( "Привет", 1, 280);

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
profesor08, 2019-12-01
@profesor08

You draw a line to the right, then up, close the outline and paint it. Here's what's happening to you.

ctx.beginPath();
ctx.moveTo(1, 150);
ctx.lineTo(100, 130);
ctx.lineTo(100, 230);
ctx.closePath();
ctx.stroke();
ctx.fillStyle = 'red';
ctx.fill();

And set the size of the canvas so that all your manipulations do not go beyond its limit.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question