I\'m displaying an image (from a file) on the browser using html... I have another program that keeps taking a screenshot of my screen and storing it as an image file \"image.jp
First, when you set the src
attribute of your image object, the image has not been loaded yet, you need to refresh your canvas when the onload
of the image gets fired (when the image is done loading).
Secondly, the browser tries to use the cached image image.jpeg
. To avoid that, add a bogus argument to the image URI.
For example :
var timeoutPeriod = 1000;
var imageURI = 'image.jpeg';
var x=0, y=0;
var img = new Image();
img.onload = function() {
var canvas = document.getElementById("x");
var context = canvas.getContext("2d");
context.drawImage(img, x, y);
x+=20; y+=20;
setTimeout(timedRefresh,timeoutPeriod);
};
function timedRefresh() {
// just change src attribute, will always trigger the onload callback
img.src = imageURI + '?d=' + Date.now();
}
And then it should work.