Here is the basic implementation of how i'm drawing an image to the canvas.
var canvas = document.getElementById('myCanvas');
var context = canvas.getContext('2d');
var imageObj = new Image();
function drawImg(x, y) {
imageObj.onload = function() {
context.drawImage(imageObj, x, y);
};
imageObj.src = 'http://www.mysite.com/temp.jpg';
}
I have a server side image rendering script, where I am writing an image to the same file location eg: "temp.jpg". I'm using Ajax to cue the browser when the image changes, and I run the function to load the image again. However, it seems since the URL is always the same, the first image loaded is cached and will never redraw.
I've found several solutions but I'm not sure what will work best or is the real optimal solution. Some suggestions have included slightly manipulating the URL with a timestamp, and I've seen some references to streaming.
Some key points:
- I don't want to clear the canvas, I want the most recent image to be on top of the previous image, in the event the x,y coordinate change or the image size is different, it's supposed to layer.
- I really don't want to change the image filename for each new image, that seems overkill.
Thank you.
Edit:
Decided to include my github project for the socket.io version of this I am working on. I've got about 5 different versions in the works but this is the one I'm tracking mostly. Feel free to rip it apart I'm sure I have some bad practices when it comes to memory management. https://github.com/Pwn9/tcp-io-bridge