[英]How to make a background of unknown size to an HTML5 canvas?
I want users to upload an image or specify a URL of an image and then be able to look like they're drawing on top of the image. 我希望用户上传图像或指定图像的URL,然后使其看起来像在图像上方绘制。 So I can make a
<canvas>
element with a background image. 因此,我可以使用背景图像制作
<canvas>
元素。 But I don't know beforehand how big that image is (well I can figure it out if they uploaded an image). 但是我事先不知道该图像有多大(好了,如果他们上传了图像,我可以弄清楚)。 How can I deal with this using jQuery?
我如何使用jQuery处理呢?
I know I can call $('img#id_name').width
and create a canvas based on that width (and get the height the same way). 我知道我可以调用
$('img#id_name').width
并基于该宽度创建一个画布(并以相同的方式获取高度)。 But I want that to be the background image. 但我希望将其作为背景图像。
You can get image's original width/height values after loading and than use css()
method for defining values. 您可以在加载后获取图像的原始宽度/高度值,然后使用
css()
方法定义值。
var img = $('img#id_name')[0]; // Get my img elem
var pic_real_width, pic_real_height;
$("<img/>") // Make in memory copy of image to avoid css issues
.attr("src", $(img).attr("src"))
.load(function() {
iWidth = this.width; // Note: $(this).width() will not
iHeight = this.height; // work for in memory images.
$('#canvasEle').css({'width': iWidth + 'px', 'height': iHeight + 'px'});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.