1

enter code hereCan we implement Object Erasing in konva js for eraser tool. Actually i need to erase free drawing line if we erase with destination-out im facing issues while moving or dragging lines from one place to another. To overcome this issue can anyone suggest me how to implement Object eraser in konva js or group the free drawn line and eraser lines.

import React from "react"
import Konva from "konva";
import {Layer, Line, Stage} from "react-konva";
import {CirclePicker} from "react-color";
const Pencil =() => {
    const [tool, setTool] = React.useState("pen");
    const [isDrawing, toggleDrawing] = React.useState(false);
    const [lines, setLines] = React.useState([]);
    const[size,setSize] = React.useState(3);
    const[color,setColor]= React.useState("red");
return(
    <div>
        <CirclePicker  value={color} color={color} onChange = {e=>{
            setColor(e.hex);
        }}/>
        <select
            value={tool}
            onChange={e => {
                setTool(e.target.value);
            }}
        >
            <option value="pen">Pen</option>
            <option value="eraser">Eraser</option>
        </select>
        <input value={size} onChange={e =>{
            setSize(parseInt(e.target.value))
            console.log(setSize)
        }} type='range' step='3' min='3' max='16'/>
    <Stage

        width={window.innerWidth}
        height={window.innerHeight}
        onMouseDown={e => {
            toggleDrawing(true);
            const pointer = e.target.getStage().getPointerPosition();
            const newLines = lines.concat({
                id: Date.now(),
                tool: tool,
                points: [pointer.x, pointer.y]
            });
            setLines(newLines);
        }}
        onMouseMove={e => {
            if (!isDrawing) {
                return;
            }
            const pointer = e.target.getStage().getPointerPosition();
            const newLines = lines.slice();
            const lastLine = {
                ...newLines[newLines.length - 1]
            };
            lastLine.size=size;
            lastLine.color=color;
            lastLine.points = lastLine.points.concat([pointer.x, pointer.y]);
            newLines[newLines.length - 1] = lastLine;
            setLines(newLines);
        }}
        onMouseUp={e => {
            toggleDrawing(false);
        }}>
    <Layer
    >
        {lines.map(line => (
            <Line
                draggable={true}
                x={window.length}
                y={window.length}
                width={window.length}
                height={window.length}
                onDragEnd={e => {
                    e.target.to({
                        // duration: 0.5,
                        // easing: Konva.Easings.ElasticEaseOut,
                        // scaleX: 1,
                        // scaleY: 1,
                        shadowOffsetX: 0,
                        shadowOffsetY: 0
                    });
                }}
                key={line.id}
                strokeWidth={line.size}
                stroke={line.color}
                points={line.points}
                globalCompositeOperation={
                    line.tool === "eraser" ? "destination-out" : "source-over"
                }
            />
        ))}
    </Layer>

    </Stage>
    </div>)



};


export default Pencil;

  • See the 'Free Drawing' example on the Konva docs site at https://konvajs.org/docs/sandbox/Free_Drawing.html. Note the small tool selector dropdown where you switch from freehand pen to freehand eraser. The important concept is the globalCompositeOperation setting. There are plain canvas examples on the web of this technique, eg https://codepen.io/mfosker/pen/ZYgoqG – Vanquished Wombat Jun 09 '20 at 11:30
  • If `destination-out` doesn't work will for you, you can just use white color. – lavrton Jun 10 '20 at 17:04
  • @lavrton thanks for your kind replay destination-out is working but when i move the free drawing lines that destination-out lines are also moving in that case there is no use of eraser – Rajashekhar Reddy Jun 11 '20 at 08:22
  • How do you move it? Can you make a demo? – lavrton Jun 11 '20 at 14:53
  • @lavrton i have added my code at sandbox and link is here https://codesandbox.io/s/crazy-lichterman-thqhk?file=/src/App.js – Rajashekhar Reddy Jun 12 '20 at 06:30
  • @lavrton this was my code and i have posted in sandbox please feel free to come here again if there are any issues in my question – Rajashekhar Reddy Jun 12 '20 at 06:31
  • @RajaShekharReddy So do I understand correctly, that you want to have ability to drag&drop a line that was erased and it must look the same even when moving? – lavrton Jun 12 '20 at 16:02
  • @lavrton i need to drag and drop or move lines and i it should look same like erased when i move lines Yes that was requirement Bro Thanks For help Bro – Rajashekhar Reddy Jun 15 '20 at 04:28
  • That may be VERY hard to implement with declarative react API. Probably it will be simpler with Konva + native canvas access. – lavrton Jun 18 '20 at 21:07
  • @lavrton Thank for your kind reply can we have demo how to do that and can we render ppt and docx files in canvas through Konva? – Rajashekhar Reddy Jun 19 '20 at 05:41
  • @lavrton"' new problem" need your help""Hey, can someone help me I have implemented zoom on free drawing its all working fine but when i zoom in and zoom out on mouse wheel in that case my size/stroke width of the pencil is not adjustable, if i zoom in more then i need to increase the pencil size to 100 or more if i zoom out more i need to set the pencil size for 0.01 like that low values, My question is if we zoom in or zoom out the pencil size should be same, i mean the pencil size should be normal like before zoom in and zoom out, can anyone help me out ..... – Rajashekhar Reddy Jun 22 '20 at 08:41
  • @lavrton i have created a demo here please check it out once free, https://codesandbox.io/s/kind-lamport-kwk5v – Rajashekhar Reddy Jun 22 '20 at 13:30
  • @lavrton my problem is zoom in and zoom out at that time pencil stroke is need to be constant i mean before zoom in or zoom out the pencil size should be same https://stackoverflow.com/questions/60680088/konva-js-free-drawing-drag-zoom-cant-draw-correctly-with-pointer-after-d as per this example also pencil size is changing on zoom in and zoom out can you please help me this out – Rajashekhar Reddy Jun 22 '20 at 13:34
  • @lavrton, Sorry I'm now out of this project – Rajashekhar Reddy Jun 11 '22 at 06:49

0 Answers0