When I am trying to get a screenshot and save it as PNG before uploading video to server, I am having the following problem
In docs toDataURL("data:image/png;")
apparently not documented:
Try replace .toDataURL("data:image/png;");
by .toDataURL("image/png");
As well, took some experimentation to reach that conclusion, on iOS, looks the position .crossOrigin parameter position should be put before the .src
// Webkit will throw security error when image used on canvas and canvas.toDataUrl()
return new Promise((resolve, reject) => {
let image = new Image();
img.onload = () => {resolve(image)}
img.src = `${url}?${Date.now()}`;
img.crossOrigin = ""
})
// Webkit will not throw security error when image used on canvas and canvas.toDataUrl()
return new Promise((resolve, reject) => {
let img = new Image()
img.onload = () => {resolve(img)}
img.crossOrigin = ''
img.src = `${url}?${Date.now()}`
})
In my case, the problem was related to the presence of icons that weren't coming from my site as well as the presence of a Select input. Your video container may contain the same items. To solve, I had to hide the select inputs and icons before the screenshot was taken, then show them again after the screenshot was taken. Then, the canvas.toDataURL worked perfectly and didn't throw that security error.
Before Screenshot:
$("select[name='example']").add(".external-icon").hide();
After Screenshot:
$("select[name='example']").add(".external-icon").show();
It's because of the Same Origin Policy. Basically, you're not allowed to access the video data of something loaded from another origin/site using a canvas.
Drawing video data on the canvas sets the origin-clean
flag to false, which stops you from getting the image data in any way.
See toDataURL for more information.
Setting the attribute
before the src
solved the problem for our drawing app: https://www.trumpgraffitimemes.com/
useEffect(() => {
const img = new Image();
if (picdatanew.length !== 0) {
img.setAttribute("crossorigin", "anonymous");
img.src = picdatanew[picID].webformatURL;
setCanvasSize({
width: picdatanew[picID].webformatWidth,
height: picdatanew[picID].webformatHeight,
});
setPicturedata(img);
setWholedata([]);
setTimeout(() => {
contextRef.current.drawImage(
img,
0,
0,
picdatanew[picID].webformatWidth,
picdatanew[picID].webformatHeight
);
var image = canvasRef.current.toDataURL("image/jpg");
setMyImage(image);
}, 2000);
}
}, [picID, picdatanew]);
Hi i have the same problem, in a system where i show videos, my users have to create a thumb when they select a video, but then Security Error.
This solution only works if you can modify the server, then you can send the video or font or etc with CORS. So you have to edit httpd.conf( Apache configuration from the server where you pull the videos).
<IfModule mod_setenvif.c>
<IfModule mod_headers.c>
<FilesMatch "\.(mp4)$">
SetEnvIf Origin ":" IS_CORS
Header set Access-Control-Allow-Origin "*" env=IS_CORS
</FilesMatch>
</IfModule>
</IfModule>
and in your webpage or app, to the video tag add: crossOrigin="Anonymous"
This is a fragment of my code, after that all works again..
document.getElementById('video_thumb').innerHTML =
'<img src="'+data_uri+'" width="250"
height="250px" crossOrigin="Anonymous">';
With this, canvas.toDataURL()
don't trow error