What's the use of translate ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 6

What's the use of translate ?

ctx.textAlign="start"; ctx.fillText("start", 10, 30); ctx.translate(100, 150); ctx.fillText("after translate", 10, 30); ctx.textAlign="start"; ctx.fillText("start", 10, 30); ctx.fillText("after translate", 100, 150); We obtain the same result with one less line of code so why do we have to use translate?

14th Jan 2017, 8:50 AM
Ciel
Ciel - avatar
7 Answers
+ 9
translate(x,y) moves the canvas coordinating system x left and y down…
14th Jan 2017, 8:54 AM
Valen.H. ~
Valen.H. ~ - avatar
+ 9
translate moves the coords but then you have to draw something to see difference…… I suggest you clear the canvas on every cycle with clearRect, increase translate and then fill the text…… or use setTransform(1,0,0,1,100,150)
14th Jan 2017, 9:00 AM
Valen.H. ~
Valen.H. ~ - avatar
+ 6
Okay thanks ! I've barely completed the basic HTML course so I don't know about the functions you are mentioning. I guess I have to get more knowledge before wanting to understand all the subtilites of the course about canvas. Thanks again. Have a nice day 😊
14th Jan 2017, 9:04 AM
Ciel
Ciel - avatar
+ 5
Thanks for your quick answer ! Do u mean we should be seeing the text moving with translate while with fillText it will be static ? Then why can't I see this difference in the output version?
14th Jan 2017, 8:57 AM
Ciel
Ciel - avatar
+ 5
@Coralie asked: "We obtain the same result with one less line of code so why do we have to use translate?" You're right into your reasonning, but to have the same result in your code, you would have to write: ctx.fillText("after translate",110,180); // translate(100,150) + fillText('"...",10,30) Maybe have you get this snippet from an example to demonstrate the behaviour of translate(), or something like? Anyway, in more complexe case, translate() is useful to allow more readability of code and to handle relative coordinates instead of absolute ^^
17th Jan 2017, 3:17 AM
visph
visph - avatar
+ 5
@visph: Thanks I think I understand now 😊 This example is from the Sololearn HTML course.
24th Jan 2017, 5:31 PM
Ciel
Ciel - avatar
+ 2
to translate animation from a position to an other and we are able to modifie in the new animation
5th Feb 2017, 9:23 AM
HaMza BouRouissa
HaMza BouRouissa - avatar