简体   繁体   English

如何通过javascript在HTML5画布中应用图像宽度

[英]how to apply width of image in HTML5 canvas via javascript

Here is my script: 这是我的脚本:

var img = new Image();
img.onload = function() {
    resourceCache[url] = img;
    readyCallbacks.forEach(function(func) {
         func();
    }
};

This script binds images into a canvas. 该脚本将图像绑定到画布中。 I want to know how to set width for the image. 我想知道如何设置图像的宽度。 I couldn't understand the meaning of readyCallbacks.forEach(function(func) { func(); } 我不明白readyCallbacks.forEach(function(func){func();}的含义

Full code: 完整代码:

(function() {
var resourceCache = {};
var loading = [];
var readyCallbacks = [];

// Load an image url or an array of image urls
function load(urlOrArr) {
    if(urlOrArr instanceof Array) {
        urlOrArr.forEach(function(url) {
            _load(url);
        });
    }
    else {
        _load(urlOrArr);
    }
}

function _load(url) {
    if(resourceCache[url]) {
        return resourceCache[url];
    }
    else {
        var img = new Image();
        img.onload = function() {
            //img["width"] = "101";
            resourceCache[url] = img;

            if(isReady()) {
                readyCallbacks.forEach(function(func) {
                    func();
                });
            }
        };
        resourceCache[url] = false;
        img.src = url;
    }
}

function get(url) {
    return resourceCache[url];
}

function isReady() {
    var ready = true;
    for(var k in resourceCache) {
        if(resourceCache.hasOwnProperty(k) &&
           !resourceCache[k]) {
            ready = false;
        }
    }
    return ready;
}

function onReady(func) {
    readyCallbacks.push(func);
}

window.resources = { 
    load: load,
    get: get,
    onReady: onReady,
    isReady: isReady
};
})();

jsFiddle : https://jsfiddle.net/evq6ac0e/2/ jsFiddle: https ://jsfiddle.net/evq6ac0e/2/

var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");

var img = new Image();

//img.src = URL here;

img.onload = function() {
  ctx.fillStyle = "#000";
  ctx.fillRect(0,0,c.width,c.height);
  c.width = img.width;
  c.height = img.height;
  ctx.drawImage(img,0,0);
};

Just target your canvas and set the width and height to the image, if you just want to set the width then remove the line c.height = img.height; 只需定位画布并为图像设置宽度和高度,如果您只想设置宽度,则删除行c.height = img.height; and vice-versa for height 反之亦然

Part 2 第2部分

As far as explaining what readyCallbacks.forEach(function(func) { func(); } does: it simply iterates the array of functions and calls each function in the array. A callback function gets added to readyCallbacks each time onReady(func) is called. 就解释readyCallbacks.forEach(function(func) { func(); }作用)而言:它readyCallbacks.forEach(function(func) { func(); }数组进行迭代并调用该数组中的每个函数。每次onReady(func)被添加到readyCallbacks时,都会添加一个回调函数。叫。

The upshot of all this is that the code waits for all images to be loaded before calling all the functions registered via onRead(func) 所有这些的结果是代码在调用通过onRead(func)注册的所有函数之前,等待所有图像加载。

jsFiddle (with comments added) : https://jsfiddle.net/CanvasCode/arkjge2o/ jsFiddle(添加注释): https ://jsfiddle.net/CanvasCode/arkjge2o/

(function() {
  var resourceCache = {};
  var loading = [];
  var readyCallbacks = [];

  // Load an image url or an array of image urls
  function load(urlOrArr) {
    if (urlOrArr instanceof Array) {
      urlOrArr.forEach(function(url) {
        _load(url);
      });
    } else {
      _load(urlOrArr);
    }
  }

  function _load(url) {
    if (resourceCache[url]) {
      return resourceCache[url];
    } else {

      // Create a new img variable
      var img = new Image();
      img.onload = function() {
        // Store the new image object
        resourceCache[url] = img;

        // Check to see if all images are loaded
        if (isReady()) {
          // Calls all the callbacks in the array
          readyCallbacks.forEach(function(func) {
            func();
          });
        }
      };

      resourceCache[url] = false;

      // Start the image loading
      img.src = url;
    }
  }

  function get(url) {
    return resourceCache[url];
  }

  function isReady() {
    var ready = true;
    // For each image inside of resourceCache
    for (var k in resourceCache) {
    // See if any of the images is not an image
    // resourceCache.hasOwnProperty(k) will always return true
      if (/*resourceCache.hasOwnProperty(k) &&*/
        !resourceCache[k]) {
        ready = false;
      }
    }
    return ready;
  }

  function onReady(func) {
    readyCallbacks.push(func);
  }

  window.resources = {
    load: load,
    get: get,
    onReady: onReady,
    isReady: isReady
  };
})();

Try using dot syntax to set the dimensions of the image and adding px to the end of your value string, as in the example below: 尝试使用点语法设置图像的尺寸,并在值字符串的末尾添加px,如下例所示:

 function _load(url) { if(resourceCache[url]) { return resourceCache[url]; } else { var img = new Image(); img.onload = function() { img.width = "101px"; resourceCache[url] = img; if(isReady()) { readyCallbacks.forEach(function(func) { func(); }); } }; resourceCache[url] = false; img.src = url; } } 

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

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