±à¼ÍƼö: |
±¾ÎÄÀ´×ÔÓÚjqueryÖ®¼Ò
,html5ÖÆ×÷canvas¶¯»µÄ»ù±¾²½Ö裬¿ØÖÆcanvas¶¯»ºÍʵÀý´úÂë¡£ |
|
ÒªÔÚ HTML5 canvas ÖлæÖÆÍ¼Ïñ¶¯»Ð§¹û£¬ÄãÐèÒª»æÖƳöÿһ֡µÄͼÏñ£¬È»ºóÔÚÒ»¸ö¼«¶ÌµÄʱ¼äÄÚ´ÓÒ»Ö¡¹ý¶Éµ½ÏÂÒ»Ö¡£¬Ðγɶ¯»Ð§¹û¡£ÕâÆäʵÊÇÒ»ÖÖÊÓ¾õÆÛÆ£¬ÔÀí¾ÍÏñ²¥·ÅµçÓ°Ò»Ñù£¬½ºÆ¬ÉÏÿһ¸ñÊÇһ֡ͼƬ£¬È»ºó¿ìËٵIJ¥·ÅËüÃÇ£¬ÔÚÈ˵ÄÑÛ¾¦¿´À´¾ÍÊÇÒ»¸öÍêÕûµÄ¶¯»Ð§¹û¡£
ÖÆ×÷canvas¶¯»µÄ»ù±¾²½Öè
ÏÂÃæÊÇÄãÔÚcanvasÉÏ»æÖÆÒ»¸ö¶¯»Ö¡µÄ»ù±¾²½Ö裺
- 1¡¢Çå¿Õcanvas£º³ýÁ˱³¾°Í¼ÏñÖ®Í⣬ÄãÐèÒªÇå¿Õ֮ǰ»æÖƵÄËùÓÐͼÐΡ£
- 2¡¢±£´æcanvasµÄ״̬£ºÈç¹ûÔÚÕâÒ»²½ÖÐÄãʹÓÃÁ˲»Í¬µÄ»æÍ¼×´Ì¬£¨ÀýÈçÃè±ß´óСºÍÌî³äÉ«µÈ£©£¬²¢ÇÒÄãÏëÔÚ»æÖÆÃ¿Ò»Ö¡Ê±Ê¹ÓÃÏàͬµÄÔʼ״̬£¬ÄãÐèÒª±£´æÕâЩÔʼ״̬¡£
- 3¡¢»æÖƶ¯»Í¼ÐΣºÕâÒ»²½ÖÐÄãÐèÒª»æÖÆÄÇЩ¶¯»µÄͼÐÎÔªËØ¡£
- 4¡¢»Ö¸´canvas״̬£ºÈç¹ûÄã֮ǰ±£´æ¹ýcanvasµÄ״̬£¬ÔÚÕâÒ»²½Öн«ËüÃǻָ´¡£
¿ØÖÆcanvas¶¯»
ÎÒÃÇÐèÒªÒ»ÖÖ·½·¨À´ÔÚÖ¸¶¨Ê±¼äÄÚÖ´ÐÐÎÒÃǵĻæÖÆÍ¼Ðκ¯Êý¡£ÓÐÁ½ÖÖ·½Ê½¿ÉÒÔ¿ØÖƶ¯»µÄÖ´ÐС£
µÚÒ»ÖÖÊÇʹÓÃÏÂÃæµÄÈý¸öwindow¶ÔÏóÉϵķ½·¨£ºwindow.setInterval()£¬window.setTimeout()ºÍwindow.requestAnimationFrame()¡£ËüÃǶ¼ÄÜÔÚÖ¸¶¨Ê±¼äÄÚµ÷ÓÃÖ¸¶¨µÄº¯Êý¡£
- setInterval(function, delay)£ºÔÚÿdelayºÁÃëʱ¼äÄÚ·´¸´Ö´ÐÐfunctionÖ¸¶¨µÄº¯Êý¡£
- setTimeout(function, delay)£ºÔÚdelayºÁÃëÄÚÖ´ÐÐfunctionÖ¸¶¨µÄº¯Êý¡£
- requestAnimationFrame(callback)£ºÍ¨Öªä¯ÀÀÆ÷ÄãÐèÒªÖ´ÐÐÒ»¸ö¶¯»£¬²¢ÇëÇóä¯ÀÀÆ÷µ÷ÓÃÖ¸¶¨µÄº¯ÊýÀ´ÔÚÏÂÒ»´ÎÖØ»æÇ°¸üж¯»¡£
µÚ¶þÖÖ·½·¨ÊÇʹÓÃʼþ¼àÌý¡£ÀýÈçÄãÐèÒª×öÒ»¸öСÓÎÏ·£¬Äã¿ÉÒÔ¼àÌý¼üÅ̺ÍÊó±êµÄʼþ£¬È»ºóÔÚ²¶»ñÏàÓ¦µÄʼþʱʹÓÃsetTimeout()·½·¨À´ÖÆ×÷¶¯»Ð§¹û¡£
ΪÁË»ñµÃ¸üºÃµÄ¶¯»ÐÔÄÜ£¬ÎÒÃÇͨ³£Ê¹ÓÃrequestAnimationFrame()·½·¨¡£µ±ä¯ÀÀÆ÷×°±¸ºÃ»æÖÆÏÂÒ»Ö¡¶¯»Ê±£¬ÎÒÃÇ¿ÉÒÔ½«»æÖƺ¯Êý×÷Ϊ²ÎÊý´«ÈëÕâ¸ö·½·¨ÖС£
ͨ¹ýÔÚä¯ÀÀÆ÷×¼±¸»ÏÂÒ»Ö¡µÄʱºò£¬¸øä¯ÀÀÆ÷·¢³öÐźţ¬¿ÉÒÔʹä¯ÀÀÆ÷¶ÔÄãµÄ¶¯»½øÐÐÓ²¼þ¼ÓËÙ£¬Õâ±ÈʹÓÃsetTimeout()À´»æÖƶ¯»Ð§¹û»áºÃµÃ¶à¡£
ÏÂÃæÊÇÒ»¶ÎʾÀý´úÂ룺
function animate() {
reqAnimFrame = window.mozRequestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.msRequestAnimationFrame ||
window.oRequestAnimationFrame
;
reqAnimFrame(animate);
draw();
} |
animate()º¯ÊýÊ×ÏÈ»á»ñÈ¡requestAnimationFrame()º¯ÊýµÄÒ»¸öÒýÓá£×¢ÒâÔÚ²»Í¬µÄä¯ÀÀÆ÷ÖлáʹÓò»Í¬µÄÃû³Æ¡£±äÁ¿reqAnimFrame»áÔÚ²»Í¬µÄä¯ÀÀÆ÷ÖÐÉèÖÃΪ²»Í¬µÄÖµ£¬×ÜÖ®Ëü²»ÄÜΪ¿Õ¡£
È»ºóreqAnimFrame()·½·¨±»µ÷Ó㬲¢½«animate()º¯Êý×÷Ϊ²ÎÊý´«Èë¡£µ±ä¯ÀÀÆ÷×¼±¸ºÃ»æÖÆÏÂÒ»Ö¡¶¯»Ê±£¬animate()º¯Êý¾Í»á±»µ÷Óá£
×îºó£¬animate()º¯Êý»áµ÷ÓÃdraw()·½·¨¡£draw()·½·¨ÔÚÉÏÃæµÄ´úÂëÖÐûÓÐд³öÀ´£¬Ëüʵ¼ÊÉÏ×öµÄÊÂÇé¾ÍÊÇÇ°ÃæÌáµ½µÄ»æÖÆÒ»¸ö¶¯»Ö¡µÄ4¸ö²½Ö裺Çå¿Õcanvas£¬±£´æ×´Ì¬£¬»æÖÆÍ¼ÐΣ¬»Ö¸´×´Ì¬¡£
»¹ÓÐÒ»¼þÐèҪעÒâµÄÊÂÇéÊÇanimate()º¯Êý±ØÐë±»µ÷ÓÃÒ»´ÎÀ´Æô¶¯¶¯»£¬·ñÔòrequestAnimationFrame()º¯Êý½«ÓÀÔ¶²»»á±»µ÷Ó㬶¯»Ò²²»»á±»Õý³£Ö´ÐС£
ÏÂÃæÊÇÒ»¸öСÀý×Ó£ºÒ»¸öС¾ØÐÎÔÚcanvasÖÐÀ´»Ø²»Í£µÄÔ˶¯¡£
ÉÏÃæcanvas¶¯»µÄʵÏÖ´úÂëÈçÏ£º
var x = 0;
var y = 15;
var speed = 5;
function animate() {
reqAnimFrame = window.mozRequestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.msRequestAnimationFrame ||
window.oRequestAnimationFrame
;
reqAnimFrame(animate);
x += speed;
if(x <= 0 || x >= 475){
speed = -speed;
}
draw();
}
function draw() {
var canvas = document.getElementById("ex1");
var context = canvas.getContext("2d");
context.clearRect(0, 0, 500, 170);
context.fillStyle = "#ff00ff";
context.fillRect(x, y, 25, 25);
}
animate(); |
canvas¶¯»Ê¾Àý
ÏÂÃæÊÇÒ»¸öµØÇòÈÆÌ«ÑôÒÔ¼°ÔÂÁÁÈÆµØÇòÐýתµÄcanvas¶¯»Ð§¹û¡£
ÉÏÃæÐ§¹ûµÄʵÏÖ´úÂëÈçÏ£º
var sun = new Image();
var moon = new Image();
var earth = new Image();
function init(){
sun.src = 'img/Canvas_sun.png';
moon.src = 'img/Canvas_moon.png';
earth.src = 'img/Canvas_earth.png';
reqAnimFrame = window.mozRequestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.msRequestAnimationFrame ||
window.oRequestAnimationFrame
;
reqAnimFrame(drawSolarSystem);
}
function drawSolarSystem () {
var ctx = document.getElementById('ex2').getContext('2d');
ctx.globalCompositeOperation = 'destination-over';
ctx.clearRect(0,0,300,300); // clear canvas
ctx.fillStyle = 'rgba(0,0,0,0.4)';
ctx.strokeStyle = 'rgba(0,153,255,0.4)';
ctx.save();
ctx.translate(150,150);
// Earth
var time = new Date();
ctx.rotate( ((2*Math.PI)/60)*time.getSeconds() + ((2*Math.PI)/60000)*time.getMilliseconds() );
ctx.translate(105,0);
ctx.fillRect(0,-12,50,24); // Shadow
ctx.drawImage(earth,-12,-12);
// Moon
ctx.save();
ctx.rotate( ((2*Math.PI)/6)*time.getSeconds() + ((2*Math.PI)/6000)*time.getMilliseconds() );
ctx.translate(0,28.5);
ctx.drawImage(moon,-3.5,-3.5);
ctx.restore();
ctx.restore();
ctx.beginPath();
ctx.arc(150,150,105,0,Math.PI*2,false); // Earth orbit
ctx.stroke();
ctx.drawImage(sun,0,0,300,300);
reqAnimFrame = window.mozRequestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.msRequestAnimationFrame ||
window.oRequestAnimationFrame
;
reqAnimFrame(drawSolarSystem);
}
init(); |
½ÓÏÂÀ´µÄÀý×ÓÊÇÒ»¸öÄ£ÄâʱÖÓ¶¯»µÄÀý×Ó¡£
Ä£ÄâʱÖÓ¶¯»µÄʵÏÖ´úÂëÈçÏ£º
function clock(){
var now = new Date();
var ctx = document.getElementById('ex3').getContext('2d');
ctx.save();
ctx.clearRect(0,0,150,150);
ctx.translate(75,75);
ctx.scale(0.4,0.4);
ctx.rotate(-Math.PI/2);
ctx.strokeStyle = "black";
ctx.fillStyle = "white";
ctx.lineWidth = 8;
ctx.lineCap = "round";
// Hour marks
ctx.save();
for (var i=0;i<12;i++){
ctx.beginPath();
ctx.rotate(Math.PI/6);
ctx.moveTo(100,0);
ctx.lineTo(120,0);
ctx.stroke();
}
ctx.restore();
// Minute marks
ctx.save();
ctx.lineWidth = 5;
for (i=0;i<60;i++){
if (i%5!=0) {
ctx.beginPath();
ctx.moveTo(117,0);
ctx.lineTo(120,0);
ctx.stroke();
}
ctx.rotate(Math.PI/30);
}
ctx.restore();
var sec = now.getSeconds();
var min = now.getMinutes();
var hr = now.getHours();
hr = hr>=12 hr-12 : hr;
ctx.fillStyle = "black";
// write Hours
ctx.save();
ctx.rotate( hr*(Math.PI/6) + (Math.PI/360)*min + (Math.PI/21600)*sec )
ctx.lineWidth = 14;
ctx.beginPath();
ctx.moveTo(-20,0);
ctx.lineTo(80,0);
ctx.stroke();
ctx.restore();
// write Minutes
ctx.save();
ctx.rotate( (Math.PI/30)*min + (Math.PI/1800)*sec )
ctx.lineWidth = 10;
ctx.beginPath();
ctx.moveTo(-28,0);
ctx.lineTo(112,0);
ctx.stroke();
ctx.restore();
// Write seconds
ctx.save();
ctx.rotate(sec * Math.PI/30);
ctx.strokeStyle = "#D40000";
ctx.fillStyle = "#D40000";
ctx.lineWidth = 6;
ctx.beginPath();
ctx.moveTo(-30,0);
ctx.lineTo(83,0);
ctx.stroke();
ctx.beginPath();
ctx.arc(0,0,10,0,Math.PI*2,true);
ctx.fill();
ctx.beginPath();
ctx.arc(95,0,10,0,Math.PI*2,true);
ctx.stroke();
ctx.fillStyle = "rgba(0,0,0,0)";
ctx.arc(0,0,3,0,Math.PI*2,true);
ctx.fill();
ctx.restore();
ctx.beginPath();
ctx.lineWidth = 14;
ctx.strokeStyle = '#325FA2';
ctx.arc(0,0,142,0,Math.PI*2,true);
ctx.stroke();
ctx.restore();
reqAnimFrame = window.mozRequestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.msRequestAnimationFrame ||
window.oRequestAnimationFrame
;
reqAnimFrame(clock);
}
reqAnimFrame = window.mozRequestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.msRequestAnimationFrame ||
window.oRequestAnimationFrame
;
reqAnimFrame(clock); |
|