JavaScript Draw Frame of Video to Canvas - javascript

I am making a timeline using javascript and a canvas element. Typically, when the event on the timeline has an image associated to it, I use to following code to draw the image on the canvas:
var img = new Image();
img.src = 'image file name';
context.drawImage(img, 0, 0);
However, occasionally the 'image file name' is not actually an image, it's a video. In this case, I would just like to draw a frame of the video on the canvas. It could be any frame. However, I am unsure of how to do this.

i do not know (i am not web developer , i am android developer ) it is good or bed idea working with canvas for timeline but i think you should not use canvas . but i have find a video which can help you in this scenario in this video you can see that you can put image or video according your need in timeline here is the link .
NOTE : -
i do not own this channel or video or do not have any relation with firm .
Thank you .

Related

Export canvas on fabric.js to an image

I'm using fabric.js to make a draw and export the canvas to a .png without background. The thing is that I've been searching and this is the only thing that, in a way, makes sense to me:
document.getElementById("downloadbtn").onclick = saveImage();
function saveImage(c) {
this.href = canvas.toDataURL({
format: 'png',
quality: 0.8
});
this.download = 'testimage.png'
}
But it doesn't work... I've also tried a bunch of different stuff but the same thing happens:
Failed to execute toDataURL... error in the console.
Any helps? Ty
For those who had the same issue as me, the thing is:
You can save easily the canvas to an image, but you can't do it if you have an image inside the canvas, let me explain. If you have imported a nonlocal image into the canvas (like pasting an image with fromURL), the CORS of the web will detect this as a probable threat for the system, and will block the action.
For me, saving the canvas without the url image is fine, so I have no other solution for those who try to save the canvas with an external image, but maybe save first locally the image and then save it will work for you.
Hope it can help!

drawing image from localhost onto canvas in chrome

I am accessing an image from a folder in localhost.
Image loads correctly in img tag. But, when I try to draw that image onto canvas, it doesn't appear!
I have implemented it such that when image is dropped onto the canvas, it is drawn onto it and I draw it by getting the image from original source. When I drag again and again, then, after some attempts, it appears.
The functionality is working perfectly in Firefox and I.E.
This is the source of my image on localhost:
b.src = "http://localhost/casema...E/2780Chrysanthemum.jpg"
ctx1.drawImage(b, x2-15*z, y2-15*z, w1, h1 );
In HTML5, it not for sure that an image will always load immediately, so you
need to make sure that the image has fully loaded beford you draw it. this code may help you :
var myImage = new Image();
myImage.src = "path/to/your/image";
myImage.onload = function(){
//context is the canvas context
context.drawImage(myImage,x,y,weight,height);
}
Are you using a local HTML file on your computer? If so you need to add the parameter
--allow-file-access-from-files
when calling Chrome.

save image to a png file using processing.js

I have an small drawing application made in processing.js. I would like to save the image made using the application to a server using a php file (no problem on this).
I know the canvas element has a method to get that content, canvas.toDataURL() but I dont know how to do the same with processing.js
Any hint?
Cheers!
perhaps
var image = document.getElementsByTagName("canvas")[0].toDataURL();
So when you use saveFrame() in processing.js. The image opens in a new window and i found it hard to intercept the data. The soultion is to get the image from the canvas in javascript.
// this grabs the canvas and turns it into a base64 image
var image = document.getElementsByTagName("canvas")[0].toDataURL();
// Log the data to the canvas to check it working
console.log(image);
// then you can place the image on your web page
document.getElementById("theImage").src=image;
Then the html is easy. Just add
<img id="theImage"></img>
If you want to upload the image to a server you can acsess the data in js with.
image
Note that you can also use style display:none; and have the canvas render image for you without the canvas displaying. It also supports transparent pngs and the canvas by default is transparent

Extracting single frames from an animated GIF to canvas

I need to load all the frames of an animated GIF to an HTML5 canvas.
Please note, I don't want to "play" the animated (someone asked this before), all I want is to extract all the frames to use them as single images.
Buzzfeed have librarified the code from the repo tommitytom posted. I haven't tried it yet but it looks good.
https://github.com/buzzfeed/libgif-js
Take a look at jsgif; it downloads a GIF, parses it, and draws the individual frames of the file to a <canvas>. With a bit of digging you should be able to find the code that draws the individual frames and work from there.
Sorry, the short answer is that JavaScript has no way of controlling the current frame of an Animated GIF.
The long answer is that there are sort-of ways to do what you want with just JS, but they are very much convoluted hacks.
Example of hackish way: Create a canvas and don't add it to the DOM (so this won't be seen by anyone). In a fast loop (setTimeout), draw to this canvas constantly and collect snapshots. Compare the canvas ImageData to see if the frames have changed or not.
It would be a better use of your time, probably, to see how you can get your server to split it apart for you (with php/perl/python/etc)
When you only need the first frame of the GIF:
const image = document.getElementById('my-image');
const canvas = document.getElementById('my-canvas');
const ctx = canvas.getContext("2d");
// context.drawImage(img, x, y, width, height);
ctx.drawImage(image, 0, 0);
Credits: How to draw a GIF on a canvas?

What Does This Mean and How Does It Help?

At the moment I'm coding a web application that imports image data from Google Maps via the Static API - http://code.google.com/apis/maps/documentation/staticmaps/ - into an HTML5 canvas.
Unfortunately, I've run into the problem of not being able to manipulate the pixel data from Google Maps due to cross domain restrictions.
However, I've been reading this blog post by Mr. Doob, one of the people behind the Wilderness Downtown video ( http://thewildernessdowntown.com ) which employs canvas with Google Maps - http://mrdoob.com/blog/post/705 - and it reads:
"An additional challenge was that with you don't have access to the pixel data of images loaded from another domain...However, albeit pixel access is forbidden, context.drawImage() is allowed for copying areas from images hosted on other domains."
I feel this may be the solution to my problem as later in the post it shows pixel manipulation of the image, but I don't quite get what exactly it means by 'context.drawImage() is allowed for copying areas from images hosted on other domains' and it would be really helpful if someone could clarify it for me.
Thanks,
DLiKS
Edit: Here is the code I'm using to draw the Google Maps image to the canvas:
var ctx = document.getElementById('canvas').getContext('2d');
var img = new Image();
img.src = 'LINK TO GOOGLE MAPS IMAGE';
img.onload = function(){
ctx.drawImage(img,0,0);
}
The image displays OK but when I try to use getImageData to manipulate this embedded image on the canvas, I get a security error
Having read the article I think you misinterpreted what Mr.doob said:
"[Jamie] then started researching other ways of drawing the Maps Data in a way that would create the same effect."
He does no pixel manipulation, he uses context.drawImage for
"...cropping columns from the original image and positioning them one after the other horizontally."
Assign src to image using one aspx page, and that aspx page will respond with your text to the image.
For example:
image.src="CreateImage.aspx";
image.onload = function () {
ctx.drawImage(image,5,5,width,height);
}
context.drawImage() i believe is some way of manipulating a HTML 5 Canvas.
Take a look at these webpages.
http://dev.opera.com/articles/view/html-5-canvas-the-basics/
http://diveintohtml5.ep.io/canvas.html

Categories