Skip to content

blinet/canvas-helper

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Easy to edit image.

NPM version NPM downloads NPM downloads

Installation

$ npm install editor-canvas

Features

What's new!!

New function

update image function(s)

  • image width/height will not change !
  • new parameter (Canvas) to easy to use.

Description

editor canvas is a simple package help you to edit your image and text. (All example bellow is under discord, but you can use the function anywhere)

resizeText

Resize text to not go out canvas image.

const Discord = require("discord.js");
const client = new Discord.Client({
  intents: [
    Intents.FLAGS.GUILDS,
    Intents.FLAGS.GUILD_MESSAGES,
  ],
});
const Canvas = require("canvas");
const editor = require("editor-canvas");

client.on("messageCreate", async (message) => {
  var args = message.content.split(" ");
  if (args[0].toLowerCase() === "write") {
    var canvas = Canvas.createCanvas(512, 512),
      ctx = canvas.getContext("2d");

    var words = args.slice(1);
    ctx.font = editor.resizeText(ctx, { text: words });
    ctx.fillText(words, 100, 0);

    message.channel.send({ files: [canvas.toBuffer()] });
  }
});

splitText

Split text to not go out canvas image.

const Discord = require("discord.js");
const client = new Discord.Client({
  intents: [
    Intents.FLAGS.GUILDS,
    Intents.FLAGS.GUILD_MESSAGES,
  ],
});
const Canvas = require("canvas");
const editor = require("editor-canvas");

client.on("messageCreate", async (message) => {
  var args = message.content.split(" ");
  if (args[0].toLowerCase() === "write") {
    var canvas = Canvas.createCanvas(512, 512),
      ctx = canvas.getContext("2d");

    var words = args.slice(1);
    ctx.font = "20px ";
    words = editor.splitText(ctx, { text: words });
    ctx.fillText(words, 100, 0);

    message.channel.send({ files: [canvas.toBuffer()] });
  }
});

drawCenter

draw image in a specify center point

const Discord = require("discord.js");
const client = new Discord.Client({
  intents: [
    Intents.FLAGS.GUILDS,
    Intents.FLAGS.GUILD_MESSAGES,
  ],
});
const Canvas = require("canvas");
const editor = require("editor-canvas");

client.on("messageCreate", async (message) => {
  var args = message.content.split(" ");
  if (args[0].toLowerCase() === "center") {
    var canvas = Canvas.createCanvas(512, 512),
      ctx = canvas.getContext("2d");

    var avatar = message.author.displayAvatarURL({
      dynamic: false,
      format: "jpg",
      size: 1024,
    });

    avatar = await editor.drawCircle({ image: avatar, Canvas });
    editor.drawCenter(ctx, avatar, 200, 200, 100, 100);

    message.channel.send({ files: [canvas.toBuffer()] });
  }
});

drawCircle

from an image to circle.

const Discord = require("discord.js");
const client = new Discord.Client({
  intents: [
    Intents.FLAGS.GUILDS,
    Intents.FLAGS.GUILD_MESSAGES,
  ],
});
const editor = require("editor-canvas");

client.on("messageCreate", async (message) => {
  if (message.content === "circle") {
    var avatar = message.author.displayAvatarURL({
      dynamic: false,
      format: "jpg",
      size: 2048,
    });

    avatar = await editor.drawCircle({ image: avatar });

    message.channel.send({ files: [avatar] });
  }
});

drawSquare

Curve the edge for image

const Discord = require("discord.js");
const client = new Discord.Client({
  intents: [
    Intents.FLAGS.GUILDS,
    Intents.FLAGS.GUILD_MESSAGES,
  ],
});
const editor = require("editor-canvas");

client.on("messageCreate", async (message) => {
  if (message.content === "curve") {
    var avatar = message.author.displayAvatarURL({
      dynamic: false,
      format: "jpg",
      size: 1024,
    });

    avatar = await editor.drawSquare({ image: avatar });

    message.channel.send({ files: [avatar] });
  }
});

drawPolygon

Draw any polygon with simple step

const Discord = require("discord.js");
const client = new Discord.Client({
  intents: [
    Intents.FLAGS.GUILDS,
    Intents.FLAGS.GUILD_MESSAGES,
  ],
});
const editor = require("editor-canvas");

