image.onload
我正在使用 Canvas 在使用&context.drawImage
组合加载/绘制的图像上执行几件事。我正在使用一个返回值的简单函数来计算缩放图像的边界大小。我需要这些值以供稍后在我的代码中使用,但无论我做什么,我都无法将这些值分配给变量。在为 Canvas 分配计算尺寸后,我也无法访问 Canvas 的 styleheight/stylewidth 属性。
这是我的代码的伪充足
$(document).ready(function(){
//Canvas access, context reference etc here.
//Since I'm assigning styles to the canvas on the fly, the canvas has no ht/wdt yet
var dimes = '';
var image = new Image();
image.onload = function(){
//Apply original image height/width as canvas height/width 'attributes'
//(so that I can save the original sized image)
//Check if the image is larger than the parent container
//Calculate bounds if not
//Apply calculated dimensions as 'style' height/width to the canvas, so that the image fits
dimes = scaleImage(...);
//Works!
console.log(dimes);
//Rest all code
}
image.src = '...';
//Blank!!!
console.log(dimes);
//These all blank as well!!!
jQuery('#mycanvas').height() / width() / css('height') / css('width');
document.getElementById(canvas).style.height / .style.width / height / width;
});
我需要访问“重置”类型的函数的计算尺寸,该函数将我的画布与绘制的图像重置为计算的大小。