日期:2014-05-16 浏览次数:20441 次
<canvas id="mycanvas1" width="270" height="270"></canvas> <script type="text/javascript"> var ctx = document.getElementById("mycanvas1").getContext('2d'); var img = new Image(); img.onload = function(){ for(var i = 0; i < 4;i++){ ctx.drawImage(img,0,270*i,270,270,0,0,270,270); } } img.src = 'themes/img/1.png'; </script>
<html> <script> window.onload = function(){ var value = [1,2,3,4]; var index = 0; var timer = setInterval(change, 1000); function change(){ if (index >= value.length) { clearInterval(timer); return; } var div = document.getElementById("d"); div.innerHTML = value[index].toString(); index++; } } </script> <body> <div id="d"></div> </body> </html>
------解决方案--------------------
for(var i = 0; i < 4;i++){
ctx.drawImage(img,0,270*i,270,270,0,0,270,270);
}
var me = this;
for(var i = 0; i < 4;i++){
(function(i){
ctx.drawImage(me , 270*i );
})(i)
}
------解决方案--------------------
<canvas id="mycanvas1" width="500" height="500"></canvas> <script type="text/javascript"> var ctx = document.getElementById("mycanvas1").getContext('2d'); var img = new Image(); var i=0,t; img.onload = function(){t=setInterval("theforever_csdn()",1000); }; img.style.position="absolute"; img.style.top="-1000px"; document.body.appendChild(img).src = 'http://avatar.profile.csdn.net/A/9/7/2_theforever.jpg'; function theforever_csdn(){ ctx.clearRect(0, 0, 500, 500); ctx.drawImage(img,0,0,120,120,i*100,0,120,120); i++;if(i==3) clearInterval(t); } </script>
------解决方案--------------------
for(var i = 0; i < 4;i++){
ctx.drawImage(img,0,270*i,270,270,0,0,270,270);
}
==》
drawImage(img,i);
function drawImage(img,index)
{
ctx.drawImage(img,0,270*index,270,270,0,0,270,270);
index++;
if(index < 4){
setTimeout(function(){drawImage(img,index);});
}
}