Skip to content

Commit adf703b

Browse files
committed
May we select rect
1 parent 5689eb6 commit adf703b

File tree

2 files changed

+54
-5
lines changed

2 files changed

+54
-5
lines changed

src/components/printer/Screen.tsx

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,59 @@
1-
import React from 'react';
1+
import React, { MouseEvent, useRef, useState } from 'react';
22

33
const Screen = () => {
4+
const canvasRef = useRef<HTMLCanvasElement>(null);
5+
6+
const [mouse, setMouse] = useState({
7+
startX: 0,
8+
startY: 0,
9+
});
10+
const [draging, setDraging] = useState(true);
11+
12+
const handleMouseDown = (ev: MouseEvent) => {
13+
setMouse({
14+
startX: ev.pageX - (canvasRef.current?.getBoundingClientRect().left || 0),
15+
startY: ev.pageY - (canvasRef.current?.getBoundingClientRect().top || 0),
16+
});
17+
18+
setDraging(true);
19+
};
20+
21+
const handleMouseMove = (ev: MouseEvent) => {
22+
if (!draging) {
23+
return;
24+
}
25+
26+
const ctx = canvasRef.current?.getContext('2d');
27+
28+
if (ctx) {
29+
const width =
30+
ev.pageX -
31+
(canvasRef.current?.getBoundingClientRect().left || 0) -
32+
mouse.startX;
33+
const height =
34+
ev.pageY -
35+
(canvasRef.current?.getBoundingClientRect().top || 0) -
36+
mouse.startY;
37+
38+
ctx.clearRect(0, 0, 1000, 1000);
39+
ctx.fillStyle = 'red';
40+
ctx.fillRect(mouse.startX, mouse.startY, width, height);
41+
}
42+
};
43+
44+
const handleMouseUp = () => {
45+
setDraging(false);
46+
};
47+
448
return (
5-
<section className="flex flex-1">
6-
<p>Hello!</p>
7-
</section>
49+
<canvas
50+
ref={canvasRef}
51+
style={{ cursor: draging ? 'crosshair' : 'default' }}
52+
className="relative w-full h-full"
53+
onMouseDown={handleMouseDown}
54+
onMouseMove={handleMouseMove}
55+
onMouseUp={handleMouseUp}
56+
/>
857
);
958
};
1059

src/main.dev.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ const createScreenWindow = () => {
181181
if (screenWindow == null) {
182182
screenWindow = new BrowserWindow({
183183
frame: false,
184-
transparent: true,
184+
// transparent: true,
185185
webPreferences: {
186186
nodeIntegration: true,
187187
},

0 commit comments

Comments
 (0)