GithubHelp home page GithubHelp logo

chq2018515 / react-sketch-canvas Goto Github PK

View Code? Open in Web Editor NEW

This project forked from vinothpandian/react-sketch-canvas

0.0 0.0 0.0 5.3 MB

React component to draw using SVG

Home Page: https://vinoth.info/react-sketch-canvas

License: MIT License

JavaScript 5.70% TypeScript 94.30%

react-sketch-canvas's Introduction

React Sketch Canvas

Freehand vector drawing tool for React using SVG as canvas 🖌


npm version

Overview

Features

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

Requirements

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 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 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
className PropTypes.string "" Class for using with CSS selectors
width PropTypes.string 100% canvas width (em/rem/px)
height PropTypes.string 100% canvas width (em/rem/px)
canvasColor PropTypes.string white canvas color (HTML colors)
background PropTypes.string '' Set SVG background using CSS background value
strokeColor PropTypes.string black Pen color
strokeWidth PropTypes.number 4 Pen stroke size
eraserWidth PropTypes.number 8 Erase size
allowOnlyPointerType PropTypes.string all allow pointer type ("all"/"mouse"/"pen"/"touch")
onUpdate PropTypes.func all Returns the current sketch path in CanvasPath type on every update
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

Example

<ReactSketchCanvas width="25em" height="10rem" />

Styling

You can pass a CSS in JS style object to style the element. By default it has a border with following properties

canvas {
  border: 0.0625rem solid #9c9c9c;
  border-radius: 0.25rem;
}

Example

<ReactSketchCanvas
  style={{
    border: "0.0625rem solid #9c9c9c",
  }}
/>

Functions

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


react-sketch-canvas's People

Contributors

vinothpandian avatar kastango avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.