• Stars
    star
    116
  • Rank 303,894 (Top 6 %)
  • Language
    HTML
  • License
    MIT License
  • Created over 7 years ago
  • Updated 5 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

An ES6 utility for canvas with built-in functions and chained methods.

CanvasConstructor Logo

canvas-constructor

An ES6 chainable class for node-canvas with built-in utilities.

npm npm Total alerts GitHub license

Support Server


Installation

This module requires one of the following packages to be installed for Node.js:

Note: If you are building a website, no extra dependencies are required.


How to use it:

Node.js:

const { Canvas } = require('canvas-constructor/napi-rs');
// or `canvas-constructor/skia` if you are using `skia-canvas`
// or `canvas-constructor/cairo` if you are using `canvas`

new Canvas(300, 300)
	.setColor('#AEFD54')
	.printRectangle(5, 5, 290, 290)
	.setColor('#FFAE23')
	.setTextFont('28px Impact')
	.printText('Hello World!', 130, 150)
	.png();

Browser:

<script type="text/javascript" src="https://unpkg.com/canvas-constructor"></script>
<script type="text/javascript">
	const canvasElement = document.getElementById('canvas');
	new CanvasConstructor.Canvas(canvasElement)
		.setColor('#AEFD54')
		.printRectangle(5, 5, 290, 290)
		.setColor('#FFAE23')
		.setTextFont('28px Impact')
		.printText('Hello World!', 130, 150);
</script>

Alternatively, you can import canvas-constructor/browser.

  • That will create a canvas with size of 300 pixels width, 300 pixels height.
  • Set the color to #AEFD54
  • Draw a rectangle with the previous color, covering all the pixels from (5, 5) to (290 + 5, 290 + 5)
  • Set the color to #FFAE23
  • Set the font size to 28 pixels with font Impact.
  • Write the text 'Hello World!' in the position (130, 150)
  • Return a buffer.

Now, let's suppose we want to add images, we'll use Canvas.loadImage, which works in both Node.js and browser:

const { Canvas, loadImage } = require('canvas-constructor/napi-rs');

async function createCanvas() {
	const image = await loadImage('./images/kitten.png');

	return new Canvas(300, 400)
		.printImage(image, 0, 0, 300, 400)
		.setColor('#FFAE23')
		.setTextFont('28px Impact')
		.setTextAlign('center')
		.printText('Kitten!', 150, 370)
		.pngAsync();
}
  • That will create a canvas with size of 300 pixels width, 400 pixels height.
  • Draw an image, given a Buffer (the image from the images folder).
  • Set the color to #FFAE23
  • Set the font size to 28 pixels with font Impact.
  • Set the text alignment to center.
  • Write the text 'Kitten!' in the position (150, 370)
  • Return a buffer.

And now, you have created an image with a kitten in the background and some centred text in the bottom of it.

If you experience issues with @napi-rs/canvas, skia-canvas, or canvas, please refer to their respective package repositories, this package is just a convenient wrapper for the three.

More Repositories

1

veza

IPC/TCP Networking Utility to connect several processes with great concurrency.
TypeScript
63
star
2

Sneyra

A very powerful Music Bot based on Discord.js and Klasa
JavaScript
35
star
3

levenshtein-wasm

An experimental lightning-fast Wasm-compiled levenshtein library.
TypeScript
23
star
4

kyra.dev

My personal website
Vue
7
star
5

Skyra-i18n

Standalone bot
TypeScript
7
star
6

Posrednik

Moderation Bot based in Klasa
JavaScript
7
star
7

klasa-ts-template

A barebones TypeScript Klasa Bot Template!
TypeScript
6
star
8

canvas-constructor-server

A canvas-constructor based API for image generation and microservicing.
TypeScript
6
star
9

drakhtar

C++ Videogame made on top of SDL2
C++
6
star
10

tjslang

TranslatableJS Language, i18n and userfriendlyness first.
JavaScript
6
star
11

Ny-API

Skyra's Website Frontends
TypeScript
5
star
12

lyrch

Experimental Serenity bot
Rust
5
star
13

http-nextra

Dependency-less and lightning-fast HTTP wrapper
JavaScript
5
star
14

ipc-link

Connect multiple processes via IPC using node-ipc for backends
JavaScript
5
star
15

Sheera

Tiny Discord Bot for WoL
JavaScript
5
star
16

StonesCreed

Browser videogame built with Phaser.js and TypeScript
TypeScript
5
star
17

awesome-klasa

A curated list of amazingly awesome Klasa libraries and plugins.
5
star
18

escape-the-flying-prison

A videogame made with Unity 2019.2.1f1 using experimental HDRP to show-case AI behaviours.
C#
4
star
19

klastera

A powerful discord.js sharder using Veza and written in TypeScript
TypeScript
4
star
20

serialized-tags

Read plain objects and turn plain strings into fast i18n-ready template strings
JavaScript
3
star
21

paket-queue

A lightweight batch queue packer library written in TypeScript.
TypeScript
2
star
22

lore

2
star
23

Raspberry

TypeScript
2
star
24

PreciseTimeout

Set highly-accurate timeouts for your projects
JavaScript
2
star
25

kyranet

About me!
2
star
26

dev-guides

2
star
27

countrw.kyra.dev

A free open-source platform to track and manage counters
Vue
2
star
28

Moonlight

TypeScript
1
star
29

nas

Network-Attached Storage
Rust
1
star
30

asteroids-online

The same as Asteroids, but online.
C
1
star
31

map4

C#
1
star
32

rest

Discord rest sub-package extracted from Project Blue
TypeScript
1
star
33

ig1

Repository for IG1 (Graphical Data Processing)'s practises at UCM
C++
1
star
34

asteroids

Yet Another SDL2 C++ Videogame - UCM 2018/2019 - Videogame Development Technologies - 2nd Semester
C++
1
star
35

tfg

TypeScript
1
star
36

benchmarks

A personal benchmark suite and archive for all kinds of experiments
JavaScript
1
star
37

arkanoid-godot

An implementation of Arkanoid written with Godot 4
GDScript
1
star
38

drakhtar-website

Website for Drakhtar
Vue
1
star
39

vectoria

A web utility to inspect SVG files
TypeScript
1
star