Javascript 为什么所有画布示例都使用 ctx?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4994046/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why do all canvas examples use ctx?
提问by j08691
Is this a requirement or a convention? If it's a convention what's the reason? Ctx doesn't seem especially memorable or intuitive.
这是要求还是约定?如果是公约,原因是什么?Ctx 似乎不是特别令人难忘或直观。
e.g.
例如
var ctx = document.getElementById('canvas').getContext('2d');
回答by Jacob Relkin
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.
当然,您可以使用任何您喜欢的名称——在这种情况下没有真正的命名约定。
回答by Tarantula
It's a convention, since the canvas in the mostly open-source browsers uses Cairo, and in Cairo, they're called "Contexts".
这是一个约定,因为大多数开源浏览器中的画布使用 Cairo,而在 Cairo,它们被称为“上下文”。
回答by gor
It is just name for variable. It could be anything. Ctx is just short word for ConTeXt.
它只是变量的名称。它可以是任何东西。Ctx 只是ConTeXt 的简称。
回答by stecb
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 :)
我认为“ctx”真的很好解释而且足够简短:)
回答by Delta
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".
可能是因为这些例子在教一些东西,谁写的想确保读者知道 ctx 是画布上下文,因为 ctx 是上下文的缩写,但是当你可以只写“ctx”时写“上下文”太无聊了。