网上百度的,感觉”从中间到两边“的效果写的不是很好,改了一下,感觉可以了!
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript">
image.src="eg_mouse.jpg";
canvas = document.getElementById("canvas1");
context = canvas.getContext("2d");
drawLeft = image.width/2;
function startLeftToRight(){
clearInterval(intervalId1);
context.clearRect(0,0,image.width,image.height);
intervalId1 = setInterval("leftToRight();",50);
context.drawImage(image,0,0,drawW1, image.height,0,0,drawW1,image.height);
clearInterval(intervalId1);
function centerToOuter(){
//drawLeft-drawW1/2是左端点,drawLeft是中间, drawLeft+drawW1/2是右端点
context.drawImage(image,drawLeft-drawW1/2,0,drawW1,image.height,drawLeft-drawW1/2,0,drawW1,image.height);
clearInterval(intervalId2);
function startCenterToOuter(){
clearInterval(intervalId2);
context.clearRect(0,0,image.width,image.height);
intervalId2 = setInterval("centerToOuter();",50);
context.drawImage(image,baiyechuangItemW*i,0,drawW1,image.height,baiyechuangItemW*i,0,drawW1,image.height);
if(drawW1>baiyechuangItemW){
clearInterval(intervalId3);
function startBaiyechuang(){
baiyechuangItemW = image.width/10.0;
clearInterval(intervalId3);
context.clearRect(0,0,image.width,image.height);
intervalId3 = setInterval("baiyechuang();", 50);
<h1><input type="button" value="从左到右装载图片" οnclick="startLeftToRight();"/></h1>
<h1><input type="button" value=“从中间到两边加载图片" οnclick="startCenterToOuter();"/></h1>
<h1><input type="button" value="百叶窗方式装载图片" οnclick="startBaiyechuang();"/></h1>
<canvas id="canvas1" width="400px" height="400px"></canvas>