【问题标题】:NSBezierPath stroke not scaled correctlyNSBezierPath 笔划未正确缩放
【发布时间】:2021-02-18 18:19:28
【问题描述】:

我有一个简单的 NSView 子类,它在指定位置绘制圆形节点。

为了在我的视图中渲染节点,我将图形上下文的原点转换为视图框架的中心并对其进行缩放,使其在限制维度中从 -1.25 跨越到 1.25(节点坐标都在范围 -1...1)。然后我为每个节点创建一个 NSBezierPath 使用椭圆形:构造函数。最后,我用黄色填充路径并用黑色描边。

但是...虽然黄色填充看起来不错,但黑色轮廓没有正确缩放!

我错过了什么?

代码如下:

  override func draw(_ dirtyRect: NSRect)
  {
    let nodeRadius   = CGFloat(0.05)
    let unscaledSpan = CGFloat(2.5)
    
    super.draw(dirtyRect)
    
    NSColor.white.set()
    self.frame.fill()
    
    guard let graph = graph else { return }
    
    let scale = min(bounds.width/unscaledSpan, bounds.height/unscaledSpan)
    
    NSGraphicsContext.current?.saveGraphicsState()
    defer { NSGraphicsContext.current?.restoreGraphicsState() }
    
    let xform = NSAffineTransform()
    xform.translateX( by: 0.5*bounds.width, yBy: 0.5*bounds.height)
    xform.scale(by: scale)
    xform.concat()
    
    for v in graph.vertices
    {
      let r = NSRect(x: v.x-nodeRadius, y: v.y-nodeRadius, width: 2.0*nodeRadius, height: 2.0*nodeRadius)
      let p = NSBezierPath(ovalIn: r)
      NSColor.yellow.set()
      p.fill()
      NSColor.black.set()
      p.stroke()
    }
  }

这就是我所看到的(显示了两种不同的窗口大小

显然,填充和描边的翻译都可以正常工作。 但是,描边的缩放是关闭的。

感谢任何/所有提示/建议。

【问题讨论】:

    标签: swift core-graphics nsview


    【解决方案1】:

    Doh...我没有考虑缩放对线宽的影响。

    进行了以下编辑,一切都很好:

      ...
      let p = NSBezierPath(ovalIn: r)
      p.lineWidth = CGFloat(0.01)
      NSColor.yellow.set()
      p.fill()
      NSColor.black.set()
      p.stroke()
      ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多