Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

react-konva

Package Overview
Dependencies
Maintainers
1
Versions
128
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-konva

React binding to canvas element via Konva framework

  • 18.2.10
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
264K
decreased by-16.52%
Maintainers
1
Weekly downloads
 
Created

What is react-konva?

react-konva is a React wrapper for the Konva framework, which is a 2D canvas library for creating complex graphics on the web. It allows developers to use React components to draw shapes, images, and text on the canvas, making it easier to integrate with React applications.

What are react-konva's main functionalities?

Drawing Shapes

This feature allows you to draw basic shapes like rectangles and circles on the canvas. The code sample demonstrates how to create a red rectangle and a green circle using React components.

```jsx
import React from 'react';
import { Stage, Layer, Rect, Circle } from 'react-konva';

const DrawingShapes = () => (
  <Stage width={window.innerWidth} height={window.innerHeight}>
    <Layer>
      <Rect x={20} y={20} width={100} height={100} fill='red' />
      <Circle x={200} y={200} radius={50} fill='green' />
    </Layer>
  </Stage>
);

export default DrawingShapes;
```

Handling Events

This feature allows you to handle user interactions like clicks and drags. The code sample shows how to display an alert when a blue rectangle is clicked.

```jsx
import React from 'react';
import { Stage, Layer, Rect } from 'react-konva';

const HandlingEvents = () => {
  const handleClick = () => {
    alert('Rectangle clicked!');
  };

  return (
    <Stage width={window.innerWidth} height={window.innerHeight}>
      <Layer>
        <Rect x={20} y={20} width={100} height={100} fill='blue' onClick={handleClick} />
      </Layer>
    </Stage>
  );
};

export default HandlingEvents;
```

Animating Shapes

This feature allows you to animate shapes on the canvas. The code sample demonstrates how to animate a purple rectangle moving horizontally across the screen.

```jsx
import React, { useRef, useEffect } from 'react';
import { Stage, Layer, Rect } from 'react-konva';

const AnimatingShapes = () => {
  const rectRef = useRef(null);

  useEffect(() => {
    const anim = new Konva.Animation((frame) => {
      const rect = rectRef.current;
      rect.x((rect.x() + frame.timeDiff * 0.1) % window.innerWidth);
    }, rectRef.current.getLayer());
    anim.start();

    return () => anim.stop();
  }, []);

  return (
    <Stage width={window.innerWidth} height={window.innerHeight}>
      <Layer>
        <Rect ref={rectRef} x={20} y={20} width={100} height={100} fill='purple' />
      </Layer>
    </Stage>
  );
};

export default AnimatingShapes;
```

Other packages similar to react-konva

Keywords

FAQs

Package last updated on 21 Jun 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc