Javascript画布元素-图像数组
我只是在学习JS,尝试在没有jQuery的情况下做一些事情,我想做一些类似的事情,但是我想使用一个图像数组,而不仅仅是一个图像数组 我的图像数组是这样形成的Javascript画布元素-图像数组,javascript,arrays,loops,canvas,Javascript,Arrays,Loops,Canvas,我只是在学习JS,尝试在没有jQuery的情况下做一些事情,我想做一些类似的事情,但是我想使用一个图像数组,而不仅仅是一个图像数组 我的图像数组是这样形成的 var image_array = new Array() image_array[0] = "image1.jpg" image_array[1] = "image2.jpg" canvas元素是这样写的。(几乎完全取自Mozilla网站) 函数绘图(){ var ctx=document.getElementById('canvas
var image_array = new Array()
image_array[0] = "image1.jpg"
image_array[1] = "image2.jpg"
canvas元素是这样写的。(几乎完全取自Mozilla网站)
函数绘图(){
var ctx=document.getElementById('canvas').getContext('2d');
var img=新图像();
img.src='sample.png';
img.onload=函数(){
对于(i=0;i,只需迭代数组,并使用其宽度和高度属性定位图像:
function draw() {
var ctx = document.getElementById('canvas').getContext('2d'),
img, i, image_array = [];
image_array.push("http://sstatic.net/so/img/logo.png");
image_array.push("http://www.google.com/intl/en_ALL/images/logo.gif");
// ...
for (i = 0; i < image_array.length; i++) {
img = new Image();
img.src = image_array[i];
img.onload = (function(img, i){ // temporary closure to store loop
return function () { // variables reference
ctx.drawImage(img,i*img.width,i*img.height);
}
})(img, i);
}
}
函数draw(){
var ctx=document.getElementById('canvas').getContext('2d'),
img,i,image_数组=[];
图像\u数组。推送(“http://sstatic.net/so/img/logo.png");
图像\u数组。推送(“http://www.google.com/intl/en_ALL/images/logo.gif");
// ...
对于(i=0;i
检查示例。只需迭代数组,并使用其宽度和高度属性定位图像:
function draw() {
var ctx = document.getElementById('canvas').getContext('2d'),
img, i, image_array = [];
image_array.push("http://sstatic.net/so/img/logo.png");
image_array.push("http://www.google.com/intl/en_ALL/images/logo.gif");
// ...
for (i = 0; i < image_array.length; i++) {
img = new Image();
img.src = image_array[i];
img.onload = (function(img, i){ // temporary closure to store loop
return function () { // variables reference
ctx.drawImage(img,i*img.width,i*img.height);
}
})(img, i);
}
}
函数draw(){
var ctx=document.getElementById('canvas').getContext('2d'),
img,i,image_数组=[];
图像\u数组。推送(“http://sstatic.net/so/img/logo.png");
图像\u数组。推送(“http://www.google.com/intl/en_ALL/images/logo.gif");
// ...
对于(i=0;i
检查示例。这些图像可能以错误的顺序显示-在浏览器中加载图像时调用onload方法。小图像可能在大图像之前调用onload,即使它在阵列中稍后出现。这些图像可能以错误的顺序显示-在浏览器中加载图像时调用onload方法。A小映像可能会在大映像之前调用onload,即使它在阵列中稍后出现。