I have a canvas object in which I want to put an image for a web application. I can upload an image, but I ran into two problems: the image will not stretch to the canvas, and the canvas will not stretch to cover the entire div in any browser other than Firefox.
http://jsfiddle.net/LFJ59/1/
var canvas = $("#imageView");
var context = canvas.get(0).getContext("2d");
$(document).ready(drawImage());
$(window).resize(refreshCanvas());
refreshCanvas();
function refreshCanvas() {
canvas.attr("width", $(window).get(0).innerWidth / 2);
canvas.attr("height", $(window).get(0).innerHeight / 2);
drawImage();
};
function drawImage() {
context.shadowBlur = 20;
context.shadowColor = "rgb(0,0,0)";
var image = new Image();
image.src = "http://www.netstate.com/states/maps/images/ca_outline.gif";
$(image).load(function () {
image.height = canvas.height();
image.width = canvas.width();
context.drawImage(image);
});
};
Is there a solution to make the canvas responsive? Or do I just need to lock the canvas and image to predefined sizes?
source
share