-
Notifications
You must be signed in to change notification settings - Fork 86
/
rect.js
108 lines (99 loc) · 2.8 KB
/
rect.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
function normalizeRect(rect) {
let x = rect.p1.x
let y = rect.p1.y
let width = rect.p2.x - x
let height = rect.p2.y - y
if (width < 0) {
x = rect.p2.x
width = -width
}
if (height < 0) {
y = rect.p2.y
height = -height
}
return {x: x, y: y, width: width, height: height}
}
class QRectCreator {
constructor(shapeType) {
this.shapeType = shapeType
this.rect = {
p1: {x: 0, y: 0},
p2: {x: 0, y: 0}
}
this.started = false
let ctrl = this
qview.onmousedown = function(event) { ctrl.onmousedown(event) }
qview.onmousemove = function(event) { ctrl.onmousemove(event) }
qview.onmouseup = function(event) { ctrl.onmouseup(event) }
qview.onkeydown = function(event) { ctrl.onkeydown(event) }
}
stop() {
qview.onmousedown = null
qview.onmousemove = null
qview.onmouseup = null
qview.onkeydown = null
}
reset() {
this.started = false
invalidate(this.rect)
}
buildShape() {
let rect = this.rect
let r = normalizeRect(rect)
switch (this.shapeType) {
case "line":
return new QLine(rect.p1, rect.p2, qview.lineStyle)
case "rect":
return new QRect(r, qview.lineStyle)
case "ellipse":
let rx = r.width / 2
let ry = r.height / 2
return new QEllipse(r.x + rx, r.y + ry, rx, ry, qview.lineStyle)
case "circle":
let rc = Math.sqrt(r.width * r.width + r.height * r.height)
return new QEllipse(rect.p1.x, rect.p1.y, rc, rc, qview.lineStyle)
default:
alert("unknown shapeType: " + this.shapeType)
return null
}
}
onmousedown(event) {
this.rect.p1 = qview.getMousePos(event)
this.started = true
}
onmousemove(event) {
if (this.started) {
this.rect.p2 = qview.getMousePos(event)
invalidate(this.rect)
}
}
onmouseup(event) {
if (this.started) {
this.rect.p2 = qview.getMousePos(event)
qview.doc.addShape(this.buildShape())
this.reset()
}
}
onkeydown(event) {
if (event.keyCode == 27) { // keyEsc
this.reset()
}
}
onpaint(ctx) {
if (this.started) {
this.buildShape().onpaint(ctx)
}
}
}
qview.registerController("LineCreator", function() {
return new QRectCreator("line")
})
qview.registerController("RectCreator", function() {
return new QRectCreator("rect")
})
qview.registerController("EllipseCreator", function() {
return new QRectCreator("ellipse")
})
qview.registerController("CircleCreator", function() {
return new QRectCreator("circle")
})