1.0.0 • Published 4 years ago

react-scrollable-canvas v1.0.0

Weekly downloads
4
License
MIT
Repository
github
Last release
4 years ago

react-scrollable-canvas

Build Status

react-scrollable-canvas is a React component library that you can create scrollable canvas easily and quickly.

Installation

Install react-scrollable-canvas with npm:

npm install react-scrollable-canvas --save

Example

demo

import React, { Component, createRef } from 'react';
import { ScrollableCanvas } from './react-scrollable-canvas';

const WIDTH = 300;
const HEIGHT = 300;
const LARGE_WIDTH = 600;
const LARGE_HEIGHT = 600;
const CIRCLE_RADIUS = 5;
const CIRCLE_SIZE = 30;

export default class ScrollableCanvasExample extends Component {
  canvasRef = createRef();
  ctx = null;

  draw = (scrollTop, scrollLeft) => {
    // draw canvas here.
    this.ctx.clearRect(0, 0, WIDTH, HEIGHT);
    for (let y = -scrollTop % CIRCLE_SIZE; y < HEIGHT - (scrollTop % CIRCLE_SIZE); y += CIRCLE_SIZE) {
      for (let x = -scrollLeft % CIRCLE_SIZE; x < WIDTH - (scrollLeft % CIRCLE_SIZE); x += CIRCLE_SIZE) {
        this.ctx.beginPath();
        this.ctx.arc(x + CIRCLE_SIZE / 2, y + CIRCLE_SIZE / 2, CIRCLE_RADIUS, 0, 360, false);
        this.ctx.fillStyle = `rgba(${(scrollLeft + x) / 2}, ${(scrollTop + y) / 2}, 128, 0.8)`;
        this.ctx.fill();
      }
    }
  };

  componentDidMount() {
    this.ctx = this.canvasRef.current.getContext('2d');
    this.draw(0, 0);
  }

  render() {
    return (
      <ScrollableCanvas
        width={WIDTH}
        height={HEIGHT}
        largeWidth={LARGE_WIDTH}
        largeHeight={LARGE_HEIGHT}
        canvasRef={this.canvasRef}
        onScroll={this.draw}
      />
    );
  }
}

Props

ScrollableCanvas Props

NameTypeDescription
widthnumberRequired. Width of the display size.
heightnumberRequired. Height of the display size.
largeWidthnumberRequired. Width of the canvas size.
largeHeightnumberRequired. Height of the canvas size.
waitnumberFunction onScroll is called every (wait) milliseconds. Used for throttle function. Default value is 10.
noScrollbarbooleanHide scroll bar. Default value is false.
onScroll(scrollTop: number, scrollLeft: number) => voidThis function is called when scrolled.
canvasRefReact.RefObjectThis references a canvas element inside ScrollableCanvas.

ScrollableCanvasContainer Props

NameTypeDescription
widthnumberRequired. Width of the display size.
heightnumberRequired. Height of the display size.
largeWidthnumberRequired. Width of the canvas size.
largeHeightnumberRequired. Height of the canvas size.
waitnumberFunction onScroll is called every (wait) milliseconds. Used for throttle function. Default value is 10.
noScrollbarbooleanHide scroll bar. Default value is false.
onScroll(scrollTop: number, scrollLeft: number) => voidThis function is called when scrolled.
childrenReact.ReactNodeChildren of ScrollableCanvasContainer.

Canvas Props

NameTypeDescription
widthnumberWidth css property. Default value is 500.
heightnumberHeight css property. Default value is 500.
translateXnumbertranslateX css property. Default value is 0.
translateYnumbertranslateY css property. Default value is 0.
topnumbertop css property. Default value is 0.
leftnumberleft css property. Default value is 0.