1
- import { Quadrilateral } from "dynamsoft-barcode-reader-bundle" ;
1
+ import { Point , Quadrilateral } from "dynamsoft-barcode-reader-bundle" ;
2
2
3
3
export class OverlayManager {
4
4
overlay : HTMLCanvasElement | undefined ;
5
5
context : CanvasRenderingContext2D | undefined ;
6
+ globalPoints : Point [ ] | undefined ;
6
7
7
8
initOverlay ( overlay : HTMLCanvasElement ) : void {
8
9
this . overlay = overlay ;
@@ -28,6 +29,7 @@ export class OverlayManager {
28
29
drawOverlay ( localization : Quadrilateral , text : any ) : void {
29
30
if ( this . context ) {
30
31
let points = localization . points ;
32
+ this . globalPoints = points ;
31
33
this . context . beginPath ( ) ;
32
34
this . context . moveTo ( points [ 0 ] . x , points [ 0 ] . y ) ;
33
35
this . context . lineTo ( points [ 1 ] . x , points [ 1 ] . y ) ;
@@ -62,4 +64,94 @@ export class OverlayManager {
62
64
this . context . fillText ( text , left , top + 50 ) ;
63
65
}
64
66
}
67
+
68
+ setPoints ( points : Point [ ] ) : void {
69
+ this . globalPoints = points ;
70
+ this . overlay ! . addEventListener ( "mousedown" , ( event ) => this . updatePoint ( event , this . context ! , this . overlay ! ) ) ;
71
+ this . overlay ! . addEventListener ( "touchstart" , ( event ) => this . updatePoint ( event , this . context ! , this . overlay ! ) ) ;
72
+ this . drawQuad ( this . context ! , this . overlay ! ) ;
73
+ }
74
+
75
+ updatePoint ( e : MouseEvent | TouchEvent , context : CanvasRenderingContext2D , canvas : HTMLCanvasElement ) : void {
76
+ if ( ! this . globalPoints ) {
77
+ return ;
78
+ }
79
+ let globalPoints = this . globalPoints ;
80
+ function getCoordinates ( e : MouseEvent | TouchEvent ) : Point {
81
+ let rect = canvas . getBoundingClientRect ( ) ;
82
+
83
+ let scaleX = canvas . clientWidth / canvas . width ;
84
+ let scaleY = canvas . clientHeight / canvas . height ;
85
+
86
+ let mouseX = ( e instanceof MouseEvent ? e . clientX : e . touches [ 0 ] . clientX ) ;
87
+ let mouseY = ( e instanceof MouseEvent ? e . clientY : e . touches [ 0 ] . clientY ) ;
88
+ if ( scaleX < scaleY ) {
89
+ mouseX = mouseX - rect . left ;
90
+ mouseY = mouseY - rect . top - ( canvas . clientHeight - canvas . height * scaleX ) / 2 ;
91
+
92
+ mouseX = mouseX / scaleX ;
93
+ mouseY = mouseY / scaleX ;
94
+ }
95
+ else {
96
+ mouseX = mouseX - rect . left - ( canvas . clientWidth - canvas . width * scaleY ) / 2 ;
97
+ mouseY = mouseY - rect . top ;
98
+
99
+ mouseX = mouseX / scaleY ;
100
+ mouseY = mouseY / scaleY ;
101
+ }
102
+
103
+ return { x : Math . round ( mouseX ) , y : Math . round ( mouseY ) } ;
104
+ }
105
+
106
+ let delta = 10 ;
107
+ let coordinates = getCoordinates ( e ) ;
108
+ let ref = this ;
109
+ for ( let i = 0 ; i < globalPoints . length ; i ++ ) {
110
+ if ( Math . abs ( globalPoints [ i ] . x - coordinates . x ) < delta && Math . abs ( globalPoints [ i ] . y - coordinates . y ) < delta ) {
111
+ canvas . addEventListener ( "mousemove" , dragPoint ) ;
112
+ canvas . addEventListener ( "mouseup" , releasePoint ) ;
113
+ canvas . addEventListener ( "touchmove" , dragPoint ) ;
114
+ canvas . addEventListener ( "touchend" , releasePoint ) ;
115
+
116
+ function dragPoint ( e : MouseEvent | TouchEvent ) {
117
+ coordinates = getCoordinates ( e ) ;
118
+ globalPoints [ i ] . x = coordinates . x ;
119
+ globalPoints [ i ] . y = coordinates . y ;
120
+ ref . drawQuad ( context , canvas ) ;
121
+ }
122
+
123
+ function releasePoint ( ) {
124
+ canvas . removeEventListener ( "mousemove" , dragPoint ) ;
125
+ canvas . removeEventListener ( "mouseup" , releasePoint ) ;
126
+ canvas . removeEventListener ( "touchmove" , dragPoint ) ;
127
+ canvas . removeEventListener ( "touchend" , releasePoint ) ;
128
+ }
129
+
130
+ break ;
131
+ }
132
+ }
133
+ }
134
+
135
+ drawQuad ( context : CanvasRenderingContext2D , canvas : HTMLCanvasElement ) : void {
136
+ let globalPoints = this . globalPoints ;
137
+ if ( ! globalPoints || globalPoints . length < 4 ) {
138
+ return ;
139
+ }
140
+
141
+ context . clearRect ( 0 , 0 , canvas . width , canvas . height ) ;
142
+ context . strokeStyle = "#00ff00" ;
143
+ context . lineWidth = 2 ;
144
+ for ( let i = 0 ; i < globalPoints . length ; i ++ ) {
145
+ context . beginPath ( ) ;
146
+ context . arc ( globalPoints [ i ] . x , globalPoints [ i ] . y , 5 , 0 , 2 * Math . PI ) ;
147
+ context . stroke ( ) ;
148
+ }
149
+ context . beginPath ( ) ;
150
+ context . moveTo ( globalPoints [ 0 ] . x , globalPoints [ 0 ] . y ) ;
151
+ context . lineTo ( globalPoints [ 1 ] . x , globalPoints [ 1 ] . y ) ;
152
+ context . lineTo ( globalPoints [ 2 ] . x , globalPoints [ 2 ] . y ) ;
153
+ context . lineTo ( globalPoints [ 3 ] . x , globalPoints [ 3 ] . y ) ;
154
+ context . lineTo ( globalPoints [ 0 ] . x , globalPoints [ 0 ] . y ) ;
155
+ context . stroke ( ) ;
156
+ }
65
157
}
0 commit comments