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

tracing-canvas

v1.0.14

Published

A reusable React/Next.js component that creates interactive p5.js sketches, tracing patterns based on mouse movements on a canvas.

Downloads

983

Readme

Tracing Canvas

A reusable React component that allows you to create interactive p5.js sketches. The Tracing Canvas will enable you to dynamically trace patterns based on mouse movements and click interactions and customize how the drawing behaves. This package can create interactive and visually engaging canvases for web apps running on React (Next.js integration coming soon).

Summary

The Tracing Canvas is a React component built on top of p5.js and react-p5. It provides an easy way to integrate custom sketching and drawing patterns into a React/Next.js project.

Features

  • Dynamic Drawing Patterns: The module includes prebuilt patterns such as roots, right-angle lines, and spiral branches drawn as the mouse moves.
  • Customizable Behavior: You can define custom click, draw, and drag (on mouse movement) functions
  • Customizable Drawing Parameters: You can control frame rates, stroke weights, colors, and branch lengths.
  • Overlay Support - Any other React components can be layered on top of the canvas using the children prop.
  • Responsive Canvas: The canvas resizes dynamically with the browser window.

How To Use

Installation

First, add the tracing-canvas module to your project:

npm install tracing-canvas

You also need React installed:

npm install react react-dom

Basic Usage



import React from "react";
import CanvasTrace from "tracing-canvas";

const MySketch = () => {
  return (
    <CanvasTrace/>
  );
};

export default MySketch;

Props

style (optional)

  • Type: object
  • Default: { width: '100%', height: '400px' }
  • Description: Defines the inline styles for the canvas container

options (optional)

  • Type: object
  • Description: A set of options to customize the drawing behavior. The full list of parameters is detailed below.

customDrawFunction (optional)

  • Type: function(p5, isMouseInside)
  • Description: A function to define custom drawing behavior - the behavior that affects the entire canvas on every frame (same as the p5.draw() function). It receives the p5 instance and a boolean indicating if the mouse is inside the canvas.

customClickFunction (optional)

  • Type: function(p5, isMouseInside, mouseX, mouseY)
  • Description: A function to define custom clicking behavior - the behavior that occurs after the mouse pointer is clicked. It receives the p5 instance, a boolean indicating if the mouse is inside the canvas, the x position of the mouse (relative to the canvas), and the y position of the mouse (also relative).

CustomStrokeFunction (optional)

  • Type: function(p5, isMouseInside, mouseX, mouseY)
  • Description: A function to define custom hovering behavior - the behavior that occurs after the mouse pointer is hovered or dragged. It receives the p5 instance, a boolean indicating if the mouse is inside the canvas, the x position of the mouse (relative to the canvas), and the y position of the mouse (also relative).

children (optional)

  • Type: ReactNode
  • Description: Any child elements rendered on top of the canvas.

Options

The options object allows you to fine-tune the sketch's behavior. Below is a list of supported parameters and their descriptions.

| Parameter | Type | Default | Description | |------------------------|---------------|-----------|---------------------------------------------------------------------------------------------| | customFrameRate | number | 2 | Sets the frame rate for the sketch. Frame rate is the amount of times the draw function is called per second | | incFade | number | 3 | Controls the increment fade effect on the canvas background. The higher the value the higher the fade you get per frame | | maxBranchLengthLow | number | 25 | Minimum value for the random range used for calculating the maximum branch length. | | maxBranchLengthHigh | number | 50 | Maximum value for the random range used for calculating the maximum branch length. | | minRootLength | number | 10 | Minimum possible length for the roots drawn on the canvas. | | rootCount | number | 12 | Number of root lines generated during a draw. | | branchIterations | number | 3 | Number of iterations for drawing branches off of the roots. | | branchStrokeWeight | number | 0.9 | Stroke weight used when drawing (line width). | | backgroundColor | string | #ffffff | The background color of the canvas (supports hex, RGB, or named color formats like "blue"). | | drawingColor | string | #000000 | The color of the drawing. | | rgbInc | array | [0, 0, 0] | An array that specifies the RGB increments for changing the stroke color. For each frame, the stroke color's RGB values will adjust based on rgbInc. For example, [1, 0, 0] increases the red value by 1 per frame.| | colorRange | number | 0 | The threshold for color change. When the color has changed by this amount from the original drawing color, the rgbInc direction will reverse, gradually reducing the color until it reaches drawingColor - colorRange | | mode | number | 0 | Selects between preset drawing functions: 0 for roots, 1 for right angles, 2 for spirals. | | invertedFade | boolean | false | If true, inverts the fade effect by using black instead of white for the fade color. Useful for dark backgrounds |

You can pass these options into the component as shown below:

<CanvasTrace
  options={{
    customFrameRate: 4,
    maxFade: 5,
    drawingColor: '#FF5733',
    mode: 1
  }}
/>

Demonstration

BrancingCanvas 2

Related Dependencies

  • p5.js - A library for creating interactive visuals with code.
  • react-p5 - A React wrapper for p5.js.
  • react - Peer dependency required to run the module.
  • react-dom - Peer dependency required to run the module.

Peer Dependencies

You need to ensure that the following peer dependencies are installed in your project:

  • react >= 17.0.1
  • react-dom >= 17.0.1

License

This project is licensed under the MIT License - see the LICENSE file for details.

Author

Danail Dimitrov