隨著一年一度的七夕節(jié)到來,各種文藝活動和特效也開始出現(xiàn)在我們的生活中。而Javascript作為Web前端開發(fā)中最常用的編程語言之一,也有許多與七夕相關(guān)的特效,下面我們就來一一介紹。
首先是“愛的箭頭”特效。這個特效是指當(dāng)用戶點擊頁面上的某個位置時,會出現(xiàn)一支向目標(biāo)位置移動的箭頭,以此來表達(dá)愛情的寓意。具體實現(xiàn)方式為:
var arrow = document.createElement("div"); arrow.style.position = "absolute"; arrow.style.width = "0"; arrow.style.height = "0"; arrow.style.borderWidth = "10px 10px 0"; arrow.style.borderStyle = "solid"; arrow.style.borderColor = "red transparent transparent"; arrow.style.left = startX + "px"; arrow.style.top = startY + "px"; document.body.appendChild(arrow); var distance = Math.sqrt(Math.pow(endX - startX, 2) + Math.pow(endY - startY, 2)); var time = distance / speed; var startTime = new Date().getTime(); var timer = setInterval(function() { var nowTime = new Date().getTime(); var passedTime = nowTime - startTime; var percent = passedTime / time; if (percent >= 1) { arrow.parentNode.removeChild(arrow); clearInterval(timer); } else { var currentX = startX + (endX - startX) * percent; var currentY = startY + (endY - startY) * percent; arrow.style.left = currentX + "px"; arrow.style.top = currentY + "px"; } }, 10);
然后是“千紙鶴”特效。這個特效是指當(dāng)用戶在頁面中某個區(qū)域內(nèi)點擊鼠標(biāo)時,會出現(xiàn)許多飛舞的千紙鶴,以此來凸顯節(jié)日氣氛。實現(xiàn)方式如下:
var canvas = document.createElement("canvas"); canvas.width = container.clientWidth; canvas.height = container.clientHeight; container.appendChild(canvas); var ctx = canvas.getContext("2d"); var cranes = []; canvas.addEventListener("mousedown", function(event) { var x = event.clientX - canvas.offsetLeft; var y = event.clientY - canvas.offsetTop; for (var i = 0; i < 5; i++) { cranes.push({ x: x, y: y, vx: Math.random() * 10 - 5, vy: -10 - Math.random() * 5, life: 100 }); } }); setInterval(function() { ctx.clearRect(0, 0, canvas.width, canvas.height); ctx.fillStyle = "white"; for (var i = 0; i < cranes.length; i++) { var crane = cranes[i]; crane.x += crane.vx; crane.y += crane.vy; crane.vy += 0.2; crane.life -= 1; if (crane.life <= 0) { cranes.splice(i, 1); i--; } else { ctx.beginPath(); ctx.moveTo(crane.x, crane.y); ctx.lineTo(crane.x + 20, crane.y + 20); ctx.lineTo(crane.x + 40, crane.y); ctx.closePath(); ctx.fill(); } } }, 30);
最后是“星空背景”特效。這個特效是指當(dāng)用戶進入頁面后,會出現(xiàn)一片璀璨的星空背景,以此來營造浪漫的感覺。具體實現(xiàn)方式為:
var canvas = document.createElement("canvas"); canvas.width = container.clientWidth; canvas.height = container.clientHeight; container.appendChild(canvas); var ctx = canvas.getContext("2d"); var stars = []; for (var i = 0; i < 100; i++) { stars.push({ x: Math.random() * canvas.width, y: Math.random() * canvas.height, r: Math.random() * 2 + 1 }); } setInterval(function() { ctx.clearRect(0, 0, canvas.width, canvas.height); ctx.fillStyle = "white"; for (var i = 0; i < stars.length; i++) { var star = stars[i]; star.x += Math.random() * 4 - 2; star.y += Math.random() * 4 - 2; if (star.x < -star.r || star.x > canvas.width + star.r || star.y < -star.r || star.y > canvas.height + star.r) { star.x = Math.random() * canvas.width; star.y = Math.random() * canvas.height; } ctx.beginPath(); ctx.arc(star.x, star.y, star.r, 0, Math.PI * 2); ctx.fill(); } }, 30);
總之,Javascript能夠?qū)崿F(xiàn)許多與七夕節(jié)相關(guān)的特效,幫助我們在頁面上表達(dá)自己的情感與祝福。在Web前端開發(fā)中,我們相信還會有更多的有趣特效出現(xiàn),讓我們一起期待吧。