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 ADDED
File without changes
package/package.json ADDED
@@ -0,0 +1,12 @@
1
+ {
2
+ "name": "canvas-react-light",
3
+ "version": "1.0.0",
4
+ "main": "src/index.js",
5
+ "module": "src/index.js",
6
+ "license": "MIT",
7
+ "peerDependencies": {
8
+ "mobx": "^6.15.0",
9
+ "mobx-react-lite": "^3.4.3",
10
+ "react": "^18.3.1"
11
+ }
12
+ }
@@ -0,0 +1,14 @@
1
+ import { observer } from "mobx-react-lite";
2
+ import Tools from "./Tools";
3
+ import Canvas from "./Canvas";
4
+
5
+ const Border = observer(() => {
6
+ return (
7
+ <div>
8
+ <Tools />
9
+ <Canvas />
10
+ </div>
11
+ );
12
+ });
13
+
14
+ export default Border;
@@ -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,15 @@
1
+ import { makeAutoObservable } from "mobx";
2
+
3
+ class CanvasState {
4
+ canvas = null;
5
+
6
+ constructor() {
7
+ makeAutoObservable(this);
8
+ }
9
+
10
+ setCanvas(canvas) {
11
+ this.canvas = canvas;
12
+ }
13
+ }
14
+
15
+ export default new CanvasState();
@@ -0,0 +1,15 @@
1
+ import { makeAutoObservable } from "mobx";
2
+
3
+ class ToolState {
4
+ tool = null;
5
+
6
+ constructor() {
7
+ makeAutoObservable(this);
8
+ }
9
+
10
+ setTool(tool) {
11
+ this.tool = tool;
12
+ }
13
+ }
14
+
15
+ export default new ToolState();
@@ -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
+ }