Open G0dwin opened 8 years ago
Here's some JS that should mostly work:
function generateScreenshot() {
html2canvas(document.body.parentElement).then(function(canvas) {
document.body.appendChild(canvas);
// create a low quality version
var data = canvas.toDataURL('image/jpeg', 0.1);
document.body.removeChild(canvas);
//upload the file data
var request = new XMLHttpRequest();
request.open('POST', '/my_url', true);
request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
request.send(
'file=' + encodeURI(data)
);
});
}
It looks like, in order to capture SVGs, we need to explicitly set the dimensions on the root node and define other properties in the SVG itself:
function generateScreenshot() {
// explicitly set the size of the svg
var svgs = document.getElementsByTagName('svg');
for (var i = 0; i < svgs.length; i++) {
svgs[i].setAttribute('height', svgs[i].clientHeight);
svgs[i].setAttribute('width', svgs[i].clientWidth);
}
// capture the page
html2canvas(document.body, { useCORS: true }).then(function(canvas) {
var data = canvas.toDataURL('image/jpeg', 0.9);
var src = encodeURI(data);
// do somethingwith the data
// ...
// reset the svg height and width
for (var i = 0; i < svgs.length; i++) {
svgs[i].removeAttribute('height');
svgs[i].removeAttribute('width');
}
});
}
<svg class="sprite icons bb-icon-logo" viewBox="0 0 291 288.5">
<style>
#bb-icon-logo {
fill: #00ADEF;
}
</style>
<path d="m290.7 136.1c-8.8.9-16.9-5.1-18.4-14-1.5-8.8 4-17.3 12.6-19.4-1.6-5.4-3.5-10.7-5.7-15.8-8 3.9-17.6 1-22.1-6.8-4.5-7.8-2.1-17.6 5.2-22.5-3.3-4.5-7-8.8-10.8-12.9-6.2 6.3-16.2 6.9-23.1 1.2-6.9-5.8-8-15.8-2.8-23-4.7-3.1-9.5-5.9-14.5-8.4-3.6 8.1-12.9 12.1-21.3 9-8.4-3.1-12.9-12.1-10.5-20.6-5.4-1.3-10.9-2.2-16.5-2.9-.6 8.8-8 15.8-16.9 15.8-9 0-16.3-7-16.9-15.8-5.5.6-10.9 1.6-16.2 2.8 2.4 8.5-2.1 17.5-10.6 20.6-8.4 3-17.7-1-21.3-9.1-5 2.5-9.9 5.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2.5.5-3.8.3-1.3.5-2.7.6-4 .1-.3.1-.5.1-.8 0-.3 0-.5.1-.8 0-1.1.2-2.2.4-3.1.3-1 .4-2 .4-3.1.1-3 .7-6.2 1.6-9.6 1-3.4 1.9-6.8 2.8-10.1l4-15.1c.4-1.4.8-2.8 1.1-4.1.3-1.3 1-2.3 1.9-3.1.9-.6 1.9-.9 3.2-.8.3-.1.6 0 .8.1.2.1.5.2.8.1l15.1 4c1.6.4 3 .9 4.3 1.4 1.3.5 2.2 1.3 2.6 2.4.5.6.5 1.6.2 2.8"></path>
</svg>
It looks like we can get the svg style using getComputedStyle
. This this pen for example code.
To ensure that our pages looks as expected on different devices, it may be possible to take a screenshot and upload the image to our server so that we can later investigate where problems exist.
There are definitely some privacy concerns here, however, as long as the data is dealt with securely and is kept confidential, we should not breach the users' trust.
If the proof of concept succeeds, we should gemify the concept so that others can make use of it as well.