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

@shawngoh87/react-sketch-canvas

v6.2.0

Published

react-sketch-canvas - Freehand vector drawing tool for React using SVG as canvas

Downloads

32

Readme

npm    NPM    npm npm bundle size    npm bundle size codecov This project was generated using DTS.

Overview

Features

  • Supports Desktop and Mobile.
  • Accepts input from Mouse, touch, and graphic tablets.

Requirements

  • Requires React >= 16.4

Wanna test React Sketch Canvas before using it?

Installation

If you use npm

npm i react-sketch-canvas

or with yarn

yarn add react-sketch-canvas

Example

Common usage example

import * as React from 'react';
import { ReactSketchCanvas } from 'react-sketch-canvas';

const styles = {
  border: '0.0625rem solid #9c9c9c',
  borderRadius: '0.25rem',
};

const Canvas = () => {
  return (
    <ReactSketchCanvas
      style={styles}
      width="600"
      height="400"
      strokeWidth={4}
      strokeColor="red"
    />
  );
};

To export Data URL of your sketch use ref

import * as React from "react";
import { ReactSketchCanvas } from "react-sketch-canvas";

const styles = {
  border: "0.0625rem solid #9c9c9c",
  borderRadius: "0.25rem"
};

const Canvas = class extends React.Component {
  constructor(props) {
    super(props);

    this.canvas = React.createRef();
  }

  render() {
    return (
      <div>
        <ReactSketchCanvas
          ref={this.canvas}
          strokeWidth={5}
          strokeColor="black"
        />
        <button
          onClick={() => {
            this.canvas.current.
              .exportImage("png")
              .then(data => {
                console.log(data);
              })
              .catch(e => {
                console.log(e);
              });
          }}
        >
          Get Image
        </button>
      </div>
    );
  }
};

List of Props

| Props | Expected datatype | Default value | Description | | ---------------------------------- | ----------------- | --------------------- | --------------------------------------------------------------------------------------------------- | | width | PropTypes.string | 100% | canvas width (em/rem/px) | | height | PropTypes.string | 100% | canvas width (em/rem/px) | | id | PropTypes.string | "react-sketch-canvas" | ID field to uniquely identify a SVG canvas (Supports multiple canvases in a single page) | | className | PropTypes.string | "" | Class for using with CSS selectors | | strokeColor | PropTypes.string | black | Pen color | | canvasColor | PropTypes.string | white | canvas color (HTML colors) | | backgroundImage | PropTypes.string | '' | Set SVG background with image URL | | exportWithBackgroundImage | PropTypes.bool | false | Keep background image on image/SVG export (on false, canvasColor will be set as background) | | preserveBackgroundImageAspectRatio | PropTypes.string | none | Set aspect ratio of the background image. For possible values check MDN docs | | strokeWidth | PropTypes.number | 4 | Pen stroke size | | eraserWidth | PropTypes.number | 8 | Erase size | | allowOnlyPointerType | PropTypes.string | all | allow pointer type ("all"/"mouse"/"pen"/"touch") | | onChange | PropTypes.func | | Returns the current sketch path in CanvasPath type on every path change | | onStroke | PropTypes.func | | Returns the the last stroke path and whether it is an eraser stroke on every pointer up event | | style | PropTypes.object | false | Add CSS styling as CSS-in-JS object | | svgStyle | PropTypes.object | {} | Add CSS styling as CSS-in-JS object for the SVG | | withTimestamp | PropTypes.bool | false | Add timestamp to individual strokes for measuring sketching time |

Set SVG background using CSS background value

You can specify width and height values in em or rem. It fills the parent element space if width and height are not set

Methods

You can export the sketch as an image or as a svg

Use ref to access the element and call the following functions to export image

| Props | Expected datatype | | ---------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------ | | eraseMode(boolean) | Switch to eraser mode by passing true. You can switch back to pen mode by passing false | | clearCanvas() | Clears the canvas. | | resetCanvas() | Resets the canvas and clears the undo/redo stack along with it. | | undo() | Undo the last action. | | redo() | Redo the previous action. | | exportImage(imageTypeString) | Accepts an image type as argument (ExportImageType) and returns a Promise which resolves to base64 data url of the sketch. | | exportSvg() | returns a Promise which resolves to an inline SVG element. | | exportPaths() | returns a Promise which resolves to an instance of CanvasPath. | | loadPaths(CanvasPath) | Accepts an CanvasPath exported from exportPaths() and loads it on the canvas. | | getSketchingTime() | returns a Promise which resolves the time that user sketched in the canvas (considers only when the user made the strokes or erased the strokes) |

Types

type ExportImageType = 'jpeg' | 'png';

interface Point {
  x: number;
  y: number;
}

interface CanvasPath {
  paths: Point[];
  strokeWidth: number;
  strokeColor: string;
  drawMode: boolean;
  startTimestamp?: number;
  endTimestamp?: number;
}

Thanks to

  • Philipp Spiess' tutorial
  • Draws smooth curves, thanks to François Romain's tutorial