1. <tfoot id='l189d'></tfoot>

        • <bdo id='l189d'></bdo><ul id='l189d'></ul>
        <legend id='l189d'><style id='l189d'><dir id='l189d'><q id='l189d'></q></dir></style></legend>

      1. <i id='l189d'><tr id='l189d'><dt id='l189d'><q id='l189d'><span id='l189d'><b id='l189d'><form id='l189d'><ins id='l189d'></ins><ul id='l189d'></ul><sub id='l189d'></sub></form><legend id='l189d'></legend><bdo id='l189d'><pre id='l189d'><center id='l189d'></center></pre></bdo></b><th id='l189d'></th></span></q></dt></tr></i><div id='l189d'><tfoot id='l189d'></tfoot><dl id='l189d'><fieldset id='l189d'></fieldset></dl></div>

        <small id='l189d'></small><noframes id='l189d'>

        如何从数组中的图像源创建画布图像?

        How can i create Canvas Image from image sources in an array?(如何从数组中的图像源创建画布图像?)
          <tbody id='hDBIu'></tbody>
        1. <tfoot id='hDBIu'></tfoot>

              • <bdo id='hDBIu'></bdo><ul id='hDBIu'></ul>
              • <small id='hDBIu'></small><noframes id='hDBIu'>

                <i id='hDBIu'><tr id='hDBIu'><dt id='hDBIu'><q id='hDBIu'><span id='hDBIu'><b id='hDBIu'><form id='hDBIu'><ins id='hDBIu'></ins><ul id='hDBIu'></ul><sub id='hDBIu'></sub></form><legend id='hDBIu'></legend><bdo id='hDBIu'><pre id='hDBIu'><center id='hDBIu'></center></pre></bdo></b><th id='hDBIu'></th></span></q></dt></tr></i><div id='hDBIu'><tfoot id='hDBIu'></tfoot><dl id='hDBIu'><fieldset id='hDBIu'></fieldset></dl></div>
                <legend id='hDBIu'><style id='hDBIu'><dir id='hDBIu'><q id='hDBIu'></q></dir></style></legend>
                  本文介绍了如何从数组中的图像源创建画布图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我想从来自我的 ajax 请求的数组中的多个图像创建一个 Canvas 图像;为此,我尝试运行循环,但 drawImage 不适用于循环.

                  I want to create a Canvas image from multiple images in an array coming from my ajax request; for that purpose i try to run the loop but drawImage does not works with the loop.

                  然后我尝试了一个函数,并在循环中调用了该函数,但同样的事情发生了 drawImage 不适用于此

                  Then i try a function, and called that function in a loop but same thing happens drawImage does not works with this

                  下面我提到了所有的代码一和函数一的循环 &一个在 drawImage 中具有静态信息的当前工作.

                  below i have mentioned all the code one with the function one with the loop & one with static information in drawImage which is currently working.

                  如果有人请指导我如何解决此问题,我将不胜感激.

                  运行良好的静态 drawImage 代码

                  function loadImages(sources, callback, imagesrc) {
                          var images = {};
                          var loadedImages = 0;
                          var numImages = 0;
                  
                          for(var src in sources) {
                            numImages++;
                          }
                  
                          for(var src in sources) {
                            images[src] = new Image();
                            images[src].onload = function() {
                              if(++loadedImages >= numImages) {
                                callback(images);
                              }
                            };
                            images[src].src = sources[src];
                          }
                  
                  
                   }
                  
                        var canvas = document.getElementById('product-image');
                  
                        canvas.height = (jQuery(window).height()) -120;
                        canvas.width = canvas.height * 0.75;
                        var heightscreen = (jQuery(window).height()) -120;
                        var canvasheight = heightscreen;
                        var canvaswidth = canvas.height * 0.75;
                        canvaswidthdiv4 = 0;
                        var widthNeeded = canvasheight * 0.75;
                  
                        var context = canvas.getContext('2d');
                  
                  // THIS IS A DUMMY ARRAY SAME AS COME IN AJAX RESPONSE  
                          var sources = 
                          {        
                          Slim_Fit: "http://localhost/plugindev/wp-content/uploads/2015/08/slimFit.png",
                          Inside_Colar: "http://localhost/plugindev/wp-content/uploads/2015/08/maincolar.png",
                          Outside_Colar: "http://localhost/plugindev/wp-content/uploads/2015/08/outer_collar1.png",
                          Main_Colar: "http://localhost/plugindev/wp-content/uploads/2015/08/inner_collar11.png"
                          };
                  
                  
                  
                        loadImages(sources, function(images) 
                        {
                  
                          context.drawImage(images.Slim_Fit, canvaswidthdiv4, 55, widthNeeded, canvasheight);  
                          context.drawImage(images.Inside_Colar, canvaswidthdiv4, 55, widthNeeded, canvasheight);
                          context.drawImage(images.Outside_Colar, canvaswidthdiv4, 55, widthNeeded, canvasheight);
                          context.drawImage(images.Main_Colar, canvaswidthdiv4, 55, widthNeeded, canvasheight);
                  
                        });
                  

                  以下是我对功能使用但不起作用的修正

                   loadImages(sources, function(images) 
                    {
                  jQuery.each( sources, function( key, value ) {
                  
                   DrawImage(key, images );
                  
                    });
                  
                    });
                  
                  function DrawImage(keyname,images){
                  
                  context.drawImage(images.keyname, canvaswidthdiv4, 55, widthNeeded, canvasheight);      
                          }
                  

                  以下是我使用循环绘制但效果不佳时的修正

                   loadImages(sources, function(images) 
                    {
                  jQuery.each( sources, function( key, value ) {
                  
                   context.drawImage(images.key, canvaswidthdiv4, 55, widthNeeded, canvasheight);
                  
                    });
                  
                    });
                  

                  请帮忙!

                  推荐答案

                  注意,js 在 Question 出现在第二、第三次将每个图像绘制在先前绘制的图像上到 canvas 上, loadImages.drawImage 的第四个参数?

                  Note, js at Question appear drawing each image over previously drawn image onto canvas at second, third, fourth arguments to .drawImage within loadImages ?

                  loadImages(sources, function(images) 
                        {    
                          context.drawImage(images.Slim_Fit, canvaswidthdiv4, 55, widthNeeded, canvasheight);  
                          context.drawImage(images.Inside_Colar, canvaswidthdiv4, 55, widthNeeded, canvasheight);
                          context.drawImage(images.Outside_Colar, canvaswidthdiv4, 55, widthNeeded, canvasheight);
                          context.drawImage(images.Main_Colar, canvaswidthdiv4, 55, widthNeeded, canvasheight);
                  
                        });
                  

                  还要注意 sources

                  // THIS IS A DUMMY ARRAY SAME AS COME IN AJAX RESPONSE  
                          var sources = 
                          {        
                          Slim_Fit: "http://localhost/plugindev/wp-content/uploads/2015/08/slimFit.png",
                          Inside_Colar: "http://localhost/plugindev/wp-content/uploads/2015/08/maincolar.png",
                          Outside_Colar: "http://localhost/plugindev/wp-content/uploads/2015/08/outer_collar1.png",
                          Main_Colar: "http://localhost/plugindev/wp-content/uploads/2015/08/inner_collar11.png"
                          };
                  

                  是对象,不是数组

                  js 可以缩短为单个 .forEach() 循环;在 .forEach 回调中调用 .drawImage 时根据需要调整 canvas 上的位置

                  js could be shortened to single .forEach() loop ; adjusting position on canvas as needed at call to .drawImage within .forEach callback

                  var canvas = document.getElementById("product-image");
                  /*
                  canvas.height = (jQuery(window).height()) - 120;
                  canvas.width = canvas.height * 0.75;
                  var heightscreen = (jQuery(window).height()) - 120;
                  var canvasheight = heightscreen;
                  var canvaswidth = canvas.height * 0.75;
                  canvaswidthdiv4 = 0;
                  var widthNeeded = canvasheight * 0.75;
                  */
                  
                  var context = canvas.getContext('2d');
                  
                  var images = ["http://lorempixel.com/50/50/cats"
                                , "http://lorempixel.com/50/50/nature"
                                , "http://lorempixel.com/50/50/animals"
                                , "http://lorempixel.com/50/50/sports"
                  ];
                  
                  images.forEach(function(src, index) {
                    var img = new Image;
                    img.onload = function() {
                      context.drawImage(this, index * this.width, index * this.width)
                    }
                    img.src = src
                  })

                  <canvas id="product-image" width="400px" height="400px"></canvas>

                  这篇关于如何从数组中的图像源创建画布图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  quot;Status Code:200 OK (from ServiceWorker)quot; in Chrome Network DevTools?(“状态码:200 OK(来自 ServiceWorker)在 Chrome 网络开发工具中?)
                  How to set a header for a HTTP GET request, and trigger file download?(如何为 HTTP GET 请求设置标头并触发文件下载?)
                  Adding custom HTTP headers using JavaScript(使用 JavaScript 添加自定义 HTTP 标头)
                  What is quot;X-Content-Type-Options=nosniffquot;?(什么是“X-Content-Type-Options=nosniff?)
                  SQL Query DocumentDB in Azure Functions by an integer not working(通过整数在 Azure Functions 中 SQL 查询 DocumentDB 不起作用)
                  Azure Functions [JavaScript / Node.js] - HTTP call, good practices(Azure Functions [JavaScript/Node.js] - HTTP 调用,良好实践)
                  <tfoot id='aTnKF'></tfoot>
                    <tbody id='aTnKF'></tbody>

                    <i id='aTnKF'><tr id='aTnKF'><dt id='aTnKF'><q id='aTnKF'><span id='aTnKF'><b id='aTnKF'><form id='aTnKF'><ins id='aTnKF'></ins><ul id='aTnKF'></ul><sub id='aTnKF'></sub></form><legend id='aTnKF'></legend><bdo id='aTnKF'><pre id='aTnKF'><center id='aTnKF'></center></pre></bdo></b><th id='aTnKF'></th></span></q></dt></tr></i><div id='aTnKF'><tfoot id='aTnKF'></tfoot><dl id='aTnKF'><fieldset id='aTnKF'></fieldset></dl></div>
                  1. <small id='aTnKF'></small><noframes id='aTnKF'>

                      <bdo id='aTnKF'></bdo><ul id='aTnKF'></ul>

                          <legend id='aTnKF'><style id='aTnKF'><dir id='aTnKF'><q id='aTnKF'></q></dir></style></legend>