Git Product home page Git Product logo

reactkonvatest's Introduction

React Konva Test

Init

npm create vite@latest
# (y, react-konva-test, React, JavaScript)

git init
git remote add origin https://github.com/BenTaylor25/ReactKonvaTest.git

cd react-konva-test
npm i

npm i react-konva konva

cd ..
git add .
git commit -m "init"
git push origin master

cd react-konva-test
npm run dev

Clear defaults

Clear src/App.js to:

import './App.css'

function App() {

  return (
    <>
      <h1>React + Konva</h1>
    </>
  )
}

export default App

Clear src/App.css and src/index.css to empty.

Create Canvas component

Create new folder src/components/.
Add files src/components/Canvas.jsx and src/components/Canvas.css.

// Canvas.jsx

import './Canvas.css';

function Canvas() {
    return (
        <div id="canvas">
            <p>test</p>
        </div>
    );
}

export default Canvas;
div#canvas p {
    color: blue;
}

Add Canvas component to App.jsx

// App.jsx

import './App.css';
import Canvas from './components/Canvas';

function App() {

  return (
    <>
      <h1>React + Konva</h1>
      <Canvas />
    </>
  )
}

export default App;

Add Konva to Canvas

// Canvas.jsx

import './Canvas.css';
import { Stage, Layer, Rect } from 'react-konva';

function Canvas() {
    return (
        <Stage id="canvas" width={window.innerWidth} height={window.innerHeight}>
            <Layer>
                <Rect
                    x={20}
                    y={20}
                    width={50}
                    height={50}
                    fill={'green'}
                    />
            </Layer>
        </Stage>
    );
}

export default Canvas;
/* Canvas.css */

#canvas {
    border: 1px solid black;
}

Make Rect dragabble

// Canvas.jsx

import { useState } from 'react';
import { Stage, Layer, Rect } from 'react-konva';

import './Canvas.css';

function Canvas() {
    // Property to hold the x and y position for use in the project.
    const [rectXY, setRectXY] = useState({
        x: 0,
        y: 0
    });

    // Function to update the property values with the new position.
    const handleRectChangePos = (event) => {
        setRectXY({
            x: event.target.x(),
            y: event.target.y()
        })
    };

    return (
        <>
            {/* Write coordinates to the screen. */}
            <p>Coordinates: ({Math.round(rectXY.x)}, {Math.round(rectXY.y)})</p>
            
            <Stage id="canvas" width={window.innerWidth} height={window.innerHeight}>
                <Layer>
                    {/* `draggable` allows us to drag the rectangle around. */}
                    {/*
                        The rectXY property is not automatically updated
                        so we need to call `handleRectChangePos()`.
                    */}
                    <Rect
                        draggable
                        onDragEnd={handleRectChangePos}
                        {...rectXY}
                        width={50}
                        height={50}
                        fill={'green'}
                        />
                </Layer>
            </Stage>
        </>
    );
}

export default Canvas;

reactkonvatest's People

Contributors

bentaylor25 avatar

Watchers

 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.