canvas-react-easy 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
@@ -0,0 +1,8 @@
1
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
+ import { observer } from "mobx-react-lite";
3
+ import Tools from "./Tools";
4
+ import Canvas from "./Canvas";
5
+ const Border = observer(() => {
6
+ return (_jsxs("div", { children: [_jsx(Tools, {}), _jsx(Canvas, {})] }));
7
+ });
8
+ export default Border;
@@ -0,0 +1,16 @@
1
+ import { jsx as _jsx } from "react/jsx-runtime";
2
+ import { useEffect, useRef } from "react";
3
+ import CanvasState from "../store/CanvasState";
4
+ import toolState from "../store/ToolState";
5
+ import { Brush } from "../tools/Brush";
6
+ const Canvas = () => {
7
+ const canvasRef = useRef(null);
8
+ useEffect(() => {
9
+ if (canvasRef.current) {
10
+ CanvasState.setCanvas(canvasRef.current);
11
+ toolState.setTool(new Brush(canvasRef.current));
12
+ }
13
+ }, []);
14
+ return _jsx("canvas", { ref: canvasRef, width: 1600, height: 900 });
15
+ };
16
+ export default Canvas;
@@ -0,0 +1,12 @@
1
+ import { jsx as _jsx } from "react/jsx-runtime";
2
+ import toolState from "../store/ToolState";
3
+ import CanvasState from "../store/CanvasState";
4
+ import { Brush } from "../tools/Brush";
5
+ const Tools = () => {
6
+ return (_jsx("div", { children: _jsx("button", { onClick: () => {
7
+ if (CanvasState.canvas) {
8
+ toolState.setTool(new Brush(CanvasState.canvas));
9
+ }
10
+ }, children: "\u041A\u0438\u0441\u0442\u044C" }) }));
11
+ };
12
+ export default Tools;
package/dist/index.js ADDED
@@ -0,0 +1,7 @@
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
+ export { Canvas, Tools, Border, CanvasState, ToolState, Brush };
@@ -0,0 +1,12 @@
1
+ // CanvasState.ts
2
+ import { makeAutoObservable } from "mobx";
3
+ class CanvasState {
4
+ constructor() {
5
+ this.canvas = null;
6
+ makeAutoObservable(this);
7
+ }
8
+ setCanvas(canvas) {
9
+ this.canvas = canvas;
10
+ }
11
+ }
12
+ export default new CanvasState();
@@ -0,0 +1,12 @@
1
+ // ToolState.ts
2
+ import { makeAutoObservable } from "mobx";
3
+ class ToolState {
4
+ constructor() {
5
+ this.tool = null;
6
+ makeAutoObservable(this);
7
+ }
8
+ setTool(tool) {
9
+ this.tool = tool;
10
+ }
11
+ }
12
+ export default new ToolState();
@@ -0,0 +1,32 @@
1
+ import Tool from "./Tool";
2
+ export class Brush extends Tool {
3
+ constructor(canvas) {
4
+ super(canvas);
5
+ this.mouseDown = false;
6
+ this.listen();
7
+ }
8
+ listen() {
9
+ this.canvas.onmousedown = this.onMouseDownHandler.bind(this);
10
+ this.canvas.onmousemove = this.onMouseMoveHandler.bind(this);
11
+ this.canvas.onmouseup = this.onMouseUpHandler.bind(this);
12
+ }
13
+ onMouseDownHandler(e) {
14
+ this.mouseDown = true;
15
+ this.ctx.beginPath();
16
+ const target = e.target;
17
+ this.ctx.moveTo(e.pageX - target.offsetLeft, e.pageY - target.offsetTop);
18
+ }
19
+ onMouseMoveHandler(e) {
20
+ if (!this.mouseDown)
21
+ return;
22
+ const target = e.target;
23
+ this.draw(e.pageX - target.offsetLeft, e.pageY - target.offsetTop);
24
+ }
25
+ onMouseUpHandler() {
26
+ this.mouseDown = false;
27
+ }
28
+ draw(x, y) {
29
+ this.ctx.lineTo(x, y);
30
+ this.ctx.stroke();
31
+ }
32
+ }
@@ -0,0 +1,15 @@
1
+ export default class Tool {
2
+ constructor(canvas) {
3
+ this.canvas = canvas;
4
+ const context = canvas.getContext("2d");
5
+ if (!context)
6
+ throw new Error("Canvas context is null");
7
+ this.ctx = context;
8
+ this.destroyEvents();
9
+ }
10
+ destroyEvents() {
11
+ this.canvas.onmousemove = null;
12
+ this.canvas.onmousedown = null;
13
+ this.canvas.onmouseup = null;
14
+ }
15
+ }
@@ -0,0 +1,2 @@
1
+ declare const Border: React.MemoExoticComponent<React.ForwardRefExoticComponent<any>>;
2
+ export default Border;
@@ -0,0 +1,2 @@
1
+ declare const Canvas: () => any;
2
+ export default Canvas;
@@ -0,0 +1,2 @@
1
+ declare const Tools: () => any;
2
+ export default Tools;
@@ -0,0 +1,7 @@
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
+ export { Canvas, Tools, Border, CanvasState, ToolState, Brush };
@@ -0,0 +1,7 @@
1
+ declare class CanvasState {
2
+ canvas: HTMLCanvasElement | null;
3
+ constructor();
4
+ setCanvas(canvas: HTMLCanvasElement): void;
5
+ }
6
+ declare const _default: CanvasState;
7
+ export default _default;
@@ -0,0 +1,7 @@
1
+ declare class ToolState {
2
+ tool: Tool | null;
3
+ constructor();
4
+ setTool(tool: Tool): void;
5
+ }
6
+ declare const _default: ToolState;
7
+ export default _default;
@@ -0,0 +1,10 @@
1
+ import Tool from "./Tool";
2
+ export declare class Brush extends Tool {
3
+ private mouseDown;
4
+ constructor(canvas: HTMLCanvasElement);
5
+ listen(): void;
6
+ onMouseDownHandler(e: MouseEvent): void;
7
+ onMouseMoveHandler(e: MouseEvent): void;
8
+ onMouseUpHandler(): void;
9
+ draw(x: number, y: number): void;
10
+ }
@@ -0,0 +1,6 @@
1
+ export default class Tool {
2
+ canvas: HTMLCanvasElement;
3
+ ctx: CanvasRenderingContext2D;
4
+ constructor(canvas: HTMLCanvasElement);
5
+ destroyEvents(): void;
6
+ }
package/package.json ADDED
@@ -0,0 +1,18 @@
1
+ {
2
+ "name": "canvas-react-easy",
3
+ "version": "1.0.0",
4
+ "main": "dist/index.js",
5
+ "types": "dist/types/index.d.ts",
6
+ "license": "MIT",
7
+ "peerDependencies": {
8
+ "mobx": "^6.0.0",
9
+ "mobx-react-lite": "^3.0.0",
10
+ "react": "^18.0.0"
11
+ },
12
+ "scripts": {
13
+ "build": "tsc"
14
+ },
15
+ "devDependencies": {
16
+ "typescript": "^5.9.3"
17
+ }
18
+ }
@@ -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,19 @@
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<HTMLCanvasElement>(null);
8
+
9
+ useEffect(() => {
10
+ if (canvasRef.current) {
11
+ CanvasState.setCanvas(canvasRef.current);
12
+ toolState.setTool(new Brush(canvasRef.current));
13
+ }
14
+ }, []);
15
+
16
+ return <canvas ref={canvasRef} width={1600} height={900} />;
17
+ };
18
+
19
+ export default Canvas;
@@ -0,0 +1,21 @@
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
9
+ onClick={() => {
10
+ if (CanvasState.canvas) {
11
+ toolState.setTool(new Brush(CanvasState.canvas));
12
+ }
13
+ }}
14
+ >
15
+ Кисть
16
+ </button>
17
+ </div>
18
+ );
19
+ };
20
+
21
+ export default Tools;
package/src/index.ts ADDED
@@ -0,0 +1,10 @@
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 { Canvas, Tools, Border, CanvasState, ToolState, Brush };
9
+
10
+
@@ -0,0 +1,16 @@
1
+ // CanvasState.ts
2
+ import { makeAutoObservable } from "mobx";
3
+
4
+ class CanvasState {
5
+ canvas: HTMLCanvasElement | null = null;
6
+
7
+ constructor() {
8
+ makeAutoObservable(this);
9
+ }
10
+
11
+ setCanvas(canvas: HTMLCanvasElement) {
12
+ this.canvas = canvas;
13
+ }
14
+ }
15
+
16
+ export default new CanvasState();
@@ -0,0 +1,16 @@
1
+ // ToolState.ts
2
+ import { makeAutoObservable } from "mobx";
3
+
4
+ class ToolState {
5
+ tool: Tool | null = null;
6
+
7
+ constructor() {
8
+ makeAutoObservable(this);
9
+ }
10
+
11
+ setTool(tool: Tool) {
12
+ this.tool = tool;
13
+ }
14
+ }
15
+
16
+ export default new ToolState();
@@ -0,0 +1,40 @@
1
+ import Tool from "./Tool";
2
+
3
+ export class Brush extends Tool {
4
+ private mouseDown = false;
5
+
6
+ constructor(canvas: HTMLCanvasElement) {
7
+ super(canvas);
8
+ this.listen();
9
+ }
10
+
11
+ listen() {
12
+ this.canvas.onmousedown = this.onMouseDownHandler.bind(this);
13
+ this.canvas.onmousemove = this.onMouseMoveHandler.bind(this);
14
+ this.canvas.onmouseup = this.onMouseUpHandler.bind(this);
15
+ }
16
+
17
+ onMouseDownHandler(e: MouseEvent) {
18
+ this.mouseDown = true;
19
+ this.ctx.beginPath();
20
+
21
+ const target = e.target as HTMLCanvasElement;
22
+ this.ctx.moveTo(e.pageX - target.offsetLeft, e.pageY - target.offsetTop);
23
+ }
24
+
25
+ onMouseMoveHandler(e: MouseEvent) {
26
+ if (!this.mouseDown) return;
27
+
28
+ const target = e.target as HTMLCanvasElement;
29
+ this.draw(e.pageX - target.offsetLeft, e.pageY - target.offsetTop);
30
+ }
31
+
32
+ onMouseUpHandler() {
33
+ this.mouseDown = false;
34
+ }
35
+
36
+ draw(x: number, y: number) {
37
+ this.ctx.lineTo(x, y);
38
+ this.ctx.stroke();
39
+ }
40
+ }
@@ -0,0 +1,19 @@
1
+ export default class Tool {
2
+ canvas: HTMLCanvasElement;
3
+ ctx: CanvasRenderingContext2D;
4
+
5
+ constructor(canvas: HTMLCanvasElement) {
6
+ this.canvas = canvas;
7
+ const context = canvas.getContext("2d");
8
+ if (!context) throw new Error("Canvas context is null");
9
+ this.ctx = context;
10
+ this.destroyEvents();
11
+ }
12
+
13
+ destroyEvents() {
14
+ this.canvas.onmousemove = null;
15
+ this.canvas.onmousedown = null;
16
+ this.canvas.onmouseup = null;
17
+ }
18
+ }
19
+
package/tsconfig.json ADDED
@@ -0,0 +1,16 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "ES6",
4
+ "module": "ESNext",
5
+ "declaration": true,
6
+ "declarationDir": "dist/types",
7
+ "outDir": "dist",
8
+ "jsx": "react-jsx",
9
+ "strict": true,
10
+ "moduleResolution": "node",
11
+ "esModuleInterop": true,
12
+ "skipLibCheck": true,
13
+ "forceConsistentCasingInFileNames": true
14
+ },
15
+ "include": ["src"]
16
+ }