Canvas drawImage sequential hierarchy problem

    var canvas = document.getElementById("poster");
    var windowWidth = $(window).width();
    var windowHeight = $(window).height();
    console.log(windowWidth, windowHeight)
    canvas.width = windowWidth * 0.76;
    canvas.height = canvas.width * 1.6;
    canvas.style.marginLeft = windowWidth * 0.12 + "px";
    canvas.style.marginTop = windowHeight * 0.07 + "px";

    var ctx = canvas.getContext("2d");
    var ratio = getPixelRatio(ctx);
//    {width,x,y}
    var postionArr = [
        {width: 0.86, x: 0.07, y: 0},
        {width: 0.88, x: 0.06, y: 0.33},
        {width: 0.76, x: 0.12, y: 0.335},
        {width: 0.72, x: 0.14, y: 0.34},
        {width: 0.6, x: 0.02, y: 1.2},
        {width: 0.2, x: 0.68, y: 1.21}];
    $(".poster-p").each(function (index, item) {
        var img = new Image();
        img.src = $(item).attr("src");
        img.onload = function () {
            ctx.drawImage(img, $("-sharpposter").width() * postionArr[index].x * ratio, $("-sharpposter").width() * postionArr[index].y * ratio, $("-sharpposter").width() * postionArr[index].width * ratio, $("-sharpposter").width() * postionArr[index].width * ($(item).height() / $(item).width()) * ratio);
        };
    });

now this order of normal drawing levels should be correct. The loading completion order should be different
so the larger files are finally drawn at the top, not in the order of cycles

.

clipboard.png

clipboard.png
the smaller picture can be drawn in a normal order

.
Mar.20,2021

write it in the answer.

in the body of the loop you wrote, only three things were done: create, load, and register callbacks. The registration callback will not block the occurrence of the next loop, so under the premise that the loading time is long and short, it is natural to determine the order by size.
so why I say you can add a preload in front of it is because you want to break the dependency between "load" and "drawImage" (because your drawImage is written in the load callback, so drawImage actually depends on loading): load first, and then in order (to find an independent order of data) drawImage, will not have this problem.


  1. first of all, canvas rendering doesn't trigger until the image is loaded, so you can't control the order in which each image is loaded. Suggestion: put all the image resources in a picture preload array, and load all the picture resources before draw. (see https://github.com/PengJiyuan.)
  2. you can set index on the array of resources to render. Array.sort, can control the order of rendering before rendering. (ditto, please refer to https://github.com/PengJiyuan.)
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b3dc13-2bc10.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b3dc13-2bc10.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?