Skip to content

Commit ee8f2dd

Browse files
committed
copy everything
1 parent 09a1e0b commit ee8f2dd

File tree

3 files changed

+32
-68
lines changed

3 files changed

+32
-68
lines changed

README.md

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,18 @@ With your support, I can make pixi-viewport even better! Please consider making
1111
<img src="https://opencollective.com/pixi-viewport/donate/[email protected]?color=blue" width=300 style="margin-top: 0.5rem; display: block"/>
1212
</a>
1313

14+
## v5+
15+
Moves pixi-viewport to pixi.js v7 (thanks [@cuire](https://github.com/cuire)!).
16+
17+
NOTE: there is a breaking change since pixi-viewport moved to pixi's new event system. `options.interaction` is removed and you need pass `options.events` to the viewport for it to work properly. The events object can be found at pixi's `renderer.events` or `app.renderer.events`.
18+
19+
```js
20+
const viewport = new Viewport({ events: renderer.events });
21+
22+
// or
23+
// const viewport = new Viewport({ events: app.renderer.events });
24+
```
25+
1426
## v4.30.0+
1527
This project was migrated to Typescript (thanks [@ShukantPal](https://github.com/ShukantPal)!). All functionality should be the same. The live Example has been updated.
1628

@@ -40,7 +52,7 @@ const viewport = new Viewport({
4052
worldWidth: 1000,
4153
worldHeight: 1000,
4254

43-
interaction: app.renderer.plugins.interaction // the interaction module is important for wheel to work properly when renderer.view is placed or scaled
55+
events: app.renderer.events // the interaction module is important for wheel to work properly when renderer.view is placed or scaled
4456
})
4557

4658
// add the viewport to the stage
@@ -118,9 +130,6 @@ viewport.plugins.add('name', plugin, index)
118130

119131
PRs are more than welcome!
120132

121-
## v4.30.0+
122-
This project was migrated to Typescript (thanks [@sukantpal](https://github.com/SukantPal)!). All functionality should be the same. The live Example has been updated.
123-
124133
## Other Libraries
125134
If you liked pixi-viewport, please try my other open source libraries:
126135
* [pixi-scrollbox](https://github.com/davidfig/pixi-scrollbox) - pixi.js scrollbox: a masked box that can scroll vertically or horizontally with scrollbars (uses pixi-viewport)
@@ -129,4 +138,4 @@ If you liked pixi-viewport, please try my other open source libraries:
129138

130139
## license
131140
MIT License
132-
(c) 2021 [YOPEY YOPEY LLC](https://yopeyopey.com/) by David Figatner ([email protected])
141+
(c) 2023 [YOPEY YOPEY LLC](https://yopeyopey.com/) by David Figatner ([email protected])

src/InputManager.ts

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export class InputManager
5151
this.viewport.on('pointercancel', this.up, this);
5252
this.viewport.on('pointerout', this.up, this);
5353
this.wheelFunction = (e) => this.handleWheel(e);
54-
this.viewport.options.divWheel.addEventListener(
54+
this.viewport.options.events.domElement.addEventListener(
5555
'wheel',
5656
this.wheelFunction as any,
5757
{ passive: this.viewport.options.passiveWheel });
@@ -64,7 +64,7 @@ export class InputManager
6464
*/
6565
public destroy(): void
6666
{
67-
this.viewport.options.divWheel.removeEventListener('wheel', this.wheelFunction as any);
67+
this.viewport.options.events.domElement.removeEventListener('wheel', this.wheelFunction as any);
6868
}
6969

7070
/**
@@ -207,22 +207,7 @@ export class InputManager
207207
{
208208
const point = new Point();
209209

210-
if (this.viewport.options.interaction)
211-
{
212-
this.viewport.options.interaction.mapPositionToPoint(point, event.clientX, event.clientY);
213-
}
214-
else if (this.viewport.options.useDivWheelForInputManager && this.viewport.options.divWheel)
215-
{
216-
const rect = this.viewport.options.divWheel.getBoundingClientRect();
217-
218-
point.x = event.clientX - rect.left;
219-
point.y = event.clientY - rect.top;
220-
}
221-
else
222-
{
223-
point.x = event.clientX;
224-
point.y = event.clientY;
225-
}
210+
this.viewport.options.events.mapPositionToPoint(point, event.clientX, event.clientY);
226211

227212
return point;
228213
}
@@ -235,13 +220,6 @@ export class InputManager
235220
return;
236221
}
237222

238-
// do not handle events coming from other elements
239-
if (this.viewport.options.interaction
240-
&& (this.viewport.options.interaction as any).interactionDOMElement !== event.target)
241-
{
242-
return;
243-
}
244-
245223
// only handle wheel events where the mouse is over the viewport
246224
const point = this.viewport.toLocal(this.getPointerPosition(event));
247225

src/Viewport.ts

Lines changed: 15 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -69,36 +69,21 @@ export interface IViewportOptions
6969
noTicker?: boolean;
7070

7171
/**
72-
* EventSystem, available from instantiated `WebGLRenderer/CanvasRenderer.plugins.interaction`
73-
*
74-
* It's used to calculate pointer postion relative to canvas location on screen
72+
* EventSystem is required now
7573
*/
76-
interaction?: EventSystem | null;
74+
events: EventSystem;
7775

7876
/**
79-
* Remove oncontextmenu=() => {} from the divWheel element
77+
* Remove oncontextmenu=() => {} from options.events.domElement
8078
*/
8179
disableOnContextMenu?: boolean;
8280

83-
/**
84-
* div to attach the wheel event
85-
*
86-
* @default document.body
87-
*/
88-
divWheel?: HTMLElement;
89-
9081
/**
9182
* Use this PIXI.ticker for updates
9283
*
9384
* @default PIXI.Ticker.shared
9485
*/
9586
ticker?: Ticker;
96-
97-
/**
98-
* Uses divWheel definition for InputManager to calculate positioning relative to containing div
99-
* this is used only if options.interaction is not defined
100-
*/
101-
useDivWheelForInputManager?: boolean;
10287
}
10388

10489
export interface ICompleteViewportOptions extends IViewportOptions
@@ -120,7 +105,7 @@ export interface IViewportTransformState
120105
scaleY: number;
121106
}
122107

123-
const DEFAULT_VIEWPORT_OPTIONS: ICompleteViewportOptions = {
108+
const DEFAULT_VIEWPORT_OPTIONS: Partial<ICompleteViewportOptions> = {
124109
screenWidth: window.innerWidth,
125110
screenHeight: window.innerHeight,
126111
worldWidth: null,
@@ -130,7 +115,6 @@ const DEFAULT_VIEWPORT_OPTIONS: ICompleteViewportOptions = {
130115
stopPropagation: false,
131116
forceHitArea: null,
132117
noTicker: false,
133-
interaction: null,
134118
disableOnContextMenu: false,
135119
ticker: Ticker.shared,
136120
};
@@ -192,7 +176,7 @@ export class Viewport extends Container
192176
public lastViewport?: IViewportTransformState | null;
193177

194178
/** The options passed when creating this viewport, merged with the default values */
195-
public readonly options: ICompleteViewportOptions & { divWheel: HTMLElement };
179+
public readonly options: ICompleteViewportOptions;
196180

197181
private _dirty?: boolean;
198182
private _forceHitArea?: IHitArea | null;
@@ -218,21 +202,17 @@ export class Viewport extends Container
218202
* @param {HitArea} [options.forceHitArea] change the default hitArea from world size to a new value
219203
* @param {boolean} [options.noTicker] set this if you want to manually call update() function on each frame
220204
* @param {PIXI.Ticker} [options.ticker=PIXI.Ticker.shared] use this PIXI.ticker for updates
221-
* @param {PIXI.EventSystem} [options.interaction=null] EventSystem, available from instantiated
222-
* WebGLRenderer/CanvasRenderer.plugins.interaction - used to calculate pointer position relative to canvas
205+
* @param {PIXI.EventSystem} [options.events] EventSystem available from app.events or added manually and passed here
223206
* location on screen
224-
* @param {HTMLElement} [options.divWheel=document.body] div to attach the wheel event
225-
* @param {boolean} [options.disableOnContextMenu] remove oncontextmenu=() => {} from the divWheel element
207+
* @param {boolean} [options.disableOnContextMenu] remove oncontextmenu=() => {} from the pixi's events.domElement
226208
*/
227-
constructor(options: IViewportOptions = {})
209+
constructor(options: IViewportOptions)
228210
{
229211
super();
230-
this.options = Object.assign(
231-
{},
232-
{ divWheel: document.body },
233-
DEFAULT_VIEWPORT_OPTIONS,
234-
options
235-
);
212+
this.options = {
213+
...DEFAULT_VIEWPORT_OPTIONS,
214+
...options,
215+
} as ICompleteViewportOptions;
236216

237217
this.screenWidth = this.options.screenWidth;
238218
this.screenHeight = this.options.screenHeight;
@@ -242,11 +222,9 @@ export class Viewport extends Container
242222
this.forceHitArea = this.options.forceHitArea;
243223
this.threshold = this.options.threshold;
244224

245-
this.options.divWheel = this.options.divWheel || document.body;
246-
247225
if (this.options.disableOnContextMenu)
248226
{
249-
this.options.divWheel.addEventListener('contextmenu', this._disableOnContextMenu);
227+
this.options.events.domElement.addEventListener('contextmenu', this._disableOnContextMenu);
250228
}
251229
if (!this.options.noTicker)
252230
{
@@ -267,7 +245,7 @@ export class Viewport extends Container
267245
}
268246
if (this.options.disableOnContextMenu)
269247
{
270-
this.options.divWheel.removeEventListener('contextmenu', this._disableOnContextMenu);
248+
this.options.events.domElement.removeEventListener('contextmenu', this._disableOnContextMenu);
271249
}
272250

273251
this.input.destroy();
@@ -1177,8 +1155,7 @@ export class Viewport extends Container
11771155
/**
11781156
* Zoom using mouse wheel
11791157
*
1180-
* NOTE: the default event listener for 'wheel' event is document.body. Use `Viewport.options.divWheel` to
1181-
* change this default
1158+
* NOTE: the default event listener for 'wheel' event is the options.events.domElement.
11821159
*
11831160
* @param {IWheelOptions} [options]
11841161
* @param {number} [options.percent=0.1] - percent to scroll with each spin

0 commit comments

Comments
 (0)