40
40
// add button event listeners
41
41
const saveAsPngButton = document . getElementById ( 'saveAsPngButton' ) ;
42
42
saveAsPngButton . addEventListener ( 'click' , saveAsPng ) ;
43
+ const saveSelectionAsPngButton = document . getElementById ( 'saveSelectionAsPngButton' ) ;
44
+ saveSelectionAsPngButton . addEventListener ( 'click' , saveSelectionAsPng ) ;
43
45
const copyToClipboardButton = document . getElementById ( 'copyToClipboardButton' ) ;
44
46
copyToClipboardButton . addEventListener ( 'click' , copyToClipboard ) ;
45
47
copyToClipboardButton . style [ 'display' ] = 'none' ; // TODO: Remove when copyToClipboard is implemented
59
61
command : 'saveAsPng' ,
60
62
text : finalSelectionCanvas . toDataURL ( )
61
63
} ) ;
64
+ // Remove the temporary canvas
62
65
finalSelectionCanvas . remove ( ) ;
66
+ // Reset the state variables
63
67
selectionCanvasContext = undefined ;
64
68
selection = { } ;
65
69
// hide the help text
98
102
}
99
103
}
100
104
101
- function saveAsPng ( ) {
105
+ function saveSelectionAsPng ( ) {
102
106
// show the help text
103
107
helpTextDiv . style [ 'display' ] = 'block' ;
104
108
117
121
selectionLayer . addEventListener ( "mousemove" , mouseMoveEventListener , true ) ;
118
122
}
119
123
124
+ function saveAsPng ( ) {
125
+ // Calculate the bounding box of all the elements on the canvas
126
+ const boundingBox = getBoundingBox ( ) ;
127
+
128
+ // copy the imagedata within the bounding box
129
+ const finalSelectionCanvas = document . createElement ( 'canvas' ) ;
130
+ finalSelectionCanvas . width = boundingBox . width ;
131
+ finalSelectionCanvas . height = boundingBox . height ;
132
+ const finalSelectionCanvasContext = finalSelectionCanvas . getContext ( '2d' ) ;
133
+ finalSelectionCanvasContext . drawImage ( graphCanvas , boundingBox . top , boundingBox . left , boundingBox . width , boundingBox . height , 0 , 0 , boundingBox . width , boundingBox . height ) ;
134
+
135
+ // Call back to the extension context to save the image of the graph to the workspace folder.
136
+ vscode . postMessage ( {
137
+ command : 'saveAsPng' ,
138
+ text : finalSelectionCanvas . toDataURL ( )
139
+ } ) ;
140
+
141
+ // Remove the temporary canvas
142
+ finalSelectionCanvas . remove ( ) ;
143
+ }
144
+
145
+ function getBoundingBox ( ) {
146
+ var ctx = graphCanvas . getContext ( '2d' ) ;
147
+ const imgData = ctx . getImageData ( 0 , 0 , graphCanvas . width , graphCanvas . height ) ;
148
+ var bytesPerPixels = 4 ;
149
+ var cWidth = graphCanvas . width * bytesPerPixels ;
150
+ var cHeight = graphCanvas . height ;
151
+ var minY = minX = maxY = maxX = - 1 ;
152
+ for ( var y = cHeight ; y > 0 && maxY === - 1 ; y -- ) {
153
+ for ( var x = 0 ; x < cWidth ; x += bytesPerPixels ) {
154
+ var arrayPos = x + y * cWidth ;
155
+ if ( imgData . data [ arrayPos + 3 ] > 0 && maxY === - 1 ) {
156
+ maxY = y ;
157
+ break ;
158
+ }
159
+ }
160
+ }
161
+ for ( var x = cWidth ; x >= 0 && maxX === - 1 ; x -= bytesPerPixels ) {
162
+ for ( var y = 0 ; y < maxY ; y ++ ) {
163
+ var arrayPos = x + y * cWidth ;
164
+ if ( imgData . data [ arrayPos + 3 ] > 0 && maxX === - 1 ) {
165
+ maxX = x / bytesPerPixels ;
166
+ break ;
167
+ }
168
+ }
169
+ }
170
+ for ( var x = 0 ; x < maxX * bytesPerPixels && minX === - 1 ; x += bytesPerPixels ) {
171
+ for ( var y = 0 ; y < maxY ; y ++ ) {
172
+ var arrayPos = x + y * cWidth ;
173
+ if ( imgData . data [ arrayPos + 3 ] > 0 && minX === - 1 ) {
174
+ minX = x / bytesPerPixels ;
175
+ break ;
176
+ }
177
+ }
178
+ }
179
+ for ( var y = 0 ; y < maxY && minY === - 1 ; y ++ ) {
180
+ for ( var x = minX * bytesPerPixels ; x < maxX * bytesPerPixels ; x += bytesPerPixels ) {
181
+ var arrayPos = x + y * cWidth ;
182
+ if ( imgData . data [ arrayPos + 3 ] > 0 && minY === - 1 ) {
183
+ minY = y ;
184
+ break ;
185
+ }
186
+ }
187
+ }
188
+ return {
189
+ 'top' : minX ,
190
+ 'left' : minY ,
191
+ 'width' : maxX - minX ,
192
+ 'height' : maxY - minY
193
+ } ;
194
+ }
195
+
120
196
function copyToClipboard ( ) {
121
197
console . log ( 'Not implemented yet...' ) ;
122
198
}
123
-
199
+
124
200
} ( ) ) ;
0 commit comments