client.on("messageCreate", async (message) => {
  if (message.content === "polygon") {
    var avatar = message.author.displayAvatarURL({
      dynamic: false,
      format: "jpg",
      size: 1024,
    });

    avatar = await editor.drawPolygon({ image: avatar, angle: 10 });

    message.channel.send({ files: [avatar] });
  }
});

resizeImage

Resize your image with specific width & height

const Discord = require("discord.js");
const client = new Discord.Client({
  intents: [
    Intents.FLAGS.GUILDS,
    Intents.FLAGS.GUILD_MESSAGES,
  ],
});
const editor = require("editor-canvas");

client.on("messageCreate", async (message) => {
  var [cmd, width, height] = message.content.trim().split(/ +/);
  if (cmd === "resize") {
    var avatar = message.author.displayAvatarURL({
      dynamic: false,
      format: "jpg",
      size: 1024,
    });

    avatar = await editor.resizeImage({
      image: avatar,
      width: width,
      height: height,
    });

    message.channel.send({ files: [avatar] });
  }
});

New

cropImage

crop your image with specific coordinates

const Discord = require("discord.js");
const client = new Discord.Client({
  intents: [
    Intents.FLAGS.GUILDS,
    Intents.FLAGS.GUILD_MESSAGES,
  ],
});
const editor = require("editor-canvas");

client.on("messageCreate", async (message) => {
  var [cmd, x, y, width, height] = message.content.trim().split(/ +/);
  if (cmd === "crop") {
    var avatar = message.author.displayAvatarURL({
      dynamic: false,
      format: "jpg",
      size: 1024,
    });

    avatar = await editor.cropImage({
      image: avatar,
      x: x,
      y: y,
      width: width,
      height: height,
    });

    message.channel.send({ files: [avatar] });
  }
});

Other

functions and its options.

resizeText

 (ctx, { text, width, font })

text // specific text. (required)

width // when text go out the spefic width  will resize. (optional)
// width: 200

font // text font to start with it. (optional)
// font: 20

splitText

 (ctx, { text, width, maxLine })

text // specific text. (required)

width // when text go out the spefic width  will resize. (optional)
// width: 200

maxLine // max line reached when text is big. (optional)
// maxLine: 2

drawCircle

 ({ image ,fill, stroke, weight, Canvas })

image // specific image. (optional)

fill // if don't want image , u can draw circle with specific color. (optional)
// fill: "BLACK"

stroke // draw a fram among image or circle, with specific color. (optional)
// stroke: "BLACK"

weight // fram width. (optional)
//weight: 5

Canvas // you can use the image in your canvas code without use "loadImage"
// else will be a Buffer image

drawSquare

 ({ image, fill, stroke, weight, curve, Canvas })

image // specific image. (optional)

fill // if don't want image , u can draw circle with specific color. (optional)
// fill: "BLACK"

stroke // draw a fram among image or circle, with specific color. (optional)
// stroke: "BLACK"

weight // fram width. (optional)
//weight: 5

curve // curve the edge. (optional)
//curve: 30

Canvas // you can use the image in your canvas code without use "loadImage"
// else will be a Buffer image

drawPolygon

 ({ image, fill, stroke, weight, angle })

image // specific image. (optional)

fill // if don't want image , u can draw circle with specific color. (optional)
// fill: "BLACK"

stroke // draw a fram among image or circle, with specific color. (optional)
// stroke: "BLACK"

weight // fram width. (optional)
//weight: 5

angle // count of polygon angle. (optional)
//angle: 10

Canvas // you can use the image in your canvas code without use "loadImage"
// else will be a Buffer image

resizeImage

 ({ image, width, height, Canvas })

image // specific image. (required)

width // the new width for image. (optional)

height // the new height for image. (optional)

Canvas // you can use the image in your canvas code without use "loadImage"
// else will be a Buffer image

cropImage

 ({ image, x, y, width, height, Canvas })

image // specific image. (required)

x // to crop from top

y // to crop from left

width // the width to crop

height // the height to crop

Canvas // you can use the image in your canvas code without use "loadImage"
// else will be a Buffer image

Thanks for using it ❤️. Support.