HTML 5 - Use <img> element vs. Canvas drawImage

半腔热情 提交于 2019-12-01 21:23:21

The img element works since the dawn of time. Png image support also since a very long time. The other piece of code always needs Javascript, and needs a more modern browser that supports HTML 5 canvas. Some of these advantages may not apply when the usecase is a pushpin in Bing Maps, since Bing Maps will have requirements on its ownn.

In general performance there won't be much difference, and this may even vary between browsers and versions. Drawing an image is pretty easy anyway (compared to, say, animation), so I wouldn't worry about it.

base_image.onload = function() {
  context.drawImage(base_image, 100, 100);
}

assigns the function as an event handler that gets called only when the image in question has finished downloading.

base_image.src = 'img/base.png';

begins the download of the image and it may be regarded as an asynchronous (bon-blocking) call for the image.

<img src='../images/myicons/pin.png'> begins drawing as the page loads and you get this line-by-line display of the image as it's data trickles in.

The difference between these approaches is at least partially a UI consideration and especially evident with large images and slow internet connection. Not so much an issue with fast connections, small/cached images.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!