Skip to content

Commit 75babdc

Browse files
committed
Add Star Shape
1 parent 3244ec1 commit 75babdc

File tree

6 files changed

+89
-4
lines changed

6 files changed

+89
-4
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,8 @@ enum StickerTypes {
129129
CROPPED = 'crop',
130130
FULL = 'full',
131131
CIRCLE = 'circle,
132-
ROUNDED = 'rounded'
132+
ROUNDED = 'rounded',
133+
STAR = 'star'
133134
}
134135

135136
```

examples/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@
77
await import('./full')
88
await import('./circle')
99
await import('./rounded')
10+
await import('./star')
1011
})().catch(console.error)

examples/star.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { Sticker } from '../src'
2+
;(async () => {
3+
console.log('\n---\n')
4+
console.log('Star example')
5+
console.log('---\n')
6+
const images = {
7+
static: {
8+
potrait: 'https://i.pinimg.com/originals/3a/53/d6/3a53d68345b56241a875595b21ec2a59.jpg',
9+
landscape: 'https://chasinganime.com/wp-content/uploads/2021/02/0_YgtEypuJ2QfMPCbn.jpg'
10+
},
11+
animated: {
12+
potrait: 'https://c.tenor.com/-1mtmQgH5eYAAAAC/watson-amelia-vtuber.gif',
13+
landscape: 'https://c.tenor.com/2RdLoyV5VPsAAAAC/ayame-nakiri.gif'
14+
}
15+
}
16+
const type = 'star'
17+
const getOptions = (pack = '', author = '') => ({
18+
pack,
19+
type,
20+
author: `${author}-${type}`
21+
})
22+
await (async () => {
23+
console.log('Static Potrait')
24+
const sticker = new Sticker(images.static.potrait, getOptions('static', 'potrait'))
25+
await sticker.toFile()
26+
console.log(`Saved to ${sticker.defaultFilename}`)
27+
})()
28+
await (async () => {
29+
console.log('Static Landscape')
30+
const sticker = new Sticker(images.static.landscape, getOptions('static', 'landscape'))
31+
await sticker.toFile()
32+
console.log(`Saved to ${sticker.defaultFilename}`)
33+
})()
34+
await (async () => {
35+
console.log('Animated Potrait')
36+
const sticker = new Sticker(images.animated.potrait, getOptions('animated', 'potrait'))
37+
await sticker.toFile()
38+
console.log(`Saved to ${sticker.defaultFilename}`)
39+
})()
40+
await (async () => {
41+
console.log('Animated Landscape')
42+
const sticker = new Sticker(images.animated.landscape, getOptions('animated', 'landscape'))
43+
await sticker.toFile()
44+
console.log(`Saved to ${sticker.defaultFilename}`)
45+
})()
46+
})()

src/internal/Metadata/StickerTypes.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@ export enum StickerTypes {
33
CROPPED = 'crop',
44
FULL = 'full',
55
CIRCLE = 'circle',
6-
ROUNDED = 'rounded'
6+
ROUNDED = 'rounded',
7+
STAR = 'star'
78
}

src/internal/convert.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ const convert = async (
5050
fit: fit.cover
5151
}).composite([
5252
{
53-
input: Buffer.from(`<svg><circle cx="256" cy="256" r="256" fill="${background}"/></svg>`),
53+
input: Buffer.from(
54+
`<svg width="512" height="512"><circle cx="256" cy="256" r="256" fill="${background}"/></svg>`
55+
),
5456
blend: 'dest-in',
5557
gravity: 'northeast',
5658
tile: true
@@ -64,7 +66,22 @@ const convert = async (
6466
}).composite([
6567
{
6668
input: Buffer.from(
67-
`<svg><rect rx="50" ry="50" width="512" height="512" fill="${background}"/></svg>`
69+
`<svg width="512" height="512"><rect rx="50" ry="50" width="512" height="512" fill="${background}"/></svg>`
70+
),
71+
blend: 'dest-in',
72+
gravity: 'northeast',
73+
tile: true
74+
}
75+
])
76+
break
77+
78+
case StickerTypes.STAR:
79+
img.resize(512, 512, {
80+
fit: fit.cover
81+
}).composite([
82+
{
83+
input: Buffer.from(
84+
`<svg width="512" height="512"><path d="m256 33.28 61.133 172.083H481.28L347.341 306.432l47.898 177.357L256 377.446 116.787 483.788l47.872-177.357L30.694 205.362h164.147L256 33.28z" fill="${background}"/></svg>`
6885
),
6986
blend: 'dest-in',
7087
gravity: 'northeast',

tests/Sticker.test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,15 @@ describe('Sticker', () => {
5959
const { height, width } = sizeOf(buffer)
6060
assert.equal(height, width)
6161
})
62+
63+
it('should create a star sticker same height and width', async () => {
64+
const sticker = new Sticker(images.static.landscape, {
65+
type: StickerTypes.STAR
66+
})
67+
const buffer = await sticker.build()
68+
const { height, width } = sizeOf(buffer)
69+
assert.equal(height, width)
70+
})
6271
})
6372

6473
describe('Animated Stickers', () => {
@@ -98,6 +107,16 @@ describe('Sticker', () => {
98107
assert.equal(width, 512)
99108
})
100109

110+
it('should create an animated star sticker with size 512x512', async () => {
111+
const sticker = new Sticker(images.animated.potrait, {
112+
type: StickerTypes.STAR
113+
})
114+
const buffer = await sticker.build()
115+
const { height, width } = sizeOf(buffer)
116+
assert.equal(height, 512)
117+
assert.equal(width, 512)
118+
})
119+
101120
it('should create an animated sticker same height and width', async () => {
102121
const sticker = new Sticker(images.animated.potrait, {
103122
type: StickerTypes.FULL

0 commit comments

Comments
 (0)