将HTML Canvas坐标系转换为笛卡尔系统

时间:2019-01-30 16:16:57

标签: javascript html5-canvas konvajs

HTML画布的原点(0,0)在左上角(请参见下图),其中x坐标正向右,y坐标正向向下。

enter image description here

但是,数学的原点是“中心”,并且正y通过Cartesian coordinate system(参见下文)向上

enter image description here

我在KonvaJS中使用阶段的偏移量和比例来反映X-max为10,Y-max为10,X-min为-10和Y-min为-10的笛卡尔系统。但是,我当前的解决方案是将象限上下颠倒(请参阅下文)。有什么建议吗?

enter image description here

当前的JavaScript方法: JSFiddle

var Width = 500;
var Height = 500;
var minX = -12; var maxX = 12;
var minY  = -12; var maxY  = 12;
var rangeX  = maxX - minX;
var rangeY  = maxY - minY;
var scaleX  = Width / rangeX;
var scaleY = Height  / rangeY;

var stage = new Konva.Stage({
container: 'container',
width: Width,
height: Height,
scaleX: scaleX,
scaleY: scaleY,
offset: {
	x: -12,
	y: -12
}			
});		

var layer = new Konva.Layer();
stage.add(layer);

var rect = new Konva.Rect({
  x: -12,
  y: -12,
  width: 24,
  height: 24,		  

  stroke: 'black',
  strokeWidth: 0.1
});		
layer.add(rect);

var position = new Konva.Text(
{
	fontSize: 0.7,
	x: -11,
	y: -11			
});
 
layer.add(position);

var t0 = new Konva.Text(
{
fontSize: 1,
x: 0,
y: 0,
text: 'Center'

});		 
var t1 = new Konva.Text(
{
fontSize: 0.7,
x: 5,
y: 5,
text: 'Top\nRight',
fill: 'blue'
});		 
var t2 = new Konva.Text(
{
fontSize: 0.7,
x: -5,
y: 5,
text: 'Top\nLeft',
fill: 'blue'
});		 
var t3 = new Konva.Text(
{
fontSize: 0.7,
x: 5,
y: -5,
text: 'Bottom\nRight',
fill: 'blue'
});		 
var t4 = new Konva.Text(
{
fontSize: 0.7,
x: -5,
y: -5,
text: 'Bottom\nLeft',
fill: 'blue'			
});		 
layer.add(t0);	
layer.add(t1);
layer.add(t2);
layer.add(t3);
layer.add(t4);	

 var yaxis = new Konva.Arrow({
  points: [0, 11, 0, -11],
  pointerLength: 0.3,
  pointerWidth: 0.2,
  pointerAtBeginning: true,
  fill: 'green',
  stroke: 'green',
  strokeWidth: 0.1
});
layer.add(yaxis);

var xaxis = new Konva.Arrow({
  points: [11, 0, -11,0],
  pointerLength: 0.3,
  pointerWidth: 0.2,
  pointerAtBeginning: true,
  fill: 'green',
  stroke: 'green',
  strokeWidth: 0.1
});
layer.add(xaxis);

var circle = new Konva.Circle({
  x: 0,
  y: 5,
  radius: 0.5,
  fill: 'purple',
  stroke: 'purple',
  strokeWidth: 0,
  draggable: true
});		

layer.add(circle);

layer.draw();

function updateText(e) {
position.text('(' + Math.round(e.target.x()) + ', ' + Math.round(e.target.y()) + ')');
layer.batchDraw();
}

circle.on('dragmove', updateText);  
  
<script src="https://unpkg.com/konva@2.4.2/konva.min.js"></script>
<body>
 <div id="container"></div>
</body>

更新:

根据安德鲁·莫顿(Andrew Morton)的建议,设置:

  

scaleY = -Height / rangeY;

有正确的想法,但需要将舞台偏移量更改为:

      offset: {
          x: -12,
          y: 12           
      }

这将导致具有正确的象限,但所有文本均已反转(请参见下文)。还有其他建议吗?

enter image description here

2 个答案:

答案 0 :(得分:2)

由于y倒置,您需要

var scaleY = -Height / rangeY

但显然,这也会导致文本倒置。您可以使用属性在Text构造函数中对此进行纠正

scaleY: -1,
verticalAlign: 'top'

参考:Konva.Text

答案 1 :(得分:1)

您可以通过计算画布中的中点并执行一些基本的算术运算,将笛卡尔(正/负x和y)点转换为对画布有意义的位置:

var getCanvasCartesianPoint = function (x, y) {
  var midWidth = width / 2;
  var midHeight = height / 2;
  var cartesianX = midWidth + x;
  var cartesianY = midHeight + y;

  return {x: cartesianX, y: cartesianY};
}

因此,当您调用getCanvasCartesianPoint(0,0)并且宽度为250时,该函数将返回位于画布中间的{x: 125, y: 125}

请参阅此代码库进行演示-https://codepen.io/joshdavenport/pen/NobERq