0.1.8 • Published 5 years ago

jw-canvas v0.1.8

Weekly downloads
1
License
MIT
Repository
github
Last release
5 years ago

jw-canvas

A react component for canvas. Mainly for handling resize events to allow auto-maintaince on pixel size and ratio to prevent stretching.

NPM version build status node version npm download

Demo

Install

NPM

Methods

MethodParametersDescription
getCanvasElementretrieve the canvas react component.
getContextget the context of the canvas.
getImageDatastartX: integer. Default: 0startY: integer. Default: 0endX: integer. Default: the canvas widthendY: integer. Default: the canvas heightget the image data of the canvas with a give area.

Props

PropDescription
maintainPixelSize(optional)whether the canvas should keep dimension from the moment it was created. Default: true
onResize(optional)event handler when the canvas is being resized.
contextType(optional)canvas context type. Default: "2d"

Usage

import React, { Component } from "react";
import ReactDOM from "react-dom";
import Canvas from "jw-canvas";

class Example extends Component {
  constructor(props) {
    super(props);

    this.resizeHandler = this.resizeHandler.bind(this);
  }

  draw() {
    const canvas = this.myCanvas;

    /** Retrieve the HTML element of the canvas. */
    const element = canvas.getCanvasElement();

    /** Get the width and height of the canvas. */
    const { width, height } = element;

    /** Get the context of the canvas. */
    const context = canvas.getContext();

    /** Get the image data of the canvas with a give area. */
    const imageData = canvas.getImageData(startX, startY, endX, endY);

    /** ... **/
  }

  resizeHandler(width, height) {
    /** ... **/
  }

  render() {
    return (
      <Canvas
        ref={myCanvas => (this.myCanvas = myCanvas)}
        onResize={this.resizeHandler}
        maintainPixelSize={false}
        contextType="2d"
      />
    );
  }
}

ReactDOM.render(<Example />, document.getElementById("root"));
0.1.8

5 years ago

0.1.7

5 years ago

0.1.6

5 years ago

0.1.5

5 years ago

0.1.4

6 years ago

0.1.3

6 years ago

0.1.1

6 years ago

0.1.0

6 years ago