{"license":"MIT","name":"react-konva","description":"React binding to canvas element via Konva framework","version":"19.0.0-0","keywords":["react","canvas","jsx","konva"],"bugs":{"url":"https://github.com/konvajs/react-konva/issues"},"main":"lib/ReactKonva.js","module":"es/ReactKonva.js","repository":{"type":"git","url":"git+ssh://git@github.com/konvajs/react-konva.git"},"dependencies":{"@types/react-reconciler":"^0.28.2","its-fine":"^1.2.5","react-reconciler":"0.31.0-beta-73bcdfbae5-20240502","scheduler":"0.25.0-beta-73bcdfbae5-20240502"},"targets":{"none":{}},"funding":[{"type":"patreon","url":"https://www.patreon.com/lavrton"},{"type":"opencollective","url":"https://opencollective.com/konva"},{"type":"github","url":"https://github.com/sponsors/lavrton"}],"peerDependencies":{"konva":"^8.0.1 || ^7.2.5 || ^9.0.0","react":">=19.0.0-beta","react-dom":">=19.0.0-beta"},"devDependencies":{"@types/chai":"^4.3.5","@types/mocha":"^10.0.1","@types/react":"18.2.8","chai":"4.3.7","konva":"^9.1.0","mocha-headless-chrome":"^4.0.0","parcel":"^2.9.1","process":"^0.11.10","react":"19.0.0-beta-73bcdfbae5-20240502","react-dom":"19.0.0-beta-73bcdfbae5-20240502","sinon":"^15.1.0","timers-browserify":"^2.0.12","typescript":"^5.1.3","use-image":"^1.1.0","util":"^0.12.5"},"scripts":{"build":"tsc -outDir ./es &&  tsc -module commonjs -outDir ./lib && cp ./ReactKonvaCore.d.ts ./lib && cp ./ReactKonvaCore.d.ts ./es","test:typings":"tsc --noEmit","preversion":"npm test","version":"npm run build","postversion":"","test":"NODE_ENV=test npm run test:build && mocha-headless-chrome -f ./test-build/unit-tests.html -a disable-web-security && npm run test:typings","test:build":"NODE_ENV=test parcel build ./test/unit-tests.html --dist-dir test-build --target none --public-url ./ --no-source-maps","test:watch":"NODE_ENV=test rm -rf ./parcel-cache && parcel serve ./test/unit-tests.html"},"typings":"react-konva.d.ts","_id":"react-konva@19.0.0-0","readme":"# React Konva\n\n[![Build Status](https://travis-ci.org/konvajs/react-konva.svg?branch=master)](https://travis-ci.org/konvajs/react-konva) [![Greenkeeper badge](https://badges.greenkeeper.io/konvajs/react-konva.svg)](https://greenkeeper.io/)\n\n![ReactKonva Logo](https://cloud.githubusercontent.com/assets/1443320/12193428/3bda2fcc-b623-11e5-8319-b1ccfc95eaec.png)\n\nReact Konva is a JavaScript library for drawing complex canvas graphics using\n[React](http://facebook.github.io/react/).\n\nIt provides declarative and reactive bindings to the\n[Konva Framework](http://konvajs.github.io/).\n\n# [OPEN DEMO](https://codesandbox.io/s/5m3nwp787x)\n\nAn attempt to make [React](http://facebook.github.io/react/) work with the HTML5\ncanvas library. The goal is to have similar declarative markup as normal React\nand to have similar data-flow model.\n\n**At the current moment, `react-konva` is not supported in React Native environment.**\n\nCurrently you can use all `Konva` components as React components and all `Konva`\nevents are supported on them in same way as normal browser events are supported.\n\n## Installation\n\n```bash\nnpm install react-konva konva --save\n```\n\n## [Tutorials and Documentation](https://konvajs.github.io/docs/react/)\n\n## Example\n\n```javascript\nimport React, { useState } from 'react';\nimport { render } from 'react-dom';\nimport { Stage, Layer, Rect, Text } from 'react-konva';\nimport Konva from 'konva';\n\nconst ColoredRect = () => {\n  const [color, setColor] = useState('green');\n\n  const handleClick = () => {\n    setColor(Konva.Util.getRandomColor());\n  };\n\n  return (\n    <Rect\n      x={20}\n      y={20}\n      width={50}\n      height={50}\n      fill={color}\n      shadowBlur={5}\n      onClick={handleClick}\n    />\n  );\n};\n\nconst App = () => {\n  return (\n    <Stage width={window.innerWidth} height={window.innerHeight}>\n      <Layer>\n        <Text text=\"Try click on rect\" />\n        <ColoredRect />\n      </Layer>\n    </Stage>\n  );\n};\n\nrender(<App />, document.getElementById('root'));\n```\n\nTo get more info about `Konva` you can read\n[Konva Overview](http://konvajs.github.io/docs/overview.html).\n\n**Actually you don't need to learn `react-konva`. Just learn `Konva` framework, you will understand how to use `react-konva`**\n\n## Core API\n\n`react-konva` supports all shapes, that `Konva` supports with the same names, and also it supports all the same events like `click`, `touchmove`, `dragend`, etc with \"on\" prefix like `onClick`, `onTouchMove`, `onDragEnd`.\n\n### Getting reference to Konva objects\n\nTo get reference of `Konva` instance of a node you can use `ref` property.\n\n```javascript\nimport React, { useEffect, useRef } from 'react';\n\nconst MyShape = () => {\n  const circleRef = useRef();\n\n  useEffect(() => {\n    // log Konva.Circle instance\n    console.log(circleRef.current);\n  }, []);\n\n  return <Circle ref={circleRef} radius={50} fill=\"black\" />;\n};\n```\n\n### Strict mode\n\nBy default `react-konva` works in \"non-strict\" mode. If you changed a property **manually** (or by user action like `drag&drop`) properties of the node will be not matched with properties from `render()`. `react-konva` updates ONLY properties changed in `render()`.\n\nIn strict mode `react-konva` will update all properties of the nodes to the values that you provided in `render()` function, no matter changed they or not.\n\nYou should decide what mode is better in your actual use case.\n\nTo enable strict mode globally you can do this:\n\n```javascript\nimport { useStrictMode } from 'react-konva';\n\nuseStrictMode(true);\n```\n\nOr you can enable it only for some components:\n\n```javascript\n<Rect width={50} height={50} fill=\"black\" _useStrictMode />\n```\n\nTake a look into this example:\n\n```javascript\nimport { Circle } from 'react-konva';\nimport Konva from 'konva';\n\nconst Shape = () => {\n  const [color, setColor] = React.useState();\n\n  return (\n    <Circle\n      x={0}\n      y={0}\n      draggable\n      radius={50}\n      fill={color}\n      onDragEnd={() => {\n        setColor(Konva.Util.getRandomColor());\n      }}\n    />\n  );\n};\n```\n\nThe circle is `draggable` and it changes its color on `dragend` event. In `strict` mode position of the node will be reset back to `{x: 0, y: 0}` (as we defined in render). But in `non-strict` mode the circle will keep its position, because `x` and `y` are not changed in render.\n\n### Minimal bundle\n\nBy default `react-konva` imports full `Konva` version. With all the shapes and all filters. To minimize bundle size you can use minimal core version of `react-konva`:\n\n```javascript\n// load minimal version of 'react-konva`\nimport { Stage, Layer, Rect } from 'react-konva/lib/ReactKonvaCore';\n\n// minimal version has NO support for core shapes and filters\n// if you want import a shape into Konva namespace you can just do this:\nimport 'konva/lib/shapes/Rect';\n```\n\nDemo: [https://codesandbox.io/s/6l97wny44z](https://codesandbox.io/s/6l97wny44z)\n\n### Usage with Next.js\n\nNote: `react-konva` is designed to work in the client-side. On the server side, it will render just empty div. So it doesn't make much sense to use react-konva for server-side rendering. In Next.js you may have issue like\n\n> Module not found: Can't resolve 'canvas'\n\nWhy do we see this error? `canvas` module is used for canvas rendering in Node.JS environment. `konva` library will use it there, but it doesn't have this dependency explicitly.\n\n#### Use dynamic loading\n\nNext.js docs: https://nextjs.org/docs/pages/building-your-application/optimizing/lazy-loading\n\nWith this approach your canvas component will be loaded on the client-side only. So you will not have any issues with server-side rendering. Also `next.js` will automatically understand that it doesn't need to load `canvas` module, because it is used for server-side rendering only.\n\n#### Step 1 - Create canvas component\n\nYou need to define your canvas components somewhere in your `components` folder.\n\n**It must be placed outside of `pages` or `app` folder (because they are used for server rendering).**\n\nYour `components/canvas.js` file may look like this:\n\n```js\nimport { Stage, Layer, Circle } from 'react-konva';\n\nfunction Canvas(props) {\n  return (\n    <Stage width={window.innerWidth} height={window.innerHeight}>\n      <Layer>\n        <Circle x={200} y={100} radius={50} fill=\"green\" />\n      </Layer>\n    </Stage>\n  );\n}\n\nexport default Canvas;\n```\n\n#### Step 2 - Use dynamic import\n\nThen you can use it in your page. Notice, it is imported to have `'use client';`.\n\n```js\n'use client';\nimport dynamic from 'next/dynamic';\n\nconst Canvas = dynamic(() => import('../components/canvas'), {\n  ssr: false,\n});\n\nexport default function Page(props) {\n  return <Canvas />;\n}\n```\n\n#### Step 3 - Setup next.config.js\n\nIn some versions of next.js you may need to set up `next.config.js` to make it work:\n\n```js\n/** @type {import('next').NextConfig} */\nconst nextConfig = {\n  webpack: (config) => {\n    config.externals = [...config.externals, { canvas: 'canvas' }]; // required to make Konva & react-konva work\n    return config;\n  },\n};\n\nmodule.exports = nextConfig;\n```\n\n### Usage with React Context\n\n**Note: this section may be not relevant, because this issue was fixed in `react-konva@18.2.2`. So context should work by default.**\n\nDue to a [known issue](https://github.com/facebook/react/issues/13336) with React, Contexts are not accessible by children of the react-konva `Stage` component. If you need to subscribe to a context from within the `Stage`, you need to \"bridge\" the context by creating a `Provider` as a child of the `Stage`. For more info, see [this discussion](https://github.com/konvajs/react-konva/issues/188#issuecomment-478302062) and this [react-redux demo](https://github.com/konvajs/react-konva/issues/311#issuecomment-454411007). Here is an example of bridging the context ([live demo](https://codesandbox.io/s/ykqw8r4r21)):\n\n```js\nimport React, { Component } from 'react';\nimport Konva from 'konva';\nimport { render } from 'react-dom';\nimport { Stage, Layer, Rect } from 'react-konva';\n\nconst ThemeContext = React.createContext('red');\n\nconst ThemedRect = () => {\n  const value = React.useContext(ThemeContext);\n  return (\n    <Rect x={20} y={50} width={100} height={100} fill={value} shadowBlur={10} />\n  );\n};\n\nconst Canvas = () => {\n  return (\n    <ThemeContext.Consumer>\n      {(value) => (\n        <Stage width={window.innerWidth} height={window.innerHeight}>\n          <ThemeContext.Provider value={value}>\n            <Layer>\n              <ThemedRect />\n            </Layer>\n          </ThemeContext.Provider>\n        </Stage>\n      )}\n    </ThemeContext.Consumer>\n  );\n};\n\nclass App extends Component {\n  render() {\n    return (\n      <ThemeContext.Provider value=\"blue\">\n        <Canvas />\n      </ThemeContext.Provider>\n    );\n  }\n}\n```\n\n## Comparisons\n\n### react-konva vs react-canvas\n\n[react-canvas](https://github.com/Flipboard/react-canvas) is a completely\ndifferent react plugin. It allows you to draw DOM-like objects (images, texts)\non canvas element in very performant way. It is NOT about drawing graphics, but\nreact-konva is exactly for drawing complex graphics on `<canvas>` element from\nReact.\n\n### react-konva vs react-art\n\n[react-art](https://github.com/reactjs/react-art) allows you to draw graphics on\na page. It also supports SVG for output. But it has no support of events of\nshapes.\n\n### react-konva vs vanilla canvas\n\nVanilla canvas is faster because when you use `react-konva` you have two layers of abstractions. Konva framework is on top of canvas and React is on top of Konva.\nDepending on the use case this approach can be slow.\nThe purpose of `react-konva` is to reduce the complexity of the application and use well-known declarative way for drawing on canvas.\n\n## [CHANGELOG](https://github.com/konvajs/react-konva/releases)\n\n**Note: you can find a lot of demos and examples of using Konva there:\n[http://konvajs.github.io/](http://konvajs.github.io/). Really, just go there and take a look what Konva can do for you. You will be able to do the same with `react-konva` too.**\n\n```\n\n```\n","readmeFilename":"README.md","gitHead":"ba195daadfd2347ae4d429570dbee6b20433cb0d","homepage":"https://github.com/konvajs/react-konva#readme","_nodeVersion":"20.8.0","_npmVersion":"10.1.0","dist":{"integrity":"sha512-Y2ppBWTzgG+5O5O+pZnGtcytlZxAlBqdDWk25/7CBQm2zIJMOJ2fRpFdA8AYwQ8wgziVzfJ06IVFCycy47qbMQ==","shasum":"b8a65b26a672f973da57c3300fb0fb90c3e3c1eb","tarball":"http://registry-npm.ywwl.com/react-konva/download/react-konva-19.0.0-0.tgz","fileCount":15,"unpackedSize":64797,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQDnddN66RtCO9iwpXzV5GqCo3ikcvLNK81vIwZke2A+OwIgLBlJSwDph5HIbzR6E/R8I1F+2FzSuhizwaYKeKIoZFQ="}],"size":12918,"noattachment":false,"key":"/react-konva/-/react-konva-19.0.0-0.tgz"},"_npmUser":{"name":"lavrton","email":"lavrton@gmail.com"},"directories":{},"maintainers":[{"name":"lavrton","email":"lavrton@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/react-konva_19.0.0-0_1716305577071_0.4655044304979712"},"_hasShrinkwrap":false,"publish_time":1716305577275,"_cnpm_publish_time":1716305577275,"dist-tags":{"beta":"19.0.0-1","latest":"19.3.2","legacy":"18.2.16","next":"19.2.0-3"}}