Skip to content

Commit c5b0a5b

Browse files
committed
feat: invert flag
1 parent 0168727 commit c5b0a5b

File tree

6 files changed

+35
-5
lines changed

6 files changed

+35
-5
lines changed

src/packlets/dashboard/App.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { FunctionComponent } from 'react'
99
import { PrintRender } from '../print/Render'
1010
import { Descriptor } from './Descriptor'
1111
import { DiameterInput } from './input/Diameter'
12+
import { InvertInput } from './input/Invert'
1213
import { TagIdInput } from './input/TagId'
1314
import { TypeInput } from './input/Type'
1415
import { UrlInput } from './input/Url'
@@ -22,6 +23,7 @@ export const App: FunctionComponent = () => {
2223
<TypeInput />
2324
<TagIdInput />
2425
<UrlInput />
26+
<InvertInput />
2527
<DiameterInput />
2628
</VStack>
2729
<Descriptor />
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { HStack, Switch, Text } from '@chakra-ui/react'
2+
import { inputAtom, useInputAtom } from '../inputAtom'
3+
4+
export const InvertInput = () => {
5+
const { invert } = useInputAtom()
6+
7+
return (
8+
<HStack spacing={3}>
9+
<Text>Invert</Text>
10+
<Switch
11+
size="md"
12+
isChecked={invert}
13+
onChange={() => inputAtom.setKey('invert', !invert)}
14+
/>
15+
</HStack>
16+
)
17+
}

src/packlets/dashboard/inputAtom.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ interface Options {
77
tagId: string
88
diameter: number
99
url: string
10+
invert: boolean
1011
}
1112

1213
export const inputAtom = persistentMap<Options>(
@@ -16,6 +17,7 @@ export const inputAtom = persistentMap<Options>(
1617
tagId: 'CG00123',
1718
diameter: 5,
1819
url: 'grtn.org/i',
20+
invert: false,
1921
},
2022
{
2123
encode: JSON.stringify,

src/packlets/print/Render.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { drawSticker } from './canvas/drawSticker'
77
import './render.css'
88

99
export const PrintRender: FunctionComponent = () => {
10-
const { type, diameter, tagId, url } = useInputAtom()
10+
const { type, diameter, tagId, url, invert } = useInputAtom()
1111
const canvasRef = useRef(null)
1212

1313
useEffect(() => {
@@ -19,9 +19,10 @@ export const PrintRender: FunctionComponent = () => {
1919

2020
const ctx = canvas.getContext('2d')!
2121

22-
if (type === 'sticker') drawSticker(canvas, ctx, tagId, url)
23-
else if (type === 'flag') drawFlag(canvas, ctx, tagId, url, diameter)
24-
}, [type, tagId, canvasRef, diameter, url])
22+
if (type === 'sticker') drawSticker(canvas, ctx, tagId, url, invert)
23+
else if (type === 'flag')
24+
drawFlag(canvas, ctx, tagId, url, invert, diameter)
25+
}, [type, tagId, canvasRef, diameter, url, invert])
2526

2627
return <canvas ref={canvasRef} />
2728
}

src/packlets/print/canvas/drawFlag.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export const drawFlag = async (
1010
ctx: CanvasRenderingContext2D,
1111
tagId: string,
1212
url: string,
13+
invert: boolean,
1314
diameter: number
1415
) => {
1516
const wrapAround = Math.ceil(20 * diameter)
@@ -22,6 +23,9 @@ export const drawFlag = async (
2223
canvas.width = width
2324
canvas.height = height
2425
canvas.dataset.tape = 'black/white'
26+
27+
if (invert) ctx.filter = 'invert(1)'
28+
2529
ctx.fillStyle = 'white'
2630
ctx.fillRect(0, 0, width, height)
2731
ctx.fillStyle = 'black'

src/packlets/print/canvas/drawSticker.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,18 @@ export const drawSticker = async (
1010
canvas: HTMLCanvasElement,
1111
ctx: CanvasRenderingContext2D,
1212
tagId: string,
13-
url: string
13+
url: string,
14+
invert: boolean
1415
) => {
1516
/**
1617
* Prepare the canvas
1718
*/
1819
canvas.width = 112
1920
canvas.height = 174
2021
canvas.dataset.tape = 'gold/black'
22+
23+
if (invert) ctx.filter = 'invert(1)'
24+
2125
ctx.fillStyle = 'white'
2226
ctx.fillRect(0, 0, 112, 174)
2327
ctx.fillStyle = 'black'

0 commit comments

Comments
 (0)