canvas - drawImage并不总是有效

时间:2016-04-09 13:13:00

标签: javascript canvas

有时画布画不起作用,我不知道为什么......

有javascript:

function preload_image_to_canvas(){
    var canvas = document.getElementById("canvas");
    var context = canvas.getContext("2d");
    var background = document.getElementById("background_picture");

    // Set canvas dimension
    canvas.width = background.width;
    canvas.height = background.height;

    // Draw the background

    draw_background(context, background, function(){

        canvas.style.display = 'block';
        canvas.style.position = 'inherit';
        background.style.display = 'none';
    });
}

// This functions draws a given image on the canvas
function draw_background(context, background, cb) {
    context.drawImage(background, 0, 0);
    cb(context);
}

还有PHP:

echo '<div id="placeholder">';
echo '<img id="background_picture" onload="preload_image_to_canvas();" src="'$background_info->background_url . '.jpg" />';
echo '</div>';

也许你有一些提示?

0 个答案:

没有答案