【问题标题】:Loading multiple png images in html5 canvas在 html5 画布中加载多个 png 图像
【发布时间】:2015-11-10 16:48:12
【问题描述】:

我已经看过很多关于如何加载单个图像然后绘制该图像以及在图像上绘制形状等的代码 sn-ps 和教程,但是有没有一种方法可以加载多个图像(png)将透明部分放到画布上,这样您就可以将它们分层然后下载合成?

布赖恩

【问题讨论】:

    标签: html image canvas


    【解决方案1】:

    这是一个图像预加载器的示例,它将等待所有图像完全加载,然后调用start() 函数:

    // image preloader
    
    // canvas related variables
    var canvas=document.getElementById("canvas");
    var ctx=canvas.getContext("2d");
    var cw=canvas.width;
    var ch=canvas.height;
    
    // put the paths to your images in imageURLs[]
    var imageURLs=[];  
    imageURLs.push("https://dl.dropboxusercontent.com/u/139992952/multple/face1.png");
    imageURLs.push("https://dl.dropboxusercontent.com/u/139992952/multple/face2.png");
    
    // the loaded images will be placed in imgs[]
    var imgs=[];
    var imagesOK=0;
    startLoadingAllImages(imagesAreNowLoaded);
    
    // Create a new Image() for each item in imageURLs[]
    // When all images are loaded, run the callback (==imagesAreNowLoaded)
    function startLoadingAllImages(callback){
    
      // iterate through the imageURLs array and create new images for each
      for (var i=0; i<imageURLs.length; i++) {
        // create a new image an push it into the imgs[] array
        var img = new Image();
        // Important! By pushing (saving) this img into imgs[],
        //     we make sure the img variable is free to
        //     take on the next value in the loop.
        imgs.push(img);
        // when this image loads, call this img.onload
        img.onload = function(){ 
          // this img loaded, increment the image counter
          imagesOK++; 
          // if we've loaded all images, call the callback
          if (imagesOK>=imageURLs.length ) {
            callback();
          }
        };
        // notify if there's an error
        img.onerror=function(){alert("image load failed");} 
        // set img properties
        img.src = imageURLs[i];
      }      
    }
    
    // All the images are now loaded
    // Do drawImage & fillText
    function imagesAreNowLoaded(){
    
      // the imgs[] array now holds fully loaded images
      // the imgs[] are in the same order as imageURLs[]
    
      ctx.font="30px sans-serif";
      ctx.fillStyle="#333333";
    
      // drawImage the first image (face1.png) from imgs[0]
      // and fillText its label below the image
      ctx.drawImage(imgs[0],0,10);
      ctx.fillText("face1.png", 0, 135);
    
      // drawImage the first image (face2.png) from imgs[1]
      // and fillText its label below the image
      ctx.drawImage(imgs[1],200,10);
      ctx.fillText("face2.png", 210, 135);
    
    }
    

    【讨论】:

    • 您不应该使用for loop 去思考每个image 对象,然后渲染它而不是复制代码吗?你也不应该有一个class 来保持图像位置吗? OP 可能想要在画布周围移动图像。
    • @Canvas 叹息...请查看代码。确实有一个 for 循环可以创建、获取、加载和保存每个图像对象——没有重复的代码。如果您愿意,您可以创建一个类,但是(IMO)一个类是多余的,因为代码非常简单(并且通常在应用程序开始时只使用一次)。加载后,所有图像都可以通过 imgs[] 数组随时在代码中使用。
    猜你喜欢
    • 2015-08-31
    • 1970-01-01
    • 2011-10-04
    • 1970-01-01
    • 1970-01-01
    • 2018-07-30
    • 1970-01-01
    • 1970-01-01
    • 2015-10-12
    相关资源
    最近更新 更多