ferdyyy
ferdyyy

Reputation: 535

SpriteKit SKCropNode cropping wrong area

I am using SpriteKit to draw a graph (with the ability to zoom in and pan around).

When I use an SKCropNode to crop the grid of my graph it doesn't crop the desired area. It crops less, no matter if I use a rectangular SKShapeNode or a SKSpriteNode (with image) as .maskNode.

Here is my code:

    //GRID
    let grid = SKCropNode()
    graphViewModel.graphScene.addChild(grid)

    let ratio:CGFloat = 1000 / 500
    let width = (graphViewModel.sceneSize.width*0.95)
    let newSize = CGSize(width: width, height: width/ratio)

    let origin = CGPoint(x: -newSize.width/2.0, y: 0.0)
    let rectangularMask = SKShapeNode(rect: CGRect(origin: origin, size: newSize))
    rectangularMask.fillColor = UIColor.lightGray
    rectangularMask.zPosition = -10.0 //So it appears behind the grid, doesn't affect the cropping
    grid.maskNode = rectangularMask
    graphViewModel.graphScene.addChild(rectangularMask)

Here are two screenshots to illustrate what I mean:

This is the graph with its grid not being cropped. Graph Grid not cropped

This is the graph with the maskNode set. The lightGray Area is the actual rectangularNode and the grid is being cut off a lot less than it ought to be. Graph Grid cropped but wrongly

My scene is scaled so I can zoom in without pixelating. When I disable zooming (setting the scene's size to the view's size) then the bug disappears. Unfortunately I need zooming without any pixel artefacts.

Maybe someone has an idea how to fix this issue. It might also be a SpriteKit Bug.

Upvotes: 3

Views: 425

Answers (0)

Related Questions