canvas-react-easy 1.0.3 → 1.0.5

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.
@@ -1,12 +1,17 @@
1
- import { jsx as _jsx } from "react/jsx-runtime";
1
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
2
  import toolState from "../store/ToolState";
3
3
  import CanvasState from "../store/CanvasState";
4
4
  import { Brush } from "../tools/Brush";
5
+ import { Rectangle } from "../tools/Rectangle";
6
+ import { Circle } from "../tools/Circle";
7
+ import { TextTool } from "../tools/Text";
8
+ import { ImageTool } from "../tools/ImageTool";
5
9
  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" }) }));
10
+ const selectBrush = () => CanvasState.canvas && toolState.setTool(new Brush(CanvasState.canvas));
11
+ const selectRectangle = () => CanvasState.canvas && toolState.setTool(new Rectangle(CanvasState.canvas));
12
+ const selectCircle = () => CanvasState.canvas && toolState.setTool(new Circle(CanvasState.canvas));
13
+ const selectText = () => CanvasState.canvas && toolState.setTool(new TextTool(CanvasState.canvas));
14
+ const selectImage = () => CanvasState.canvas && toolState.setTool(new ImageTool(CanvasState.canvas));
15
+ return (_jsxs("div", { style: { marginBottom: "10px" }, children: [_jsx("button", { onClick: selectBrush, children: "\u041A\u0438\u0441\u0442\u044C" }), _jsx("button", { onClick: selectRectangle, children: "\u041F\u0440\u044F\u043C\u043E\u0443\u0433\u043E\u043B\u044C\u043D\u0438\u043A" }), _jsx("button", { onClick: selectCircle, children: "\u041A\u0440\u0443\u0433" }), _jsx("button", { onClick: selectText, children: "\u0422\u0435\u043A\u0441\u0442" }), _jsx("button", { onClick: selectImage, children: "\u0418\u0437\u043E\u0431\u0440\u0430\u0436\u0435\u043D\u0438\u0435" })] }));
11
16
  };
12
17
  export default Tools;
@@ -0,0 +1,32 @@
1
+ import Tool from "./Tool";
2
+ export class Circle extends Tool {
3
+ constructor(canvas) {
4
+ super(canvas);
5
+ this.startX = 0;
6
+ this.startY = 0;
7
+ this.mouseDown = false;
8
+ this.listen();
9
+ }
10
+ listen() {
11
+ this.canvas.onmousedown = this.onMouseDownHandler.bind(this);
12
+ this.canvas.onmouseup = this.onMouseUpHandler.bind(this);
13
+ }
14
+ onMouseDownHandler(e) {
15
+ this.mouseDown = true;
16
+ const target = e.target;
17
+ this.startX = e.pageX - target.offsetLeft;
18
+ this.startY = e.pageY - target.offsetTop;
19
+ }
20
+ onMouseUpHandler(e) {
21
+ if (!this.mouseDown)
22
+ return;
23
+ this.mouseDown = false;
24
+ const target = e.target;
25
+ const endX = e.pageX - target.offsetLeft;
26
+ const endY = e.pageY - target.offsetTop;
27
+ const radius = Math.sqrt(Math.pow(endX - this.startX, 2) + Math.pow(endY - this.startY, 2));
28
+ this.ctx.beginPath();
29
+ this.ctx.arc(this.startX, this.startY, radius, 0, 2 * Math.PI);
30
+ this.ctx.stroke();
31
+ }
32
+ }
@@ -0,0 +1,34 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ import Tool from "./Tool";
11
+ export class ImageTool extends Tool {
12
+ constructor(canvas) {
13
+ super(canvas);
14
+ this.listen();
15
+ }
16
+ listen() {
17
+ this.canvas.onclick = this.onClickHandler.bind(this);
18
+ }
19
+ onClickHandler(e) {
20
+ return __awaiter(this, void 0, void 0, function* () {
21
+ const target = e.target;
22
+ const x = e.pageX - target.offsetLeft;
23
+ const y = e.pageY - target.offsetTop;
24
+ const url = prompt("Введите URL изображения");
25
+ if (!url)
26
+ return;
27
+ const img = new Image();
28
+ img.src = url;
29
+ img.onload = () => {
30
+ this.ctx.drawImage(img, x, y, img.width, img.height);
31
+ };
32
+ });
33
+ }
34
+ }
@@ -0,0 +1,33 @@
1
+ import Tool from "./Tool";
2
+ export class Rectangle extends Tool {
3
+ constructor(canvas) {
4
+ super(canvas);
5
+ this.startX = 0;
6
+ this.startY = 0;
7
+ this.mouseDown = false;
8
+ this.listen();
9
+ }
10
+ listen() {
11
+ this.canvas.onmousedown = this.onMouseDownHandler.bind(this);
12
+ this.canvas.onmouseup = this.onMouseUpHandler.bind(this);
13
+ }
14
+ onMouseDownHandler(e) {
15
+ this.mouseDown = true;
16
+ const target = e.target;
17
+ this.startX = e.pageX - target.offsetLeft;
18
+ this.startY = e.pageY - target.offsetTop;
19
+ }
20
+ onMouseUpHandler(e) {
21
+ if (!this.mouseDown)
22
+ return;
23
+ this.mouseDown = false;
24
+ const target = e.target;
25
+ const endX = e.pageX - target.offsetLeft;
26
+ const endY = e.pageY - target.offsetTop;
27
+ const width = endX - this.startX;
28
+ const height = endY - this.startY;
29
+ this.ctx.beginPath();
30
+ this.ctx.rect(this.startX, this.startY, width, height);
31
+ this.ctx.stroke();
32
+ }
33
+ }
@@ -0,0 +1,21 @@
1
+ import Tool from "./Tool";
2
+ export class TextTool extends Tool {
3
+ constructor(canvas) {
4
+ super(canvas);
5
+ this.listen();
6
+ }
7
+ listen() {
8
+ this.canvas.onclick = this.onClickHandler.bind(this);
9
+ }
10
+ onClickHandler(e) {
11
+ const target = e.target;
12
+ const x = e.pageX - target.offsetLeft;
13
+ const y = e.pageY - target.offsetTop;
14
+ const text = prompt("Введите текст");
15
+ if (!text)
16
+ return;
17
+ this.ctx.font = "20px Arial";
18
+ this.ctx.fillStyle = "black";
19
+ this.ctx.fillText(text, x, y);
20
+ }
21
+ }
@@ -1,2 +1,4 @@
1
- declare const Border: React.MemoExoticComponent<React.ForwardRefExoticComponent<any>>;
1
+ declare const Border: (() => import("react/jsx-runtime").JSX.Element) & {
2
+ displayName: string;
3
+ };
2
4
  export default Border;
