1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
| var _W = window.innerWidth, _H = window.innerHeight, _speed = 1000 / 10, loopTimer = null, canvasObj = null, context = null, imageList = [], animateList = [], animateAction = 0, animateCurframe = 0; main(); function main() { setCanvas("donglegend", _W, _H); loadResource({ 'id': 'dongsheng', 'src': 'images/dongsheng.jpg' }); }
function setCanvas(id, w, h) { var object = document.getElementById(id); object.innerHTML = '<canvas id="' + id + '_canvas" width="' + _W +'" height="'+_H+'">' + '</canvas>'; canvasObj = document.getElementById(id + "_canvas"); context = canvasObj.getContext("2d"); }
function loadResource(item){ var image = new Image(); image.onload = function() { imageList[item.id] = image; animateList = divideCoordinate(image.width, image.height, 2, 4); loop(); } image.src = item.src; }
function divideCoordinate(w, h, row, col) { var i, j, cw = w / col, ch = h / row, r = [], c; for (i = 0; i < row; i++) { c = []; for (j = 0; j < col; j++) { c.push({x : cw * j, y : ch * i, width : cw, height : ch}); } r.push(c); } return r; };
function loop() { if (loopTimer) { clearInterval(loopTimer); } loopTimer = setInterval(function() { show(); }, _speed) }
function show() { context.fillRect(0, 0, _W, _H); draw(context, imageList['dongsheng'], animateList); }
function draw(c, bitmapData, list) { if(animateCurframe >= list[animateAction].length-1){ animateCurframe = 0; animateAction++; if(animateAction > list.length-1){ animateAction = 0; } }else{ animateCurframe++; } var p = list[animateAction][animateCurframe]; c.drawImage( bitmapData, p.x, p.y, p.width, p.height, (_W - p.width)/2, (_H - p.height)/3, p.width, p.height ) }
|