How to use the canvas.registerFont function in canvas

To help you get started, we’ve selected a few canvas examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github Androz2091 / AtlantaBot / utils / fortniteShop.js View on Github external
const Canvas = require("canvas"),
fortnite = require("fortnite-9812"),
fs = require("fs"),
CronJob = require("cron").CronJob,
Discord = require("discord.js");
const { resolve } = require("path");
// Register assets fonts
Canvas.registerFont(resolve("./assets/fonts/Burbank-Big-Condensed-Bold-Font.otf"), { family: "Burbank" });

const langList = fs.readdirSync("./languages/").map((f) => f.split(".")[0]);

const applyItemName = (canvas, text) => {
    const ctx = canvas.getContext("2d");
    // Declare a base size of the font
    let fontSize = 40;
    do {
        ctx.font = `${fontSize -= 5}px Burbank`;
    } while (ctx.measureText(text).width > 260);
    return ctx.font;
};

/**
 * Delete old png files
 */
github SwitchbladeBot / switchblade / src / utils / CanvasUtils.js View on Github external
// Initiliaze fonts
    registerFont('src/assets/fonts/Comic-Sans-MS.ttf', { family: 'Comic Sans MS' })
    registerFont('src/assets/fonts/Montserrat-Thin.ttf', { family: 'Montserrat Thin' })
    registerFont('src/assets/fonts/Montserrat-ThinItalic.ttf', { family: 'Montserrat Thin', style: 'italic' })
    registerFont('src/assets/fonts/Montserrat-Light.ttf', { family: 'Montserrat Light' })
    registerFont('src/assets/fonts/Montserrat-LightItalic.ttf', { family: 'Montserrat Light', style: 'italic' })
    registerFont('src/assets/fonts/Montserrat-Regular.ttf', { family: 'Montserrat' })
    registerFont('src/assets/fonts/Montserrat-Italic.ttf', { family: 'Montserrat', style: 'italic' })
    registerFont('src/assets/fonts/Montserrat-Medium.ttf', { family: 'Montserrat Medium' })
    registerFont('src/assets/fonts/Montserrat-MediumItalic.ttf', { family: 'Montserrat Medium', style: 'italic' })
    registerFont('src/assets/fonts/Montserrat-SemiBold.ttf', { family: 'Montserrat SemiBold' })
    registerFont('src/assets/fonts/Montserrat-SemiBoldItalic.ttf', { family: 'Montserrat SemiBold', style: 'italic' })
    registerFont('src/assets/fonts/Montserrat-Bold.ttf', { family: 'Montserrat', weight: 'bold' })
    registerFont('src/assets/fonts/Montserrat-BoldItalic.ttf', { family: 'Montserrat', style: 'italic', weight: 'bold' })
    registerFont('src/assets/fonts/Montserrat-ExtraBold.ttf', { family: 'Montserrat ExtraBold' })
    registerFont('src/assets/fonts/Montserrat-ExtraBoldItalic.ttf', { family: 'Montserrat ExtraBold', style: 'italic' })
    registerFont('src/assets/fonts/Montserrat-Black.ttf', { family: 'Montserrat Black' })
    registerFont('src/assets/fonts/Montserrat-BlackItalic.ttf', { family: 'Montserrat Black', style: 'italic' })
    registerFont('src/assets/fonts/SFProDisplay-Regular.ttf', { family: 'SF Pro Display' })

    // Image loading
    Image.from = function (url, localFile = false) {
      return loadImage(url)
    }

    Image.buffer = (url, localFile = false) => localFile ? FileUtils.readFile(url) : URLtoBuffer(url)

    // Context functions
    Context2d.prototype.roundImage = function (img, x, y, w, h, r) {
      this.drawImage(this.roundImageCanvas(img, w, h, r), x, y, w, h)
      return this
    }
