-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
61 lines (47 loc) · 1.19 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import './style.css'
import Konva from 'konva/lib/Core';
import { Rect } from 'konva/lib/shapes/Rect';
import { Circle } from 'konva/lib/shapes/Circle';
let shapeArray = [];
var stage = new Konva.Stage({
container: 'container',
width: window.innerWidth,
height: window.innerHeight,
});
// add canvas element
var layer = new Konva.Layer();
stage.add(layer);
let isNowDrawing = false;
// add cursor styling
stage.on('mousedown', mousedownHandler);
stage.on('mousemove', mousemoveHandler);
stage.on('mouseup', mouseupHandler);
let circle = null;
function mousedownHandler(e) {
isNowDrawing = true;
circle = new Circle({
x: stage.getPointerPosition().x,
y: stage.getPointerPosition().y,
radius: 0,
fill: '#d2d2d2',
stroke: 'black',
strokeWidth: 2,
});
// add the shape to the layer
layer.add(circle).batchDraw();
circle.on('click', function (e) {
});
}
function mousemoveHandler(e) {
if(!isNowDrawing) return;
const newRadius = stage.getPointerPosition().x - circle.x();
if(newRadius > 0) {
circle.radius(newRadius);
layer.batchDraw();
} else if(newRadius < 0) {
circle = null;
}
}
function mouseupHandler(e) {
isNowDrawing = false;
}