Skip to content

Commit

Permalink
chore: mark version 1.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
LuckyYam committed May 2, 2022
1 parent be73d8b commit 478f06e
Show file tree
Hide file tree
Showing 17 changed files with 457 additions and 8 deletions.
Binary file added .github/assets/crush.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added .github/assets/friendship.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added .github/assets/triggered.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
77 changes: 76 additions & 1 deletion README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ yarn add @shineiichijo/canvas-chan
- Can be used for shipping people ❤️
- Can be used to make a person simp (ahm..)
- Generates image of Guess-The-Pokemon game (hidden and shown)
- And more...

# Usage

Expand Down Expand Up @@ -43,7 +44,7 @@ import { writeFile } from 'fs-extra'
const level = Math.floor(Math.random() * 100)
const ship = await new Ship(options, level, 'Amazing').build()
//now let's write the image (Buffer)
await writeFile('ship.png', ship, 'Amazing')
await writeFile('ship.png', ship)
})()
```

Expand Down Expand Up @@ -113,4 +114,78 @@ import { writeFile } from 'fs-extra'
<div align=center>
<a href="https://github.com/ShineiIchijo/Canvas-chan/blob/master/.github/assets/shownPokemon.png"><img src="https://raw.githubusercontent.com/ShineiIchijo/Canvas-chan/master/.github/assets/shownPokemon.png" alt="pokemon" border="0"></a>

</div>

## Triggered

```ts
import { Triggered } from '@shineiichijo/canvas-chan' //const { Triggered } = require('@shineiichijo/canvas-chan')
import { writeFile } from 'fs-extra'

(async () => {
const triggered = await new Triggered('https://i.pinimg.com/originals/5a/d2/0b/5ad20b2696a632543659055d62928028.jpg').build()
//now let's write the gif (Buffer)
await writeFile('triggered.gif', triggered)
})()
```

### Result

<div align=center>
<a href="https://github.com/ShineiIchijo/Canvas-chan/blob/master/.github/assets/triggered.gif"><img src="https://raw.githubusercontent.com/ShineiIchijo/Canvas-chan/master/.github/assets/triggered.gif" alt="triggered" border="0"></a>

</div>

## Crush

```ts
import { Crush } from '@shineiichijo/canvas-chan' //const { Crush } = require(''@shineiichijo/canvas-chan')
import { writeFile } from 'fs-extra'

(async () => {
const image = await new Crush('https://images-wixmp-ed30a86b8c4ca887773594c2.wixmp.com/f/bf447e2d-c744-4fb1-b4da-2630503601d2/d6kk0aq-b2032456-df83-4660-b4a4-7b7be582f5e9.png/v1/fill/w_1089,h_734,strp/tokisaki_kurumi_by_neostratos_d6kk0aq-pre.png?token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJ1cm46YXBwOjdlMGQxODg5ODIyNjQzNzNhNWYwZDQxNWVhMGQyNmUwIiwiaXNzIjoidXJuOmFwcDo3ZTBkMTg4OTgyMjY0MzczYTVmMGQ0MTVlYTBkMjZlMCIsIm9iaiI6W1t7ImhlaWdodCI6Ijw9MTM1MzciLCJwYXRoIjoiXC9mXC9iZjQ0N2UyZC1jNzQ0LTRmYjEtYjRkYS0yNjMwNTAzNjAxZDJcL2Q2a2swYXEtYjIwMzI0NTYtZGY4My00NjYwLWI0YTQtN2I3YmU1ODJmNWU5LnBuZyIsIndpZHRoIjoiPD0yMDA3MCJ9XV0sImF1ZCI6WyJ1cm46c2VydmljZTppbWFnZS5vcGVyYXRpb25zIl19.glklG5KmumFpSZbhBhu1Ow0b24ERYV3h99HMk4H_eXI')
await writeFile('crush.png', image)
})()
```

## Result

<div align=center>
<a href="https://github.com/ShineiIchijo/Canvas-chan/blob/master/.github/assets/crush.png"><img src="https://raw.githubusercontent.com/ShineiIchijo/Canvas-chan/master/.github/assets/crush.png" alt="crush" border="0"></a>

</div>

## Friendship

```ts
import { Friendship, IFriendShip } from '@shineiichijo/canvas-chan' //const { Friendship } = require('@shineiichijo/canvas-chan')
import { writeFile } from 'fs-extra'