github SwitchbladeBot / switchblade / src / utils / CanvasUtils.js View on Github external
static initializeHelpers () {
    const self = this

    // Initiliaze fonts
    registerFont('src/assets/fonts/Comic-Sans-MS.ttf', { family: 'Comic Sans MS' })
    registerFont('src/assets/fonts/Montserrat-Thin.ttf', { family: 'Montserrat Thin' })
    registerFont('src/assets/fonts/Montserrat-ThinItalic.ttf', { family: 'Montserrat Thin', style: 'italic' })
    registerFont('src/assets/fonts/Montserrat-Light.ttf', { family: 'Montserrat Light' })
    registerFont('src/assets/fonts/Montserrat-LightItalic.ttf', { family: 'Montserrat Light', style: 'italic' })
    registerFont('src/assets/fonts/Montserrat-Regular.ttf', { family: 'Montserrat' })
    registerFont('src/assets/fonts/Montserrat-Italic.ttf', { family: 'Montserrat', style: 'italic' })
    registerFont('src/assets/fonts/Montserrat-Medium.ttf', { family: 'Montserrat Medium' })
    registerFont('src/assets/fonts/Montserrat-MediumItalic.ttf', { family: 'Montserrat Medium', style: 'italic' })
    registerFont('src/assets/fonts/Montserrat-SemiBold.ttf', { family: 'Montserrat SemiBold' })
    registerFont('src/assets/fonts/Montserrat-SemiBoldItalic.ttf', { family: 'Montserrat SemiBold', style: 'italic' })
    registerFont('src/assets/fonts/Montserrat-Bold.ttf', { family: 'Montserrat', weight: 'bold' })
    registerFont('src/assets/fonts/Montserrat-BoldItalic.ttf', { family: 'Montserrat', style: 'italic', weight: 'bold' })
    registerFont('src/assets/fonts/Montserrat-ExtraBold.ttf', { family: 'Montserrat ExtraBold' })
    registerFont('src/assets/fonts/Montserrat-ExtraBoldItalic.ttf', { family: 'Montserrat ExtraBold', style: 'italic' })
    registerFont('src/assets/fonts/Montserrat-Black.ttf', { family: 'Montserrat Black' })
    registerFont('src/assets/fonts/Montserrat-BlackItalic.ttf', { family: 'Montserrat Black', style: 'italic' })
    registerFont('src/assets/fonts/SFProDisplay-Regular.ttf', { family: 'SF Pro Display' })
github luoxuhai / generator-cover / src / index.js View on Github external
fontStyle = '74px "Source Han Serif"',
  font,
  bgImage,
  bgColor,
  fontColor = '#FFFFFF',
  savePath = __dirname,
  fileName = `${Date.now()}.jpg`,
  quality = 1,
  width = 470,
  height = 750,
  left = 90
}) => {
  bookName = bookName.trim();

  if (font && font.fontPath)
    registerFont(font.fontPath, {
      family: font.family
    });

  const fontScale = 1.2,
    canvas = createCanvas(width, height),
    ctx = canvas.getContext('2d');

  // TODO: 渐变背景
  // const grd=cxt.createLinearGradient(0, 0, width, height);
  // grd.addColorStop(0, "#FF0000");

  if (bgColor) {
    ctx.fillStyle = bgColor;
    ctx.fillRect(0, 0, width, height);
  }
github officialpiyush / image-gen-api / src / backend / generate / achievement.js View on Github external
/* Import Packages */
const {createCanvas, loadImage, registerFont} = require('canvas');
const path = require('path');
const {shortenText} = require('../util/CanvasUtil');
registerFont(path.join(__dirname, '..', 'assets', 'fonts', 'Minecraftia.ttf'), {
  family: 'Minecraftia'
});

/* Export Function */
module.exports = async function AchievementEndpoint(text) {
  if (text.length > 50) {
    return false;
  }
  const base = await loadImage(
    path.join(__dirname, '..', 'assets', 'images', 'achievement.png')
  );
  const canvas = createCanvas(base.width, base.height);
  const ctx = await canvas.getContext('2d');
  ctx.drawImage(base, 0, 0);
  ctx.font = '17px Minecraftia';
  ctx.fillStyle = '#ffff00';
github Ovilia / font2img.js / font2img.js View on Github external
if (dpr !== 1) {
        const unitIndex = fontSize.match(/\D/).index;
        const unit = fontSize.substr(unitIndex);
        fontSize = parseInt(fontSize, 10) * dpr + unit;

        if (maxWidth) {
            maxWidth = parseInt(maxWidth) * dpr;
        }
    }

    let fontObj;
    let fontName;
    if (fontPath) {
        fontObj = opentype.loadSync(fontPath);
        fontName = 'custom-' + new Date().getTime();
        registerFont(fontPath, {
            family: fontName
        });
    }
    else {
        fontName = font;
    }

    const boundingBox = getBoundingBox(fontName, fontSize, text, dpr);

    const canvasSize = getCanvasSize(text, canvasWidth, canvasHeight, boundingBox, maxWidth, lineHeight);

    const canvas = createCanvas(
        canvasSize.width * (1 + bleeding * 2),
        (canvasSize.height || boundingBox.height) * (1 + bleeding * 2)
    );
    const ctx = canvas.getContext('2d');