Skip to content

Commit

Permalink
Add Star Shape
Browse files Browse the repository at this point in the history
  • Loading branch information
Helvio88 committed Feb 9, 2023
1 parent 3244ec1 commit 75babdc
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 4 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,8 @@ enum StickerTypes {
CROPPED = 'crop',
FULL = 'full',
CIRCLE = 'circle,
ROUNDED = 'rounded'
ROUNDED = 'rounded',
STAR = 'star'
}

```
Expand Down
1 change: 1 addition & 0 deletions examples/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@
await import('./full')
await import('./circle')
await import('./rounded')
await import('./star')
})().catch(console.error)
46 changes: 46 additions & 0 deletions examples/star.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { Sticker } from '../src'
;(async () => {
console.log('\n---\n')
console.log('Star example')
console.log('---\n')
const images = {
static: {
potrait: 'https://i.pinimg.com/originals/3a/53/d6/3a53d68345b56241a875595b21ec2a59.jpg',
landscape: 'https://chasinganime.com/wp-content/uploads/2021/02/0_YgtEypuJ2QfMPCbn.jpg'
},
animated: {
potrait: 'https://c.tenor.com/-1mtmQgH5eYAAAAC/watson-amelia-vtuber.gif',
landscape: 'https://c.tenor.com/2RdLoyV5VPsAAAAC/ayame-nakiri.gif'
}
}
const type = 'star'
const getOptions = (pack = '', author = '') => ({
pack,
type,
author: `${author}-${type}`
})
await (async () => {
console.log('Static Potrait')
const sticker = new Sticker(images.static.potrait, getOptions('static', 'potrait'))
await sticker.toFile()
console.log(`Saved to ${sticker.defaultFilename}`)
})()
await (async () => {
console.log('Static Landscape')
const sticker = new Sticker(images.static.landscape, getOptions('static', 'landscape'))
await sticker.toFile()
console.log(`Saved to ${sticker.defaultFilename}`)
})()
await (async () => {
console.log('Animated Potrait')
const sticker = new Sticker(images.animated.potrait, getOptions('animated', 'potrait'))
await sticker.toFile()
console.log(`Saved to ${sticker.defaultFilename}`)
})()
await (async () => {
console.log('Animated Landscape')
const sticker = new Sticker(images.animated.landscape, getOptions('animated', 'landscape'))
await sticker.toFile()
console.log(`Saved to ${sticker.defaultFilename}`)
})()
})()
3 changes: 2 additions & 1 deletion src/internal/Metadata/StickerTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ export enum StickerTypes {
CROPPED = 'crop',
FULL = 'full',
CIRCLE = 'circle',
ROUNDED = 'rounded'
ROUNDED = 'rounded',
STAR = 'star'
}
21 changes: 19 additions & 2 deletions src/internal/convert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ const convert = async (
fit: fit.cover
}).composite([
{
input: Buffer.from(`<svg><circle cx="256" cy="256" r="256" fill="${background}"/></svg>`),
input: Buffer.from(
`<svg width="512" height="512"><circle cx="256" cy="256" r="256" fill="${background}"/></svg>`
),
blend: 'dest-in',
gravity: 'northeast',
tile: true
Expand All @@ -64,7 +66,22 @@ const convert = async (
}).composite([
{
input: Buffer.from(
`<svg><rect rx="50" ry="50" width="512" height="512" fill="${background}"/></svg>`
`<svg width="512" height="512"><rect rx="50" ry="50" width="512" height="512" fill="${background}"/></svg>`
),
blend: 'dest-in',
gravity: 'northeast',
tile: true
}
])
break

case StickerTypes.STAR:
img.resize(512, 512, {
fit: fit.cover
}).composite([
{
input: Buffer.from(
`<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>`
),
blend: 'dest-in',
gravity: 'northeast',
Expand Down
19 changes: 19 additions & 0 deletions tests/Sticker.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,15 @@ describe('Sticker', () => {
const { height, width } = sizeOf(buffer)
assert.equal(height, width)
})

it('should create a star sticker same height and width', async () => {
const sticker = new Sticker(images.static.landscape, {
type: StickerTypes.STAR
})
const buffer = await sticker.build()
const { height, width } = sizeOf(buffer)
assert.equal(height, width)
})
})

describe('Animated Stickers', () => {
Expand Down Expand Up @@ -98,6 +107,16 @@ describe('Sticker', () => {
assert.equal(width, 512)
})

it('should create an animated star sticker with size 512x512', async () => {
const sticker = new Sticker(images.animated.potrait, {
type: StickerTypes.STAR
})
const buffer = await sticker.build()
const { height, width } = sizeOf(buffer)
assert.equal(height, 512)
assert.equal(width, 512)
})

it('should create an animated sticker same height and width', async () => {
const sticker = new Sticker(images.animated.potrait, {
type: StickerTypes.FULL
Expand Down

0 comments on commit 75babdc

Please sign in to comment.