Skip to content

Commit 717e77b

Browse files
committed
Save to Png is now calculating the bounding box of the elements in the graph and using that to select the relevant part of the graph canvas.
1 parent 460c845 commit 717e77b

File tree

2 files changed

+79
-2
lines changed

2 files changed

+79
-2
lines changed

src/commands/showComponentHierarchy/showComponentHierarchy_Template.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
<body>
1515
<div id="buttons">
1616
<input type="button" id="saveAsPngButton" value="Save as png">
17+
<input type="button" id="saveSelectionAsPngButton" value="Save selection as png">
1718
<input type="button" id="copyToClipboardButton" value="Copy to clipboard">
1819
<div id="helpText">Click and drag to select the area to be saved.</div>
1920
</div>

src/commands/showComponentHierarchy/showComponentHierarchy_Template.js

Lines changed: 78 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@
4040
// add button event listeners
4141
const saveAsPngButton = document.getElementById('saveAsPngButton');
4242
saveAsPngButton.addEventListener('click', saveAsPng);
43+
const saveSelectionAsPngButton = document.getElementById('saveSelectionAsPngButton');
44+
saveSelectionAsPngButton.addEventListener('click', saveSelectionAsPng);
4345
const copyToClipboardButton = document.getElementById('copyToClipboardButton');
4446
copyToClipboardButton.addEventListener('click', copyToClipboard);
4547
copyToClipboardButton.style['display'] = 'none'; // TODO: Remove when copyToClipboard is implemented
@@ -59,7 +61,9 @@
5961
command: 'saveAsPng',
6062
text: finalSelectionCanvas.toDataURL()
6163
});
64+
// Remove the temporary canvas
6265
finalSelectionCanvas.remove();
66+
// Reset the state variables
6367
selectionCanvasContext = undefined;
6468
selection = {};
6569
// hide the help text
@@ -98,7 +102,7 @@
98102
}
99103
}
100104

101-
function saveAsPng() {
105+
function saveSelectionAsPng() {
102106
// show the help text
103107
helpTextDiv.style['display'] = 'block';
104108

@@ -117,8 +121,80 @@
117121
selectionLayer.addEventListener("mousemove", mouseMoveEventListener, true);
118122
}
119123

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+
120196
function copyToClipboard() {
121197
console.log('Not implemented yet...');
122198
}
123-
199+
124200
}());

0 commit comments

Comments
 (0)