Html2canvas screenshot is used but the screenshot is very different from the original - vue.js

Html2canvas screenshot is very different from the original picture. How to solve it
This is the address of my code https://github.com/cxm1308377432/html2canvas-screenshot
The above is the interface written with vue-grid-layout, you can modify the layout at will, and the below is the screenshot, but the two are very different
function download() {
var this1 = this;
setTimeout(function() {
html2canvas(this1.$refs.mine, { backgroundColor: null }).then(canvas =>
{
let dataURL = canvas.toDataURL("image/png");
this1.downImg = dataURL; console.log(dataURL);
});
}, 1000);
}

You better have a screenshot to show how different it is. But from my experience when I'm working with that library (html2canvas), the canvas can't render from difference origin. That's CORS problem. Unlucky, I haven't found any solution for this situation.
I read your source but can't found any image url so please update your answer with screenshot.

Related

How to add image from mobile storage to mapbox-gl?

I've been trying to add an image overlay to mapbox-gl-js on a project that uses Ionic 5 + Vue.js, similar to what Leaflet does, but i cannot get it to work.
The only way that I can get it to work is by following this tutorial, but the image does not fit properly into the polygon. I've also tried this, and I can see when Capacitor requests the image, but it does not load. It works fine on Chrome, but on mobile it does not.
This is the code that I have now:
const { map } = this;
if (map.getLayer(layerId)) {
map.removeLayer(layerId);
}
if (map.getSource(sourceId)) {
map.removeSource(sourceId);
}
map.addSource(sourceId, { ...source, url: Capacitor.convertFileSrc(source.url) });
map.addLayer(layer);
whereas the source and layer objects are like this:
const sourceId = `${talhao.id}-image`;
const layerId = `${talhao.id}-image-${image.id}`;
const bbox = getBoundingBox(talhao);
const source = {
type: "image",
url: image.imagemPng,
coordinates: [
bbox.getNorthWest().toArray(),
bbox.getNorthEast().toArray(),
bbox.getSouthEast().toArray(),
bbox.getSouthWest().toArray()
]
};
const layer = {
id: layerId,
source: sourceId,
type: "raster",
paint: {
"raster-opacity": 0.85
}
};
Any help would be appreciated!
edit: It is worth noticing that I do not think that the image is the problem here, because I use the same image in another part of the screen that I'm working and it loads fine.
Turns out the mapbox-gl-js library had a bug in version 1.x that prevented the image from showing on mobile.
Upgraded to version 2.0.1 and it worked.

Sharing image generated from html canvas on FB

