Skip to content

Commit 506ccd8

Browse files
authored
Merge pull request #334 from vijayth2-cerebras/master
add geometry prop to Graphics
2 parents 6eae432 + 2ab7275 commit 506ccd8

File tree

4 files changed

+96
-7
lines changed

4 files changed

+96
-7
lines changed

src/components/Graphics.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import { Graphics as PixiGraphics } from 'pixi.js'
22
import { applyDefaultProps } from '../utils/props'
3+
import invariant from '../utils/invariant'
34

4-
const Graphics = (root, props) => {
5-
const g = new PixiGraphics()
5+
const Graphics = (root, { geometry }) => {
6+
invariant(!geometry || geometry instanceof PixiGraphics, `Graphics geometry needs to be a \`PIXI.Graphics\``)
7+
const g = geometry ? new PixiGraphics(geometry.geometry) : new PixiGraphics()
68
g.applyProps = (instance, oldProps, newProps) => {
7-
const { draw, ...props } = newProps
9+
const { draw, geometry, ...props } = newProps
810
let changed = applyDefaultProps(instance, oldProps, props)
9-
1011
if (oldProps.draw !== draw && typeof draw === 'function') {
1112
changed = true
1213
draw.call(g, g)

src/components/Graphics.mdx

+42-3
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@ https://pixijs.download/dev/docs/PIXI.Graphics.html
1414

1515
## Props
1616

17-
| prop | description |
18-
|------ |------------------------ |
19-
| draw | Draw callback function |
17+
| prop | description |
18+
|--------- |------------------------ |
19+
| draw | Draw callback function |
20+
| geometry | Graphics object |
2021

2122

2223
## Usage
@@ -72,6 +73,43 @@ function Rectangle(props) {
7273
}
7374
```
7475

76+
### The `geometry` prop
77+
Provides another Graphics object as a template. Helps in reducing memory footprint if the same shapes are used repeatedly.
78+
79+
```jsx
80+
import React, { useCallback } from 'react';
81+
import { Graphics } from '@inlet/react-pixi';
82+
83+
function Rectangle(props) {
84+
const draw = useCallback((g) => {
85+
g.clear();
86+
g.lineStyle(props.lineWidth, props.color);
87+
g.drawRect(
88+
props.lineWidth,
89+
props.lineWidth,
90+
props.width - 2 * props.lineWidth,
91+
props.height - 2 * props.lineWidth
92+
);
93+
}, [props]);
94+
95+
return <Graphics draw={draw} />
96+
}
97+
98+
function Grid(props) {
99+
100+
const geometry = <Rectangle {...props} />
101+
102+
return (
103+
<>
104+
<Graphics x={0} y={0} geometry={geometry} />
105+
<Graphics x={props.width} y={0} geometry={geometry} />
106+
<Graphics x={0} y={props.height} geometry={geometry} />
107+
<Graphics x={props.width} y={props.height} geometry={geometry} />
108+
</>
109+
);
110+
}
111+
```
112+
75113
## Example
76114

77115
<iframe
@@ -83,3 +121,4 @@ function Rectangle(props) {
83121
allowFullScreen={true}
84122
style={{ width: '100%' }}
85123
/>
124+
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`graphics renders a graphics component with draw prop 1`] = `<canvas />`;
4+
5+
exports[`graphics renders a graphics component with geometry prop 1`] = `<canvas />`;

test/graphics.spec.js

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import React from 'react'
2+
import * as PIXI from 'pixi.js'
3+
import renderer from 'react-test-renderer'
4+
import * as reactTest from '@testing-library/react'
5+
import { PixiFiber } from '../src'
6+
import { Container, Stage, Text, Graphics } from '../src'
7+
import { Context } from '../src/stage/provider'
8+
import { getCanvasProps } from '../src/stage'
9+
import { mockToSpy } from './__utils__/mock'
10+
11+
jest.mock('../src/reconciler')
12+
jest.useFakeTimers()
13+
14+
describe('graphics', () => {
15+
beforeEach(() => {
16+
window.matchMedia.mockClear()
17+
jest.clearAllMocks()
18+
mockToSpy('../src/reconciler')
19+
})
20+
21+
test.skip('renders a graphics component with draw prop', () => {
22+
const spy = jest.fn()
23+
const tree = renderer.create(
24+
<Stage>
25+
<Graphics draw={spy} />
26+
</Stage>
27+
).toJSON();
28+
expect(tree).toMatchSnapshot()
29+
expect(spy).toHaveBeenCalledTimes(1)
30+
})
31+
32+
test.skip('renders a graphics component with geometry prop', () => {
33+
const spy = jest.fn()
34+
const geometry = <Graphics draw={spy} />
35+
const tree = renderer.create(
36+
<Stage>
37+
<Graphics geometry={geometry} />
38+
<Graphics geometry={geometry} />
39+
</Stage>
40+
).toJSON();
41+
expect(tree).toMatchSnapshot()
42+
expect(spy).toHaveBeenCalledTimes(1)
43+
})
44+
})

0 commit comments

Comments
 (0)