ä¸é¢è¯¦ç»è§£éé½å¨æºç ä¸:
<!doctype html><html><head><meta charset="utf-8"><title>æ æ é¢ææ¡£</title><style>#canvas{ background:#eee;}</style></head><body><canvas id='canvas' width="500" height='500'></canvas><script>window.onload=function(){ var canvas = document.getElementById('canvas'); var context = canvas.getContext('2d'); //平移,主è¦æ¯å°åæ 轴平移å°ä¸é´,为äºç»åå®ä½æ¹ä¾¿ context.translate(250,250); //å®ä¹ç¦è· var fos = 300; //åæ¾å°ççæ°ç» var arr = []; for(var i = 0 ; i < 8 ; i++){ var arcObj = { //åå¾,ç¨éæºæ°ç®çæ¯è®©æ¯ä¸ªå°ç大å°ä¸ä¸ r:10+5*Math.random(), //èµ·å§Xåæ x:-200+i*30, //èµ·å§Yåæ y:-100+200*Math.random(), //èµ·å§Zåæ ,è¿ééè¦ç解,æ们è¦æé çæ¯ä¸ä¸ªä¸ç»´ç«ä½å°ççè¿å¨ //åX,Yè½´ä¸è½è¡¨è¾¾ç©ºé´æ,ä½ éè¦å
åçæ³è±¡Zåæ è½´æ¯é¡ºçä½ çç¼ççå°±æ¯Zè½´ z:i*10, //å°ççè¿è¡é度 speed:20 } arr.push(arcObj); } setInterval(function(){ //æ¸
é¤ç»å¸,æ¯æ¬¡ç»ä¹åå
å°ä¸æ¬¡çæ¸
é¤æ.ç¶åç»åºæ¬æ¬¡ç,å°±å¯ä»¥å½¢æå¨ç»ææ. context.clearRect(-250,-250,500,500); //å°arræåº,sort()çåæ°åæ¯ä½ä¸ºä¸ç§æ¯è¾è§å var newArr = arr.sort(function (a,b){ return a.z > b.z }); //循ç¯ç»åºåæå®ä¹çå 个å°ç for(var i = 0 ; i < newArr.length ; i++){ //zè½´çåå,æ¯æ¬¡ååé½æ¯ é度*æ¶é´+z = z;ç±äºspeedæªå®ä¹åä½,åæ¶é´å¯å¿½ç¥ arr[i].z += arr[i].speed; //让å°çæ¥å弹跳 if(arr[i].z > 600 || arr[i].z < -50){ arr[i].speed *= -1; } //è¿éç缩æ¾æ¯ä¾,ä¸å®è¦æ³¨æ,ä½ è¦æ³è±¡ä½ ç¼åæ个çåç´ä»è¿å¤é£æ¥,éæ¸å大çè¿ç¨,Zè½´ä¸æå¢å .ç¦è·å°±æ³è±¡æä»æåä½ å°ççè·ç¦»,éè¿è¿å¨å,ç°å¨å°ççè·ç¦»åç¦è·å°±å¯ä»¥å½¢æ缩æ¾æ¯ä¾. var scales = fos/(fos+arr[i].z); var x1 = arr[i].x*scales; var y1 = arr[i].y*scales; //ä¿åä¹åçcontextç»å¾ç¯å¢,å³åç»å¯ä»¥ç¨context.restoreæ¹æ³å¯ä»¥æ¢å¤, //ç®çæ¯è®©ä¸é¢çcontextååä¸å½±åå
¶ä»çç»å¾æ ·å¼. context.save(); //平移X,Y ä¹å¯ä»¥ä¸ç¨å¹³ç§»X,Yåªè¦å¨ä¸é¢çç»åä¸å®ä¹ç¸åºçX,Yä¹è½è¾¾å°ç¸åçç®ç context.translate(x1,y1); //å°åæ 轴缩æ¾,ç®çæ¯è®©å°çç大å°åçè§è§ä¸çåå. context.scale(scales,scales); context.beginPath(); //å®ä¹æ¾å°æ§é¢è²æ¸å var colorObj = context.createRadialGradient(0,0,0,0,0,arr[i].r); colorObj.addColorStop(0,'#cbc0f3'); colorObj.addColorStop(1,'#06198b'); context.fillStyle=colorObj; context.arc(0,0,arr[i].r,0,Math.PI*2); context.fill(); context.restore(); } },50);}</script></body></html>
主è¦è¦ç解ç¦è·çæ¦å¿µ,å®é
å¼åè¿ç¨ä¸,å¯è½Xè½´,Yè½´é½æå°ççè¿å¨é度åé,é£æè½å¨è¿å¨çè¿ç¨ä¸è½¬å¼¯,æå¢çç¹æ.
温馨提示:答案为网友推荐,仅供参考