This one I've been banging my head against for a few weeks now.
Scenario:
Let user generate an image out of layers they select
Convert image to canvas
Share image from canvas on facebook wall using share_open_graph (along with the image, a short text and title will be shared)
I've already had a solution in place using publish_actions but that was recently removed from the API and is no longer available.
I am using js and html for all code handling.
The issue is that I can generate a png image from the canvas but that is saved as base64 and share_open_graph doesn't allow this type of image, it needs a straight forward url such as './example.png'. I have tried using several approaches and with canvas2image, converting and saving image using file-system but all of these fail.
Does anyone have similar scenario and possible solution from April/May 2018 using share_open_graph ?
My current code looks like this - it fails at the image conversion and save to a file (Uncaught (in promise) TypeError: r.existsSync is not a function at n (file-system.js:30)). But I am open to different solutions as this is clearly not working.
html2canvas(original, { width: 1200, height: 628
}).then(function(canvas)
{
fb_image(canvas);
});
var fb_image = function(canvas) {
canvas.setAttribute('id', 'canvas-to-share');
document.getElementById('img-to-share').append(canvas);
fbGenerate.style.display = 'none';
fbPost.style.display = 'block';
var canvas = document.getElementById('canvas-to-share');
var data = canvas.toDataURL('image/png');
var encodedPng = data.substring(data.indexOf(',') + 1, data.length);
var decodedPng = base64.decode(encodedPng);
const buffer = new Buffer(data.split(/,\s*/)[1], 'base64');
pngToJpeg({ quality: 90 })(buffer).then(output =>
fs.writeFile('./image-to-fb.jpeg', output));
var infoText_content = document.createTextNode('Your image is being
posted to facebook...');
infoText.appendChild(infoText_content);
// Posting png from imageToShare to facebook
fbPost.addEventListener('click', function(eve) {
FB.ui(
{
method: 'share_open_graph',
action_type: 'og.shares',
href: 'https:example.com',
action_properties: JSON.stringify({
object: {
'og:url': 'https://example.com',
'og:title': 'My shared image',
'og:description': 'Hey I am sharing on fb!',
'og:image': './image-to-fb.jpeg',
},
}),
},
function(response) {
console.log(response);
}
);
});
};

Saving Screenshot of the web page that has Cesium

I'm trying to add a button to my interface that will download a screenshot taken of the web page.
It works for the side bar but my Cesium map appears plain white.
Can someone help me out with is?
Here is a code
var Capture = function() {
html2canvas(document.body, {
onrendered: function (canvas) {
var tempcanvas=document.createElement('canvas');
tempcanvas.width=1050;
tempcanvas.height=1050;
var context=tempcanvas.getContext('2d');
context.drawImage(canvas,5,5);
var link=document.createElement("a");
link.href=tempcanvas.toDataURL('image/jpg'); //function blocks CORS
link.download = 'screenshot.jpg';
link.click();
}
});
}
This was based on the question asked here
So the answer turned out to be by using scene.canvas.
I was directed to this solution by a similar question on the Cesium Forum.

Greensock, jquery, code does not fire in chrome?

I have no idea why, but for some reason my code does not fire in chrome. Any idea why? I read the entire documentation of Greensock but nothing that adresses my issue.
http://jsfiddle.net/w0otqcw8/6/
var t = TweenLite.to(".menu-button", 4, {left:'0', opacity:'0.9', ease:Bounce.easeOut, paused:true, reversed:true});
$(".menu-button").on("click", function() {
if (t.reversed()) {
t.play();
} else {
t.reverse();
}
});
Your code adds right and left css properties at the same time. Animate x,y,xPercent and yPercent instead.

How do I create easily a PDF from an SVG with jsPDF?

I'm trying to create a pdf but I have some SVG pictures. I found information about this problem, but I just have to use JavaScript, that's to say, no jQuery.
I found jsPDF here : https://github.com/MrRio/jsPDF
There is the plugin jspdf.plugin.sillysvgrenderer.js (in the same folder) and where we can find an exemple of PDF created in the folder test.
But when I try to generate the PDF on my own, it doesn't work and I don't understand why.
Do you know how to do it?
I got this plugin working, but only with SVG file from the tests and the I saw in the doc that only PATHs are supported :(
There is already the issue on github
https://github.com/MrRio/jsPDF/issues/384
If paths are ok for here is my code (it's more or less the code from the tests):
function demoSvgDocument() {
var doc = new jsPDF();
var test = $.get('013_sillysvgrenderer.svg', function(svgText){
var svgAsText = new XMLSerializer().serializeToString(svgText.documentElement);
doc.addSVG(svgAsText, 20, 20, doc.internal.pageSize.width - 20*2)
// Save the PDF
doc.save('TestSVG.pdf');
});
}
Another point to consider, you have to run all examples on a server. Otherwise you won't see any results probably because of the security
Try canvg for that to covert SVG to Canvas. Then convert the canvas to base64 string using .toDataURL().
More detailed answer is here https://stackoverflow.com/a/35788928/2090459
Check the demo here http://jsfiddle.net/Purushoth/hvs91vpq/
Canvg Repo: https://github.com/gabelerner/canvg
There now is svg2pdf.js which uses a fork of jsPDF.
It has been created to solve this exact task: Exporting an SVG to a PDF.
Also in the meantime, jsPDF also added a demo that shows how to possibly export SVG using canvg and the jsPDF canvas implementation.
The two solutions have different advantages and disadvantages, so you might want to try both and see if one of them suits your needs.
You can use the canvas plugin that comes with jsPDF to render the SVG on the PDF with canvg. I've had to set a few dummy properties on the jsPDF canvas implementation, and disable the interactive/animation features of canvg for this to work without errors:
var jsPdfDoc = new jsPDF({
// ... options ...
});
// ... whatever ...
// hack to make the jspdf canvas work with canvg
jsPdfDoc.canvas.childNodes = {};
jsPdfDoc.context2d.canvas = jsPdfDoc.canvas;
jsPdfDoc.context2d.font = undefined;
// use the canvg render the SVG onto the
// PDF via the jsPDF canvas plugin.
canvg(jsPdfDoc.canvas, svgSource, {
ignoreMouse: true,
ignoreAnimation: true,
ignoreDimensions: true,
ignoreClear: true
});
This seems to me a much better solution than the SVG plugin for jsPDF, as canvg has much better support of SVG features. Note that the width and height properties should be set on the <svg/> element of your SVG for canvg to render it correctly (or at least so it seemed to me).
I modified this from: https://medium.com/#benjamin.black/using-blob-from-svg-text-as-image-source-2a8947af7a8e
var yourSVG = document.getElementsByTagName('svg')[0];
//or use document.getElementById('yourSvgId'); etc.
yourSVG.setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns', 'http://www.w3.org/2000/svg');
yourSVG.setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:xlink', 'http://www.w3.org/1999/xlink');
var serializer = new XMLSerializer();
var serialSVG = serializer.serializeToString(yourSVG);
var svg = serialSVG;
var blob = new Blob([svg], {type: 'image/svg+xml'});
var url = URL.createObjectURL(blob);
var image = document.createElement('img');
// image.addEventListener('load', () => URL.revokeObjectURL(url), {once: true});
//changed above line using babel to code below;
image.addEventListener('load', function () {
return URL.revokeObjectURL(url);
}, { once: true });
image.src = url;
//Then just use your pdf.addImage() function as usual;