I have the following fonction, that gets an image as parameter:
function getIcon(img){
var canvas = document.createElement("canvas");
canvas.width = img.width;
canvas.height = img.height;
var ctx = canvas.getContext("2d");
ctx.drawImage(img, 0, 0);
var dataURL = canvas.toDataURL("image/png");
return dataURL;
}
for instance:
icon = getIcon(document.getElementById("car_marker"));
with:
<img id="car_marker" src="img/car.png"/>
Then the icon created is used as a marker in Google Map:
var marker = new google.maps.Marker({ position: latLng,
map: map,
title: description,
id: id,
icon: icon
});
This part is working fine but I now need to modify the getIcon method so it takes another image as parameter and merge both images. As the new image I need to add is in base 64 encoding, the following does not work as expected:
function getIcon(img1, img2){
var canvas = document.createElement("canvas");
canvas.width = img1.width;
canvas.height = img1.height;
var ctx = canvas.getContext("2d");
ctx.drawImage(img1, 0, 0);
ctx.drawImage(img2, 0, 0); // add the base 64 encoded image here... does not work
var dataURL = canvas.toDataURL("image/png");
return dataURL;
}
I end up with:
Could not convert JavaScript argument arg 0 [nsIDOMCanvasRenderingContext2D.drawImage]
I finally figured it out:
Then, the function that takes those 2 images:
That is working fine (except a strange refresh problem: the image2 does not appear on the image1 when the page is loaded but it’s ok after a reload).