【问题标题】:set origin coordinates for react-konva stage为 react-konva 阶段设置原点坐标
【发布时间】:2017-11-15 10:34:22
【问题描述】:

我使用react-konva 进行绘图。我已经配置了stage,并且我正在那个stage 容器中绘制某些形状。我面临的问题是原点坐标(0,0)位于舞台容器的左上角。我希望舞台的中心是原点(0,0)。这是目前的代码:

<Stage
  height={800}
  width={1200}
  style={{ backgroundColor: '#fff', border: 'solid'}}>
  {
    this.state.circlePoints.length !== 0 &&
    <LineComponent
      startX={1200/2}
      startY={800/2}
      endX={this.state.circlePoints[0].pointX*1.3}
      endY={this.state.circlePoints[0].pointY*1.3}
      startColor={firstCircle[0].outerColor}
      endColor={pmData[0].outerColor}
    />
  }
  <CircleComponent
    x={1200/2}
    y={800/2}
    outerRadius={firstCircle[0].weight*1200}
    outerColor={firstCircle[0].outerColor}
    innerRadius={firstCircle[0].weight*1200*0.3}
    innerColor={firstCircle[0].innerColor}
    shadowColor={firstCircle[0].innerColor}
    getCirclePoints={this.getCirclePoints}
  />
  {
    this.state.circlePoints.length !== 0 &&
    <CircleComponent
      x={this.state.circlePoints[0].pointX*1.3}
      y={this.state.circlePoints[0].pointY*1.3}
      outerRadius={pmData[0].weight*1200}
      outerColor={pmData[0].outerColor}
      innerRadius={pmData[0].weight*1200*0.3}
      innerColor={pmData[0].innerColor}
      shadowColor={pmData[0].innerColor}
    />
  }
</Stage>

【问题讨论】:

    标签: javascript reactjs konvajs


    【解决方案1】:

    使用图层偏移命令在舞台上重新定位图层。在下面的示例中,您可以看到我添加了 x 和 y 轴,然后将图层偏移 200px 到页面中。最后,我在 (0,0) 的图层上画了一个圆圈,以确认图层坐标仍然基于此位置,这样您就不必对绘图坐标进行任何平移。

    var stage = new Konva.Stage({
          container: 'container',
          width: 600,
          height: 400
        });
    
    var layer = new Konva.Layer();
    stage.add(layer)
    
    var axisX = new Konva.Line({
          points: [-200, 0, 200, 0],
          stroke: 'red',
          strokeWidth: 2,
          lineCap: 'round',
          lineJoin: 'round'
        });
      
    var axisY = new Konva.Line({
          points: [0, 200, 0, -200],
          stroke: 'red',
          strokeWidth: 2,
          lineCap: 'round',
          lineJoin: 'round'
        });  
      
    layer.add(axisX)  
    layer.add(axisY)  
    
    // offset the layer on the stage
    layer.offsetX(-200)
    layer.offsetY(-200)
    
    // draw a circle at 0,0
    
        var circle = new Konva.Circle({
          x: 0,
          y: 0,
          radius: 70,
          stroke: 'black',
          strokeWidth: 4
        });
    
        // add the shape to the layer
        layer.add(circle);
    
    layer.draw();
    stage.draw();
    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="utf-8" />
        <title></title>
    </head>
    <body>
      <div id="container"></div>
                
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
        <script src="https://cdn.rawgit.com/konvajs/konva/1.7.6/konva.min.js"></script>
        <script type="text/javascript" src="test.js"></script>
    
    
    
    </body>
    </html>

    【讨论】:

      【解决方案2】:

      对于以上方法无效的,请执行以下操作:

      1. 按舞台(画布)高度偏移 Y
      2. ScaleY 按当前比例的负值。 (如果没有缩放使用 -1)

      例如。在react-konva

      &lt;Stage width={500} height={500} scaleY={-1} offsetY={500}&gt;

      【讨论】:

      • 除文本将被反转。有一个讨论 here 寻找一种可能的方法。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-09
      • 2012-11-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多