简体   繁体   English

如果我尝试对尚未完全加载的图像使用drawImage()会发生什么?

[英]What would happen if I try use drawImage() for an image which not yet fully loaded?

If I pass the reference to an image to drawImage API of HTML canvas , but the image is still loading, then what would happen? 如果我将对图像的引用传递给HTML canvas drawImage API,但是图像仍在加载,那么会发生什么? Will get an exception, or the API will go ahead with the partial data, or the API will block till the image is loaded? 会发生异常,还是API将继续处理部分数据,或者API将阻塞直到加载图像?

The canvas spec dictates that nothing shall be drawn, even if it is 99% loaded. 画布规范规定,即使已加载99%,也不应绘制任何内容。 It does not throw an error. 它不会引发错误。

So if you do: 因此,如果您这样做:

var img = new Image();
img.src = 'http://placekitten.com/300/300';
// Might occur before the image is done loading (bad!)
ctx.drawImage(img, 0, 0);

This is why most people do something similar to: 这就是为什么大多数人都做类似的事情的原因:

var img = new Image();
img.onload = function() {
  // Will occur only once the image is done loading
  ctx.drawImage(img, 0, 0);
}
img.src = 'http://placekitten.com/300/300';

To ensure that the image will get drawn. 以确保将绘制图像。

The spec says that nothing is to be drawn. 规范说什么也没画。

If the first argument isn't an img, canvas, or video element, throws a TYPE_MISMATCH_ERR exception. 如果第一个参数不是img,canvas或video元素,则抛出TYPE_MISMATCH_ERR异常。 If the image has no image data, throws an INVALID_STATE_ERR exception. 如果图像没有图像数据,则引发INVALID_STATE_ERR异常。 If the one of the source rectangle dimensions is zero, throws an INDEX_SIZE_ERR exception. 如果源矩形尺寸之一为零,则引发INDEX_SIZE_ERR异常。 If the image isn't yet fully decoded, then nothing is drawn. 如果图像尚未完全解码,则不会绘制任何内容。

I tried it just for the hell of it. 我只是为了它的地狱尝试了它。 You can see the results here. 您可以在此处查看结果。

I basically used PHP to slow down the image loading. 我基本上使用PHP来减慢图像加载速度。

slowimage.php slowimage.php

<?php
    sleep(3);

    header('Content-type: image/png');
    echo file_get_contents('stackoverflow-logo-300.png');
?>

index.html index.html

<canvas id="canvas" width="512" height="512">

<script>
    var img = new Image();
    img.onload = function() {
        alert("image loaded");
    };
    img.src = "slowimage.php";

    var ctx = document.getElementById("canvas").getContext("2d");
    ctx.drawImage(img, 0, 0);
</script>

As has been established, and as you can see, nothing is drawn. 正如已经建立的,并且您可以看到,没有绘制任何内容。

I once stumbled upon this; 我曾经偶然发现这一点; the problem was that I didn't see anything drawn because I wasn't using img.onload . 问题是我没有使用img.onload所以看不到任何东西。

You can confirm it: http://jsfiddle.net/pimvdb/eGjak/100/ . 您可以确认它: http : //jsfiddle.net/pimvdb/eGjak/100/ It will most probably not show at first, but will after reload because of the cache. 它一开始很可能不会显示,但是由于缓存的原因,它将在重新加载后显示。

The best practice is to use onload at all times. 最佳做法是始终使用onload

The API will draw nothing onto the canvas. API不会在画布上绘制任何内容。 Unless you are using a loop that redraws the canvas with the image included on it the user will never see the image. 除非使用循环来重绘包含有图像的画布,否则用户将永远看不到图像。 The normal practice is to use onload with images for canvas - all browsers that support canvas support img.onload. 通常的做法是在画布上使用onload和图片-所有支持画布的浏览器都支持img.onload。 In a loop situation you may be able to get away with adding images on the fly but know that until they are downloaded they are not seen. 在循环情况下,您也许可以动态添加图像,但是知道在下载图像之前看不到它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM