Why do all canvas examples use ctx?

后端 未结 5 535
长情又很酷
长情又很酷 2020-12-24 11:38

Is this a requirement or a convention? If it\'s a convention what\'s the reason? Ctx doesn\'t seem especially memorable or intuitive.

e.g.

var ctx =          


        
相关标签:
5条回答
  • 2020-12-24 12:06

    It is a shorthand for the word "context". That's it.

    Of course you can use whatever name you like - there is no real naming convention in this instance.

    0 讨论(0)
  • 2020-12-24 12:08

    it's exactly the same if you do

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

    or

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

    I think "ctx" is really explanatory and short enough :)

    0 讨论(0)
  • 2020-12-24 12:20

    It's a convention, since the canvas in the mostly open-source browsers uses Cairo, and in Cairo, they're called "Contexts".

    0 讨论(0)
  • 2020-12-24 12:22

    It is just name for variable. It could be anything. Ctx is just short word for ConTeXt.

    0 讨论(0)
  • 2020-12-24 12:27

    Propably because the examples are teaching something, and who wrote wanted to make sure the reader knows ctx is the canvas context since ctx is an abreviation of context, but it's too boring to write "context" when you could just write "ctx".

    0 讨论(0)
提交回复
热议问题