I have a question regarding strokestyle and strokerect. I’ve realised that in a code strokestyle must be before strokerect. Can someone tell why it matters, if you put strokestyle before or after strokerect.
For example if I put the following it works but if i put it the other way around it doesn’work:
ctx.strokeStyle = ‘green’;
ctx.strokeRect(200, 150, -150, -100);
Thank you for your help
Hi @am1990 . It helps if you are more specific on what topic your question is based, in this case apparently canvas, as JS universe is vast and only mentioning method names like ‘strokerect’ can tell little of what we are talking about.
Im not exprienced with canvas, but my guess is, its how the flow of the code execution is meant to work with canvas. When we call
strokeRect, it uses the stroke styles already applied. If you call
strokeStyle, it only alters the current stroke style, not the one used for the rect you already created; any following rects however, will use the current stroke style. Its not that the code “does not work”, it just doesnt work the way you expect it to be
ctx in the example, represents the canvas object, not just an object which we build up, shaping into rect or circle and manipulating their properties as we go. Its a sand box we put and put additional shapes and form, each one using the current properties on their call.
This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.