简体   繁体   English

将画布原点设置为左下角

[英]Set canvas origin to the lower-left corner

Is there a way to set the origin to lower-left of the canvas?有没有办法将原点设置为画布的左下角? I tried scaling by −1 but after that everything is upside down.我尝试按 -1 缩放,但之后一切都颠倒了。 I need to make something like a coordinate system but only with the positive part (first quadrant).我需要制作类似坐标系的东西,但只能使用正部分(第一象限)。 So I need it to start with 0.0 at down-left corner.所以我需要它从左下角的 0.0 开始。

ctx.translate(0, canvas.height);
ctx.scale(1, -1);

See a demo on JSFiddle.请参阅 JSFiddle 上的演示。

is there a way to set the origin to lower-left of the canvas?有没有办法将原点设置在画布的左下角?

Not quite the way you'd like, no.不完全是你想要的方式,不。 The best you can do there is what icktoofay said.你能做的最好的事情就是 icktoofay 所说的。

That being said it isn't too hard to make a function to convert between one system and the other.话虽如此,制作一个在一个系统和另一个系统之间转换的函数并不难。 For example:例如:

// Given an X,Y in 1st quadrant cartesian coordinates give the corresponding screen coordinate
function cartToScreen(px, py) {
  return [px, -py + HEIGHT];
};

So you'd write:所以你会写:

var coords = cartToScreen(50,50);
// draws 50 pixels from the bottoom instead of 50 pixels from the top
ctx.fillText("lalala", coords[0], coords[1]);

Example例子

In any case, I'd strongly suggest that if you are at all able to just get used to screen coordinates.在任何情况下,如果您完全能够习惯屏幕坐标,我强烈建议您。 It will save you from loads of headaches in the future if you don't always have to account for this little difference.如果您不必总是考虑到这个微小的差异,它将使您免于未来的大量头痛。

最简单和正确的方法之一是使用 ctx.transform() 方法文档在这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM