PIXI Canvas 点击事件如下

image.png

   onMounted(() => {
      console.log('mounted')

      app.renderer.plugins.interaction.on('pointerdown', (event: PIXI.InteractionEvent) => {
        let ax = event.data.getLocalPosition(container)
        console.log(ax)
      })

    })

通过监听pointerdown事件获取点击时的PIXI Event

可以获取相关的信息,其中有一个Global.Point 这个坐标是相对的,根据不同的页面大小会显示不同的坐标,并非真是坐标,需要对其加以转换

根据文档https://pixijs.io/examples/#/interaction/dragging.js 可知,有getLocalPosition相关API

InteractionData.getLocalPosition<PIXI.Point>(displayObject: PIXI.DisplayObject, point?: PIXI.Point | undefined, globalPos?: PIXI.IPointData | undefined): PIXI.Point

getLocalPositio() 方法需要有对应的DisplayObject

若画布中有GraphicsSprite 或其他类型的DisplayObject 则直接引用即可

如无特殊DisplayObject 可以添加一个PIXI.Container 作为容器

    const container = new PIXI.Container()

    app.stage.addChild(container)

PIXI .DisplayObject

在屏幕上呈现的所有对象的基类。

Display objects implemented in PixiJS

Display ObjectDescription
PIXI.ContainerAdds support for children to DisplayObject
PIXI.GraphicsShape-drawing display object similar to the Canvas API
PIXI.SpriteDraws textures (i.e. images)
PIXI.TextDraws text using the Canvas API internally
PIXI.BitmapTextMore scaleable solution for text rendering, reusing glyph textures
PIXI.TilingSpriteDraws textures/images in a tiled fashion
PIXI.AnimatedSpriteDraws an animation of multiple images
PIXI.MeshProvides a lower-level API for drawing meshes with custom data
PIXI.NineSlicePlaneMesh-related
PIXI.SimpleMeshv4-compatibile mesh
PIXI.SimplePlaneMesh-related
PIXI.SimpleRopeMesh-related