canvas-react-light 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +0 -0
- package/package.json +12 -0
- package/src/components/Border.jsx +14 -0
- package/src/components/Canvas.jsx +17 -0
- package/src/components/Tools.jsx +15 -0
- package/src/index.js +15 -0
- package/src/store/CanvasState.js +15 -0
- package/src/store/ToolState.js +15 -0
- package/src/tools/Brush.js +35 -0
- package/src/tools/Tool.js +13 -0
package/README.md
ADDED
|
File without changes
|
package/package.json
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { useEffect, useRef } from "react";
|
|
2
|
+
import CanvasState from "../store/CanvasState";
|
|
3
|
+
import toolState from "../store/ToolState";
|
|
4
|
+
import { Brush } from "../tools/Brush";
|
|
5
|
+
|
|
6
|
+
const Canvas = () => {
|
|
7
|
+
const canvasRef = useRef(null);
|
|
8
|
+
|
|
9
|
+
useEffect(() => {
|
|
10
|
+
CanvasState.setCanvas(canvasRef.current);
|
|
11
|
+
toolState.setTool(new Brush(canvasRef.current));
|
|
12
|
+
}, []);
|
|
13
|
+
|
|
14
|
+
return <canvas ref={canvasRef} width={1600} height={900} />;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
export default Canvas;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import toolState from "../store/ToolState";
|
|
2
|
+
import CanvasState from "../store/CanvasState";
|
|
3
|
+
import { Brush } from "../tools/Brush";
|
|
4
|
+
|
|
5
|
+
const Tools = () => {
|
|
6
|
+
return (
|
|
7
|
+
<div>
|
|
8
|
+
<button onClick={() => toolState.setTool(new Brush(CanvasState.canvas))}>
|
|
9
|
+
Кисть
|
|
10
|
+
</button>
|
|
11
|
+
</div>
|
|
12
|
+
);
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
export default Tools;
|
package/src/index.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import Canvas from "./components/Canvas";
|
|
2
|
+
import Tools from "./components/Tools";
|
|
3
|
+
import Border from "./components/Border";
|
|
4
|
+
import CanvasState from "./store/CanvasState";
|
|
5
|
+
import ToolState from "./store/ToolState";
|
|
6
|
+
import { Brush } from "./tools/Brush";
|
|
7
|
+
|
|
8
|
+
export {
|
|
9
|
+
Canvas,
|
|
10
|
+
Tools,
|
|
11
|
+
Border,
|
|
12
|
+
CanvasState,
|
|
13
|
+
ToolState,
|
|
14
|
+
Brush
|
|
15
|
+
};
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import Tool from "./Tool";
|
|
2
|
+
|
|
3
|
+
export class Brush extends Tool {
|
|
4
|
+
constructor(canvas) {
|
|
5
|
+
super(canvas);
|
|
6
|
+
this.listen();
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
listen() {
|
|
10
|
+
this.canvas.onmousedown = this.onMouseDownHandler.bind(this);
|
|
11
|
+
this.canvas.onmousemove = this.onMouseMoveHandler.bind(this);
|
|
12
|
+
this.canvas.onmouseup = this.onMouseUpHandler.bind(this);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
onMouseDownHandler(e) {
|
|
16
|
+
this.mouseDown = true;
|
|
17
|
+
this.ctx.beginPath();
|
|
18
|
+
this.ctx.moveTo(e.pageX - e.target.offsetLeft, e.pageY - e.target.offsetTop);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
onMouseMoveHandler(e) {
|
|
22
|
+
if (this.mouseDown) {
|
|
23
|
+
this.draw(e.pageX - e.target.offsetLeft, e.pageY - e.target.offsetTop);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
onMouseUpHandler() {
|
|
28
|
+
this.mouseDown = false;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
draw(x, y) {
|
|
32
|
+
this.ctx.lineTo(x, y);
|
|
33
|
+
this.ctx.stroke();
|
|
34
|
+
}
|
|
35
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export default class Tool {
|
|
2
|
+
constructor(canvas) {
|
|
3
|
+
this.canvas = canvas;
|
|
4
|
+
this.ctx = canvas.getContext('2d');
|
|
5
|
+
this.destroyEvents();
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
destroyEvents() {
|
|
9
|
+
this.canvas.onmousemove = null;
|
|
10
|
+
this.canvas.onmousedown = null;
|
|
11
|
+
this.canvas.onmouseup = null;
|
|
12
|
+
}
|
|
13
|
+
}
|