🚀 Socket Launch Week Day 5:Introducing Repository Access Permissions and Custom Roles.Learn more
Sign In

cherry-box

Package Overview
Dependencies
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

cherry-box

A node-canvas package filled with utilities, manage text boxes easily and more

latest
Source
npmnpm
Version
1.4.0
Version published
Weekly downloads
146
-83.43%
Maintainers
1
Weekly downloads
 
Created
Source

About

cherry-box is a Node.js package filled with utilities, which are useful for working with node-canvas.

Example usage

const cb = require("cherry-box")
const Canvas = require("canvas")
const fs = require("fs");

let canvas = Canvas.createCanvas(1000, 200);
let ctx = canvas.getContext('2d');

let text = [
    {
        text: "I like cookies!",
        color: "#ffffff",
        font: "monospace",
        modifier: "bold",
    }
]
cb.textBox(ctx, 0, 0, 1000, 200, text, 200, [1, 1]);

// Save canvas to file
let out = fs.createWriteStream("./out.png");
let stream = canvas.createPNGStream();
stream.pipe(out); 

Using modules:

import { textBox } from "cherry-box";
import { createCanvas } from "canvas";
import fs from "fs";

let canvas = Canvas.createCanvas(1000, 200);
let ctx = canvas.getContext('2d');

let text = [
    {
        text: "I like cookies!",
        color: "#ffffff",
        font: "monospace",
        modifier: "bold",
    }
]
textBox(ctx, 0, 0, 1000, 200, text, 200, [1, 1]);

// Save canvas to file
let out = fs.createWriteStream("./out.png");
let stream = canvas.createPNGStream();
stream.pipe(out); 

Using Typescript

When using textObject you also need to import TextObject

import { textObject } from "cherry-box";

...

let text: TextObject = [
    {
        text: "I like cookies!",
        color: "#ffffff",
        font: "monospace",
        modifier: "bold",
    }
]

...

Documentation

  • textBox - Align the text to specified width and height, adjust the size of the font so it fits.
  • textSchema - An easy way to specify text color, font, shadow and more into a JSON object.

TextObject

TextObject is made of multiple objects. These objects accepts the following values:

namedescriptionexampletyperequired
textText to be displayedHello worldstringtrue
fontFont of the textArialstringtrue
colorColor of the text#FFFFFFstringtrue
modifierModifier of the textboldstringfalse
shadowShadow of the textobjectfalse

Shadow schema

Shadow is a JSON object with the following values:

x and y offsets are relative to the text size. For example use x: 10, y: 10

namedescriptionexampletyperequired
colorColor of the shadow#FFFFFFstringtrue
blurBlur of the shadow5numbertrue
offsetX and Y offset of the shadow[10, 5]arraytrue

Example text schema:

[
    {
        text: "I like cookies!",
        color: "#ff8800",
        shadow: {
            offset: [10, 10], blur: 5, color: "red"
        },
        font: "Arial",
        modifier: "bold"
    }
]

textBox

textBox is an easy way to align your text, decrease font size to fit in an area and more.

textBox Schema

namedescriptionexampletyperequired
ctxCanvas contextCanvasRenderingContext2Dtrue
xX coordinate of the text box0numbertrue
yY coordinate of the text box0numbertrue
widthWidth of the text box100numbertrue
heightHeight of the text box100numbertrue
textText to be displayed in the text boxTextObjecttrue
fontSizeMaximum font size of the text100numbertrue
alignAlign of the text[1,1]arraytrue

Align values

  • Horizontal: left 0, center 1, right 2
  • Vertical: top 0, middle 1, bottom 2

Example: [1,1]

Example use of textBox in your code:

textBox(ctx, 0, 0, canvas.width, canvas.height-20, upperText, 80, [2,1]);

wrapText

wrapText is a function similar to textBox, but it doesn't just align the text. It also wraps the text to fit in the specified width.

wrapText Schema

namedescriptionexampletyperequired
ctxCanvas contextCanvasRenderingContext2Dtrue
xX coordinate of the text box0numbertrue
yY coordinate of the text box0numbertrue
widthWidth of the text box100numbertrue
textText to be displayed in the text boxTextObjecttrue
fontSizeMaximum font size of the text100numbertrue
alignAlign of the text3numbertrue

Align values

  • left 0
  • center 1
  • right 2
  • justify 3

Example use of wrapText in your code:

wrapText(ctx, 0, 0, canvas.width, text, 20, 3);

Keywords

canvas

FAQs

Package last updated on 22 Jul 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts