Skip to content

Commit

Permalink
Add left, right and center welcome option
Browse files Browse the repository at this point in the history
  • Loading branch information
mezotv committed Feb 17, 2023
1 parent a7788de commit abd8d91
Show file tree
Hide file tree
Showing 16 changed files with 119 additions and 41 deletions.
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const Canvabase = require("./src/Canvabase");
module.exports = {
Canvas: Canvabase,
Canvabase,
AssetLoader: require('./src/AssetLoader'),
Spotify: require('./src/plugins/Spotify'),
Welcomer: require('./src/plugins/Welcomer'),
write: Canvabase.write,
Expand Down
94 changes: 77 additions & 17 deletions src/plugins/Welcomer.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ const canva = require('@napi-rs/canvas');
.setName("Dominik")
.setTitle("Welcome!")
.addBackground(["https://wallpapercave.com/wp/wp5128415.jpg", "https://wallpapercave.com/wp/wp11735586.jpg"])
.setAvatar("https://cdn.discordapp.com/avatars/347077478726238228/3b77f755fa8e66fd75d1e2d3fb8b1611.png?size=512")
.setAvatar("https://cdn.discordapp.com/avatars/347077478726238228/3b77f755fa8e66fd75d1e2d3fb8b1611.png?size=512", "center")
.setColor("#ffff")
welcomer.build().then((img) => {
canvabase.write("./test/welcomercard.png", img);
canvabase.write("./test/welcomercard.png", img);
})
})
*/

