How to fetch a remote image to display in a canvas?

32,996

Solution 1

Use the built-in JavaScript Image object.

Here is a very simple example of using the Image object:

myimage = new Image();
myimage.src = 'http://myserver/nextimage.cgi';

Here is a more appropriate mechanism for your scenario from the comments on this answer.

Thanks olliej!

It's worth noting that you can't synchronously request a resource, so you should actually do something along the lines of:

myimage = new Image();
myimage.onload = function() {
                     ctx.drawImage(myimage, x, y);
                 }
myimage.src = 'http://myserver/nextimage.cgi';

Solution 2

If you want to draw an image to a canvas you also need to wait for the image to actually load, so the correct thing to do will be:

myimage = new Image();
myimage.onload = function() {
    context.drawImage(myimage, ...);
}
myimage.src = 'http://myserver/nextimage.cgi';

Solution 3

To add an image in JavaScript you can do the following:

myimage = new Image()
myimage.src='http://....'

If an image on your page has an ID "image1", you can assign the src of image1 to myimage.src.

Solution 4

I have found that using prototypes is very helpful here. If you aren't familiar with them, prototypes are part of objects that allow you to set your own variables and/or methods to them.

Doing something like:

Image.prototype.position = {
    x: 0,
    y: 0
}

Image.prototype.onload = function(){
    context.drawImage(this, this.position.x, this.position.y);
}

allows you to set position and draw to the canvas without too much work.

The "position" variable allows you to move it around on the canvas.
So it's possible to do:

var myImg = new Image();
myImg.position.x = 20;
myImg.position.y = 200;
myImg.src = "http://www.google.com/intl/en_ALL/images/logo.gif";

and the image will automatically draw to the canvas at (20,200).

Prototype works for all HTML and native Javascript objects. So

Array.prototype.sum = function(){
    var _sum = 0.0;
    for (var i=0; i<this.length; i++){
        _sum += parseFloat(this[i]);
    }
    return _sum;
}

gives a new function to all Arrays.

However,

var Bob;
Bob.Prototype.sayHi = function(){
    alert("Hello there.");
}

will not work (for multiple reasons, but i'll just talk about prototypes).
Prototype is a "property" of sorts, which contains all the your properties/methods that you input, and is already in each of the HTML and native Javascript objects (not the ones you make).
Prototypes also allow for easy calling (you can do "myImg.position.x" instead of "myImg.prototype.position.x" ).

Besides, if you are defining you variable, you should do it more like this.

var Bob = function(){
    this.sayHi = function(){
        alert("Hello there.");
    }
}
Share:
32,996
Mark Harrison
Author by

Mark Harrison

I'm a Software Engineer at Google where I work on machine learning planning systems. From 2001-2015 I was the Pixar Tech Lead of the Data Management Group. My 50-year charter was to store and catalog all data and metadata related to the Studio's feature films. This system ("Templar") is in use to this day. From 1997 to 2001 I lived in Beijing, China and was the Chief Software Architect at AsiaInfo, the company that built China's Internet. While there my software was used to grow the China Internet from 200K to 65M users. The last I heard they were at 350M+ users. I studied computer science and worked in Texas for many years. I wrote a couple of computer books... the best one was in print for 20 years. Feel free to drop me a line! [email protected]

Updated on December 17, 2021

Comments

  • Mark Harrison
    Mark Harrison over 2 years

    How can I fetch images from a server?

    I've got this bit of code which allows me to draw some images on a canvas.

    <html>
      <head>
        <script type="text/javascript">
          function draw(){
            var canvas = document.getElementById('canv');
            var ctx = canvas.getContext('2d');
    
            for (i=0;i<document.images.length;i++){
              ctx.drawImage(document.images[i],i*150,i*130);
            }
        }
        </script>
      </head>
      <body onload="draw();">
        <canvas id="canv" width="1024" height="1024"></canvas>
        <img src="http://www.google.com/intl/en_ALL/images/logo.gif">
        <img src="http://l.yimg.com/a/i/ww/beta/y3.gif">
        <img src="http://static.ak.fbcdn.net/images/welcome/welcome_page_map.png">
      </body>
    </html>
    

    Instead of looping over document.images, i would like to continually fetch images from a server.

    for (;;) {
        /* how to fetch myimage??? */
        myimage = fetch???('http://myserver/nextimage.cgi');
        ctx.drawImage(myimage, x, y);
    }
    
  • olliej
    olliej over 15 years
    It's worth noting that you can't synchronously request a resource, so you should actually do something along the lines of myimage.onload = function() { ctx.drawImage(myimage, x, y); } myimage.src = 'myserver/nextimage.cgi';
  • olliej
    olliej over 15 years
    Canvas doesn't display child elements, it's a programmable bitmap context you can draw to with JS, one of the API functions is drawImage() which takes an Image element/object and draws it into the buffer. It allows you to do nifty things that were previously not possible eg. canvaspaint.org
  • penetra
    penetra over 15 years
    Now that I understand the question, I would give a different answer. :)