@@ -1,2 +1,2 @@
1
- declare const Canvas: () => any;
1
+ declare const Canvas: () => import("react/jsx-runtime").JSX.Element;
2
2
  export default Canvas;
@@ -1,2 +1,2 @@
1
- declare const Tools: () => any;
1
+ declare const Tools: () => import("react/jsx-runtime").JSX.Element;
2
2
  export default Tools;
@@ -1,3 +1,4 @@
1
+ import Tool from "../tools/Tool";
1
2
  declare class ToolState {
2
3
  tool: Tool | null;
3
4
  constructor();
@@ -0,0 +1,10 @@
1
+ import Tool from "./Tool";
2
+ export declare class Circle extends Tool {
3
+ private startX;
4
+ private startY;
5
+ private mouseDown;
6
+ constructor(canvas: HTMLCanvasElement);
7
+ listen(): void;
8
+ onMouseDownHandler(e: MouseEvent): void;
9
+ onMouseUpHandler(e: MouseEvent): void;
10
+ }
@@ -0,0 +1,6 @@
1
+ import Tool from "./Tool";
2
+ export declare class ImageTool extends Tool {
3
+ constructor(canvas: HTMLCanvasElement);
4
+ listen(): void;
5
+ onClickHandler(e: MouseEvent): Promise<void>;
6
+ }
@@ -0,0 +1,10 @@
1
+ import Tool from "./Tool";
2
+ export declare class Rectangle extends Tool {
3
+ private startX;
4
+ private startY;
5
+ private mouseDown;
6
+ constructor(canvas: HTMLCanvasElement);
7
+ listen(): void;
8
+ onMouseDownHandler(e: MouseEvent): void;
9
+ onMouseUpHandler(e: MouseEvent): void;
10
+ }
@@ -0,0 +1,6 @@
1
+ import Tool from "./Tool";
2
+ export declare class TextTool extends Tool {
3
+ constructor(canvas: HTMLCanvasElement);
4
+ listen(): void;
5
+ onClickHandler(e: MouseEvent): void;
6
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "canvas-react-easy",
3
- "version": "1.0.3",
3
+ "version": "1.0.5",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/types/index.d.ts",
6
6
  "license": "MIT",
@@ -1,5 +1,6 @@
1
1
  // ToolState.ts
2
2
  import { makeAutoObservable } from "mobx";
3
+ import Tool from "../tools/Tool"; // путь к Tool.ts
3
4
 
4
5
  class ToolState {
5
6
  tool: Tool | null = null;