(async () => {
const options: IFriendShip = [
{
//name of the person1
name: 'Person1',
//image of the person1 (can be a Buffer)
image: 'https://i.pinimg.com/originals/5a/d2/0b/5ad20b2696a632543659055d62928028.jpg'
},
{
//name of the person2
name: 'Person2',
//image of the person2 (can be a Buffer)
image: 'https://i.pinimg.com/originals/ea/b2/a4/eab2a46c041c81c781fff1ef0e355811.jpg'
}
]
const level = Math.floor(Math.random() * 100)
const image = await new Frienship(options, level, 'Best Friends')
//now let's write the image (Buffer)
await writeFile('frienship.png', image)
})()
```

### Result

<div align=center>
<a href="https://github.com/ShineiIchijo/Canvas-chan/blob/master/.github/assets/friendship.png"><img src="https://raw.githubusercontent.com/ShineiIchijo/Canvas-chan/master/.github/assets/friendship.png" alt="friendship" border="0"></a>

</div>
Binary file added assets/images/crush.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/images/friendship.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/images/triggered.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@shineiichijo/canvas-chan",
"version": "1.0.0",
"version": "1.0.1",
"description": "Making canvas stuffs easier",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand All @@ -26,6 +26,7 @@
"author": "Shinei Ichijo",
"devDependencies": {
"@types/axios": "^0.14.0",
"@types/gifencoder": "^2.0.1",
"@types/mocha": "^9.1.0",
"@types/node": "^17.0.17",
"@typescript-eslint/eslint-plugin": "^5.10.2",
Expand All @@ -39,6 +40,7 @@
"axios": "^0.27.2",
"canvas": "^2.9.1",
"fs-extra": "^10.1.0",
"gifencoder": "^2.0.1",
"path": "^0.12.7"
}
}
49 changes: 49 additions & 0 deletions src/Base/Crush.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { createCanvas, loadImage } from 'canvas';
import { join } from 'path';
import { Utils } from '../lib';

export class Crush {
private path = join(__dirname, '..', '..', 'assets', 'images', 'crush.png');

private utils = new Utils();

/**
* Constructs an instance of the Crush class
* @param {string | Buffer} image Image of the crush
*/

constructor(private image: string | Buffer) {}

/**
* Builds the image
* @returns {Buffer}
*/

public build = async (): Promise<Buffer> => {
if (typeof this.image !== 'string' && !Buffer.isBuffer(this.image))
throw new TypeError(
`Image should be of type string or Buffer. Recieved ${typeof this
.image}`
);
if (typeof this.image === 'string')
this.image = await this.utils.getBuffer(this.image);
const base = await loadImage(this.path);
const data = await loadImage(this.image);
const canvas = createCanvas(base.width, base.height);
const ctx = canvas.getContext('2d');
ctx.fillStyle = 'white';
ctx.fillRect(0, 0, base.width, base.height);
ctx.rotate(3.79 * (Math.PI / 180));
const { x, y, width, height } = this.utils.centerImagePart(
data,
400,
400,
79,
472
);
ctx.drawImage(data, x, y, width, height);
ctx.rotate(-3.79 * (Math.PI / 180));
ctx.drawImage(base, 0, 0);
return canvas.toBuffer();
};
}
96 changes: 96 additions & 0 deletions src/Base/Friendship.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import { createCanvas, loadImage, registerFont } from 'canvas';
import { join } from 'path';
import { Utils } from '../lib';

export class Friendship {
private paths = {
image: join(__dirname, '..', '..', 'assets', 'images', 'friendship.png'),
font: join(__dirname, '..', '..', 'assets', 'fonts', 'Pinky_Cupid.ttf'),
};

private utils: Utils = new Utils();
/**
* Constructs an instance of the Frienship class
* @param {IFriendShip[]} options Array of name and image of the people
* @param {number} percentage The level of their friencship
* @param {string} text The text to be written at the above for their friendship
*/

constructor(
private options: IFriendShip[],
private percentage: number,
private text?: string
) {}

/**
* Builds the image
* @returns {Bufffer}
*/

public build = async (): Promise<Buffer> => {
registerFont(this.paths.font, { family: 'Pinky Cupid' });
if (this.options.length <= 1)
this.options.push({
name: this.options[0].name,
image: this.options[0].image,
});
if (
(typeof this.options[0].image !== 'string' &&
!Buffer.isBuffer(this.options[0].image)) ||
(typeof this.options[1].image !== 'string' &&
!Buffer.isBuffer(this.options[1].image))
)
throw new TypeError('The image should be of type string or Buffer');
if (typeof this.options[0].image === 'string')
this.options[0].image = await this.utils.getBuffer(this.options[0].image);
if (typeof this.options[1].image === 'string')
this.options[1].image = await this.utils.getBuffer(this.options[1].image);
const image1 = await loadImage(this.options[0].image);
const image2 = await loadImage(this.options[1].image);
const base = await loadImage(this.paths.image);
const canvas = createCanvas(base.width, base.height);
const percentColors = [
{ pct: 0.0, color: { r: 0, g: 0, b: 255 } },
{ pct: 0.5, color: { r: 0, g: 255 / 2, b: 255 / 2 } },
{ pct: 1.0, color: { r: 0, g: 255, b: 0 } },
];
let text!: string;
if (this.percentage === 0 || (this.percentage > 0 && this.percentage < 10))
text = 'Awful';
else if (this.percentage >= 10 && this.percentage < 25) text = 'Very Bad';
else if (this.percentage >= 25 && this.percentage < 50) text = 'Poor';
else if (this.percentage >= 50 && this.percentage < 75) text = 'Average';
else if (this.percentage >= 75 && this.percentage < 80) text = 'Good';
else if (this.percentage >= 80 && this.percentage < 90) text = 'Great';
else if (this.percentage >= 90 && this.percentage < 95)
text = 'Best Friends';
else if (this.percentage >= 95) text = 'Soulmates';
const ctx = canvas.getContext('2d');
ctx.drawImage(image1, 70, 56, 400, 400);
ctx.drawImage(image2, 730, 56, 400, 400);
ctx.drawImage(base, 0, 0);
ctx.textAlign = 'center';
ctx.textBaseline = 'top';
ctx.fillStyle = 'green';
ctx.font = '40px Pinky Cupid';
ctx.fillText('~Friendship Meter~', 600, 15);
ctx.fillStyle = 'white';
ctx.fillText(this.options[0].name, 270, 448);
ctx.fillText(this.options[1].name, 930, 448);
ctx.font = '60px Pinky Cupid';
ctx.fillStyle = this.utils.percentColor(
this.percentage / 100,
percentColors
);
ctx.fillText(`~${this.percentage}%~`, 600, 230);
ctx.fillText(this.text ?? text, 600, 296);
ctx.font = '90px Pinky Cupid';
ctx.fillText(this.percentage > 49 ? '👍' : '👎', 600, 100);
return canvas.toBuffer();
};
}

export interface IFriendShip {
name: string;
image: string | Buffer;
}
2 changes: 1 addition & 1 deletion src/Base/GuessThePokemon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { createCanvas, loadImage } from 'canvas';
import { Utils } from '../lib/Utils';
import { join } from 'path';

export class GuessThePokemon {
export class Pokemon {
private utils: Utils = new Utils();

private paths = {
Expand Down
4 changes: 1 addition & 3 deletions src/Base/Ship.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,7 @@ export class Ship {
(typeof this.profile[1].image !== 'string' &&
!Buffer.isBuffer(this.profile[1].image))
)
throw new TypeError(
'The image should be of type string or instance of Buffer'
);
throw new TypeError('The image should be of type string or Buffer');
if (typeof this.profile[0].image === 'string')
this.profile[0].image = await this.utils.getBuffer(this.profile[0].image);
if (typeof this.profile[1].image === 'string')
Expand Down
71 changes: 71 additions & 0 deletions src/Base/Triggered.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { join } from 'path';
import GIFEncoder from 'gifencoder';
import { createCanvas, loadImage } from 'canvas';
import { Utils } from '../lib';
import { type } from 'os';

export class Triggered {
private path = join(
__dirname,
'..',
'..',
'assets',
'images',
'triggered.png'
);

private utils = new Utils();

/**
* Constructs an instance of the triggered class
* @param {string | Buffer} image Image to be triggered
*/

constructor(private image: string | Buffer) {}

/**
* Builds the triggered gif
* @returns {Buffer}
*/

public build = async (): Promise<Buffer> => {
if (typeof this.image !== 'string' && !Buffer.isBuffer(this.image))
throw new TypeError(
`Image should be of type string or Buffer. Recieved ${typeof this
.image}`
);
if (typeof this.image === 'string')
this.image = await this.utils.getBuffer(this.image);
const data = await loadImage(this.image);
const coord1 = [-25, -33, -42, -14];
const coord2 = [-25, -13, -34, -10];
const base = await loadImage(this.path);
const encoder = new GIFEncoder(base.width, base.width);
const canvas = createCanvas(base.width, base.width);
const ctx = canvas.getContext('2d');
ctx.fillStyle = 'white';
ctx.fillRect(0, 0, base.width, base.width);
const stream = encoder.createReadStream();
encoder.start();
encoder.setRepeat(0);
encoder.setDelay(50);
encoder.setQuality(200);
for (let i = 0; i < 4; i++) {
this.utils.drawImageWithTint(
ctx,
data,
'red',
coord1[i],
coord2[i],
300,
300
);
ctx.drawImage(base, 0, 218, 256, 38);
encoder.addFrame(ctx);
}
encoder.finish();
return Buffer.concat(
(await this.utils.streamToArray(stream)) as readonly Uint8Array[]
);
};
}
3 changes: 3 additions & 0 deletions src/Base/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
export * from './GuessThePokemon';
export * from './Ship';
export * from './Simp';
export * from './Triggered';
export * from './Crush';
export * from './Friendship';
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export { Ship, IShipOptions, Simp, GuessThePokemon as Pokemon } from './Base';
export * from './Base';
Loading

0 comments on commit 478f06e

Please sign in to comment.