Expand Down Expand Up @@ -67,13 +69,22 @@ class Welcomer {
/**
*
* @param {String} avatar
* @param {String} position
* @returns {Welcomer}
*/

setAvatar(avatar) {
setAvatar(avatar, position) {
if (!avatar || typeof avatar !== 'string') {
throw new Error('Expected avatar string instead got ' + typeof avatar);
}

if (!position) position = 'left';

if (position !== 'left' && position !== 'right' && position !== 'center') {
throw new Error('Expected avatar position to be left, right or center');
}

this.position = position;
this.avatar = avatar;
return this;
}
Expand All @@ -92,13 +103,14 @@ class Welcomer {
return this;
}


/**
* This function builds the canvas
* @returns {Promise<Buffer>}
*/

async build() {
let { background, name, color, avatar, title } = this;
let { background, name, color, avatar, title, position } = this;

if (!background) throw new Error('No background provided in options.');
if (!avatar) throw new Error('No avatar provided in options.');
Expand Down Expand Up @@ -174,26 +186,74 @@ class Welcomer {

const username =
name.length > 15 ? name.substring(0, 15).trim() + '...' : name;

const textWidth = ctx.measureText(username).width;
const x = canvas.width / 2 - textWidth / 2 + 65;
const y = 190;
const textWidth1 = ctx.measureText(title).width;
const renderavatar = await canva.loadImage(avatar);
// switch start
switch (position) {
case "left":
const xl = canvas.width / 2 - textWidth / 2 + 65;
const yl = 190;

ctx.fillText(`${username}`, x, y);
ctx.fillText(`${username}`, xl, yl);

ctx.font = `bold 50px Life`;
ctx.fillStyle = `${color}`;
ctx.shadowBlur = 15;
ctx.font = `bold 50px Life`;
ctx.fillStyle = `${color}`;
ctx.shadowBlur = 15;

const textWidth1 = ctx.measureText(title).width;
const x1 = canvas.width / 2 - textWidth1 / 2 + 65;
const y1 = 130;
const xl1 = canvas.width / 2 - textWidth1 / 2 + 65;
const yl1 = 135;

ctx.fillText(title, x1, y1);
ctx.fillText(title, xl1, yl1);


ctx.drawImage(renderavatar, 50, 50, 170, 170);
break;

case "right":
const xr = canvas.width / 2 - textWidth / 2 - 65;
const yr = 190;

ctx.fillText(`${username}`, xr, yr);

ctx.font = `bold 25px Life`;
ctx.fillStyle = `${color}`;
ctx.shadowBlur = 15;

const xr1 = canvas.width / 2 - textWidth1 / 2 - 65;
const yr1 = 110;

ctx.fillText(title, xr1, yr1);


ctx.drawImage(renderavatar, 580, 50, 170, 170);
break;

case "center":
const xc = canvas.width / 2 - textWidth / 2;
const yc = 225;


ctx.fillText(`${username}`, xc, yc);

ctx.font = `bold 30px Life`;
ctx.fillStyle = `${color}`;
ctx.shadowBlur = 15;
ctx.textAlign = 'center';

const xc1 = canvas.width / 2;
const yc1 = 175;

ctx.fillText(title, xc1, yc1);


ctx.drawImage(renderavatar, 350, 45, 100, 100);
break;

}

const renderavatar = await canva.loadImage(avatar);

ctx.drawImage(renderavatar, 50, 50, 170, 170);
// end

function roundPfp(x, y, w, h, r) {
ctx.beginPath();
Expand Down
4 changes: 2 additions & 2 deletions test/Index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ const welcomer = new canvabase.Welcomer()
.setName("Dominik")
.setTitle("Welcome!")
.addBackground(["https://wallpapercave.com/wp/wp5128415.jpg", "https://wallpapercave.com/wp/wp11735586.jpg"])
.setAvatar("https://cdn.discordapp.com/avatars/347077478726238228/3b77f755fa8e66fd75d1e2d3fb8b1611.png?size=512")
.setColor("#f12")
.setAvatar("https://cdn.discordapp.com/avatars/347077478726238228/3b77f755fa8e66fd75d1e2d3fb8b1611.png?size=512", "center")
.setColor("#ffff")

welcomer.build().then((img) => {
canvabase.write("./test/welcomercard.png", img);
Expand Down
Binary file modified test/welcomercard.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletions typings/AssetLoader.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export = AssetLoader;
declare class AssetLoader {
constructor(...sourceDirectories: any[]);
assetDirs: string[];
load(): Promise<any>;
readDir(directory: any): Promise<any>;
}
//# sourceMappingURL=AssetLoader.d.ts.map
1 change: 1 addition & 0 deletions typings/AssetLoader.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion typings/Canvabase.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion typings/functions/fetchColor.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion typings/functions/fetchSpotifyColor.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion typings/functions/isLight.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions typings/plugins/Achievment.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
declare const _exports: any;
export = _exports;
//# sourceMappingURL=Achievment.d.ts.map
1 change: 1 addition & 0 deletions typings/plugins/Achievment.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 12 additions & 12 deletions typings/plugins/Spotify.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,24 +30,24 @@ declare class Spotify {
setArtist(artist: string): Spotify;
artist: string;
/**
*
* @param {String} albumn
* @returns {Spotify}
*/
*
* @param {String} albumn
* @returns {Spotify}
*/
setAlbum(album: any): Spotify;
album: string;
/**
*
* @param {Number} duration
* @returns {Spotify}
*/
*
* @param {Number} duration
* @returns {Spotify}
*/
setDuration(duration: number): Spotify;
duration: number;
/**
*
* @param {String} albumArt
* @returns {Spotify}
*/
*
* @param {String} albumArt
* @returns {Spotify}
*/
setCover(albumArt: string): Spotify;
albumArt: string;
/**
Expand Down
2 changes: 1 addition & 1 deletion typings/plugins/Spotify.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 8 additions & 4 deletions typings/plugins/Welcomer.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ export = Welcomer;
.setName("Dominik")
.setTitle("Welcome!")
.addBackground(["https://wallpapercave.com/wp/wp5128415.jpg", "https://wallpapercave.com/wp/wp11735586.jpg"])
.setAvatar("https://cdn.discordapp.com/avatars/347077478726238228/3b77f755fa8e66fd75d1e2d3fb8b1611.png?size=512")
.setAvatar("https://cdn.discordapp.com/avatars/347077478726238228/3b77f755fa8e66fd75d1e2d3fb8b1611.png?size=512", "center")
.setColor("#ffff")
welcomer.build().then((img) => {
canvabase.write("./test/welcomercard.png", img);
canvabase.write("./test/welcomercard.png", img);
})
})
*/
declare class Welcomer {
Expand Down Expand Up @@ -37,16 +39,18 @@ declare class Welcomer {
/**
*
* @param {String} avatar
* @param {String} position
* @returns {Welcomer}
*/
setAvatar(avatar: string): Welcomer;
setAvatar(avatar: string, position: string): Welcomer;
position: string;
avatar: string;
/**
*
* @param {String} color
* @returns {Welcomer}
*/
setcolor(color: string): Welcomer;
setColor(color: string): Welcomer;
/**
* This function builds the canvas
* @returns {Promise<Buffer>}
Expand Down
2 changes: 1 addition & 1 deletion typings/plugins/Welcomer.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit abd8d91

Please sign in to comment.