pixi-viewport
A highly configurable viewport/2D camera designed to work with pixi.js.
Features include dragging, pinch-to-zoom, mouse wheel zooming, decelerated dragging, follow target, animate, snap to point, snap to zoom, clamping, bouncing on edges, and move on mouse edges. See the live examples below to try out all of these features.
All features are configurable and removable, so set up the viewport to be exactly what you need.
Support pixi-viewport!
With your support, I can make pixi-viewport even better! Please consider making a donation:
Live Examples
API Documentation
https://davidfig.github.io/pixi-viewport/jsdoc/
Simple Example
import * as PIXI from 'pixi.js'
import { Viewport } from 'pixi-viewport'
const app = new PIXI.Application()
document.body.appendChild(app.view)
const viewport = new Viewport({
screenWidth: window.innerWidth,
screenHeight: window.innerHeight,
worldWidth: 1000,
worldHeight: 1000,
interaction: app.renderer.plugins.interaction
})
app.stage.addChild(viewport)
viewport
.drag()
.pinch()
.wheel()
.decelerate()
const sprite = viewport.addChild(new PIXI.Sprite(PIXI.Texture.WHITE))
sprite.tint = 0xff0000
sprite.width = sprite.height = 100
sprite.position.set(100, 100)
Installation
yarn add pixi-viewport
or
npm i pixi-viewport
or grab the latest release and use it:
<script src="/directory-to-file/pixi.js"></script>
<script src="/directory-to-file/viewport.min.js"></script>
<script>
const Viewport = new pixi_viewport.Viewport(options)
</script>
Build Examples
I've included the following build examples in the docs/ directory:
You can see the live demos at https://davidfig.github.io/pixi-viewport/builds/.
Migration from pixi-viewport v3 to v4
Viewport needs to be imported or required as follows:
import { Viewport } from 'pixi-viewport'
const Viewport = require('pixi-viewport').Viewport
Plugins have been moved to their own object:
viewport.plugins.pause('drag')
viewport.plugins.resume('drag')
viewport.plugins.remove('drag')
viewport.plugins.add('name', plugin, index)
Tests
- Clone repository
- yarn install
- yarn test (for Mocha test code)
- yarn coverage (for Instanbul coverage)
Development Recipe
- clone repository
- yarn install
- yarn dev
- open browser to http://localhost:10001
PRs are more than welcome!
v4.30.0+
This project was migrated to Typescript (thanks @sukantpal!). All functionality should be the same. The live Example has been updated.
Other Libraries
If you liked pixi-viewport, please try my other open source libraries:
- pixi-scrollbox - pixi.js scrollbox: a masked box that can scroll vertically or horizontally with scrollbars (uses pixi-viewport)
- pixi-ease - pixi.js animation library using easing functions
- intersects - a simple collection of 2d collision/intersects functions. Supports points, circles, lines, axis-aligned boxes, and polygons
license
MIT License
(c) 2021 YOPEY YOPEY LLC by David Figatner (david@yopeyopey.com)