npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@avolutions/canvas-painter

v0.6.0

Published

CanvasPainter.js is a simple yet powerful JavaScript library for drawing basic shapes (rectangles, circles, etc.) on HTML5 Canvas with ease. Perfect for creating 2D graphics in your web projects.

Downloads

337

Readme

About

CanvasPainter.js is a simple yet powerful JavaScript library for drawing basic shapes (rectangles, circles, etc.) on HTML5 Canvas with ease. Perfect for creating 2D graphics in your web projects.

The core idea behind CanvasPainter.js is to provide a simple way to work with shapes that are automatically rendered to the canvas. The package includes built-in shapes, such as rectangles and circles, that you can easily create and render. If a shape's properties (like position or size) change, the canvas is automatically updated without needing to manually call any context.draw methods. You can also create your own custom shapes or extending the default shapes functionality.

This approach simplifies drawing and keeps your code clean, but if you want full control or need advanced features, you can still access the standard Canvas 2D Context API through Canvas.context and use all of its methods directly.

Installation

To install the CanvasPainter.js package, run the following command via npm:

npm install @avolutions/canvas-painter

CanvasPainter.js is designed to be flexible and works with both CommonJS and ES Modules. You can include it in your project using either of these systems based on your environment.

For CommonJS:

const { Canvas } = require('@avolutions/canvas-painter');

For ES Modules:

import { Canvas } from '@avolutions/canvas-painter';

Getting started

Getting started with CanvasPainter.js is simple. After setting up an HTML <canvas> element, you can initialize it with a single method call. From there, you can start adding shapes to the canvas, which will automatically render and update as their properties change or you render shapes manually.

Initialize the canvas

First, ensure that you have a <canvas> element in your HTML with an id:

<canvas id="myCanvas"></canvas>

After you've added the canvas element to your HTML, you can initialize it using the init() method:

import { Canvas } from '@avolutions/canvas-painter';

const canvas = Canvas.init('myCanvas');

Create a shape

Next you create shapes and draw them on the canvas. You can either use the built-in shapes or create your own.

A shape is created by simple create a new object instance of it, e.g. Rectangle:

import { Rectangle } from '@avolutions/canvas-painter';

const rectangle = new Rectangle(50, 50, 25, 75); // x, y, width, height

Draw shape manually

You can manually draw the rectangle to the canvas using Canvas.draw() method. This is useful when you only want to render the canvas once or on-demand.

import { Canvas, Rectangle } from '@avolutions/canvas-painter';

const canvas = Canvas.init('myCanvas');
const rectangle = new Rectangle(50, 50, 25, 75); // x, y, width, height

canvas.draw(rectangle);

Watch shapes

A more advanced way to draw the shape is to let the canvas watch it and (re-)draws it every time the shape changes. This allows you to simply modify the shape object without caring about rerendering of the canvas. To do so, use the Canvas.watch() method.

import { Canvas, Rectangle } from '@avolutions/canvas-painter';

const canvas = Canvas.init('myCanvas');
const rectangle = new Rectangle(50, 50, 25, 75); // x, y, width, height

canvas.watch(rectangle); // this will draw the rectangle

rectangle.width = 100; // this will redraw the rectangle with the new width

Using the JavaScript Canvas API

In addition to using CanvasPainter.js built-in drawing methods, you have full access to the official Canvas 2D API through Canvas.context. This allows you to use any method from the standard Canvas/Context API directly:

import { Canvas } from '@avolutions/canvas-painter';

// Access the 2D rendering context
const canvas = Canvas.init('myCanvas');
const ctx = canvas.context;

// Use standard Canvas API methods
ctx.fillStyle = 'blue';
ctx.fillRect(50, 50, 100, 100);

ctx.strokeStyle = 'red';
ctx.lineWidth = 5;
ctx.strokeRect(150, 150, 100, 100);

// Draw text
ctx.font = '20px Arial';
ctx.fillText('Hello, Canvas!', 200, 200);

You can explore the full Canvas API documentation here.

License

This project is licensed under the MIT License. For more details, see the LICENSE file included in the repository.