Skip to content

Commit 5fa252b

Browse files
committed
Draw success
1 parent adf703b commit 5fa252b

File tree

3 files changed

+25
-15
lines changed

3 files changed

+25
-15
lines changed

src/components/printer/Screen.tsx

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,25 @@
11
import React, { MouseEvent, useRef, useState } from 'react';
22

33
const Screen = () => {
4+
const parentRef = useRef<HTMLDivElement>(null);
45
const canvasRef = useRef<HTMLCanvasElement>(null);
56

67
const [mouse, setMouse] = useState({
78
startX: 0,
89
startY: 0,
910
});
10-
const [draging, setDraging] = useState(true);
11+
const [draging, setDraging] = useState(false);
1112

1213
const handleMouseDown = (ev: MouseEvent) => {
1314
setMouse({
1415
startX: ev.pageX - (canvasRef.current?.getBoundingClientRect().left || 0),
1516
startY: ev.pageY - (canvasRef.current?.getBoundingClientRect().top || 0),
1617
});
18+
const ctx = canvasRef.current;
19+
if (ctx) {
20+
ctx.width = window.innerWidth || 1000;
21+
ctx.height = window.innerHeight || 1000;
22+
}
1723

1824
setDraging(true);
1925
};
@@ -35,7 +41,12 @@ const Screen = () => {
3541
(canvasRef.current?.getBoundingClientRect().top || 0) -
3642
mouse.startY;
3743

38-
ctx.clearRect(0, 0, 1000, 1000);
44+
ctx.clearRect(
45+
0,
46+
0,
47+
parentRef.current?.clientWidth || 1000,
48+
parentRef.current?.clientHeight || 1000
49+
);
3950
ctx.fillStyle = 'red';
4051
ctx.fillRect(mouse.startX, mouse.startY, width, height);
4152
}
@@ -46,14 +57,18 @@ const Screen = () => {
4657
};
4758

4859
return (
49-
<canvas
50-
ref={canvasRef}
60+
<div
5161
style={{ cursor: draging ? 'crosshair' : 'default' }}
52-
className="relative w-full h-full"
53-
onMouseDown={handleMouseDown}
54-
onMouseMove={handleMouseMove}
55-
onMouseUp={handleMouseUp}
56-
/>
62+
className="w-full h-full"
63+
ref={parentRef}
64+
>
65+
<canvas
66+
ref={canvasRef}
67+
onMouseDown={handleMouseDown}
68+
onMouseMove={handleMouseMove}
69+
onMouseUp={handleMouseUp}
70+
/>
71+
</div>
5772
);
5873
};
5974

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
},

yarn.lock

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1586,11 +1586,6 @@
15861586
resolved "https://registry.npmjs.org/@types/debug/-/debug-4.1.5.tgz"
15871587
integrity sha512-Q1y515GcOdTHgagaVFhHnIFQ38ygs/kmxdNpvpou+raI9UO3YZcHDngBSYKQklcKlvA7iuQlmIKbzvmxcOE9CQ==
15881588

1589-
"@types/diff@^5.0.1":
1590-
version "5.0.1"
1591-
resolved "https://registry.npmjs.org/@types/diff/-/diff-5.0.1.tgz"
1592-
integrity sha512-XIpxU6Qdvp1ZE6Kr3yrkv1qgUab0fyf4mHYvW8N3Bx3PCsbN6or1q9/q72cv5jIFWolaGH08U9XyYoLLIykyKQ==
1593-
15941589
"@types/enzyme-adapter-react-16@^1.0.6":
15951590
version "1.0.6"
15961591
resolved "https://registry.npmjs.org/@types/enzyme-adapter-react-16/-/enzyme-adapter-react-16-1.0.6.tgz"

0 commit comments

Comments
 (0)