十年網(wǎng)站開發(fā)經(jīng)驗 + 多家企業(yè)客戶 + 靠譜的建站團(tuán)隊
量身定制 + 運(yùn)營維護(hù)+專業(yè)推廣+無憂售后,網(wǎng)站問題一站解決
html5中如何實現(xiàn)Canvas屏保動畫?這個問題可能是我們?nèi)粘W(xué)習(xí)或工作經(jīng)常見到的。希望通過這個問題能讓你收獲頗深。下面是小編給大家?guī)淼膮⒖純?nèi)容,讓我們一起來看看吧!
話不多說直接上代碼
Title
因為項目需求,該動畫中需要顯示即時更新的數(shù)據(jù),所以和普通的canvas畫出來的不一樣。但是又不能直接把html畫到canvas中去,別著急有辦法。
為了繪制 HTML 內(nèi)容,你要先用
元素包含 HTML 內(nèi)容,然后再將這個 SVG 圖像繪制到你的 canvas 中。
夸張地說,這里唯一真正棘手的事情就是創(chuàng)建 SVG 圖像。您需要做的所有事情是創(chuàng)建一個包含XML 字符串的 SVG,然后根據(jù)下面的幾部分構(gòu)造一個Blob
。
blob 對象的 MIME 應(yīng)為 "image/svg+xml"。
一個元素。
在 SVG 元素中包含的
元素。
包裹到
中的(格式化好的) HTML。
如上所述,通過使用一個 object URL,我們可以內(nèi)聯(lián) HTML 而不是從外部源加載它。當(dāng)然,只要域與原始文檔相同(不跨域),您也可以使用外部源。
//創(chuàng)建畫布 var Cans=document.getElementById("canvas"); var ctx=Cans.getContext("2d"); //設(shè)置全屏畫布 Cans.width=document.body.offsetWidth; Cans.height=document.body.offsetHeight; var DOMURL,img,svg,url; initimg("AAA");//默認(rèn)顯示數(shù)據(jù),一下代碼參考https://developer.mozilla.org/zh-CN/docs/Web/API/Canvas_API/Drawing_DOM_objects_into_a_canvas function initimg(data) { var data = ''; DOMURL = window.URL || window.webkitURL || window; img = new Image(); svg = new Blob([data], {type: 'image/svg+xml;charset=utf-8'}); url = DOMURL.createObjectURL(svg); img.src = url; } //每隔五秒刷新數(shù)據(jù),隨機(jī)從數(shù)組中?。▽嶋H情況當(dāng)然是要從后臺獲?。? var getdata = setInterval(function () { var data=["BBB","CCC","DDD","EEE"] initimg(data[Math.floor(Math.random()*8)]); },5000)
以下便是控制動畫的顯示位置和觸發(fā)動畫及關(guān)閉動畫
var raf; var arror = []; var running = false; //繪制圖形 function createStar() { return { x: 0, y: 0, vx: 0.7, vy: 0.7,//用來控制移動速度 draw: function() { ctx.drawImage(img, this.x, this.y); DOMURL.revokeObjectURL(url); } } } //清除 function clear() { ctx.fillStyle = 'rgba(255,255,255,1)'; ctx.fillRect(0,0,canvas.width,canvas.height); } //判斷圖形坐標(biāo)是否超出畫布范圍 function draw() { clear(); arror.forEach(function(ball, i){ ball.draw(); ball.x += ball.vx; ball.y += ball.vy; if (ball.y + ball.vy+50 > canvas.height || ball.y + ball.vy < 0) { ball.vy = -ball.vy; } if (ball.x + ball.vx+50 > canvas.width || ball.x + ball.vx < 0) { ball.vx = -ball.vx; } }); raf = window.requestAnimationFrame(draw); } canvas.addEventListener('click',function (e) { event.preventDefault(); window.cancelAnimationFrame(raf); if(!running){ Cans.style.display="none" document.onmousemove = document.onkeydown = document.onclick = null; clearTimeout(timer); clearInterval(getdata); clear(); }else{ running = false; bindevent(1); } }) function loadpi() { if (!running) { raf = window.requestAnimationFrame(draw); running = true; } var ball; ball = createStar(); ball.x = canvas.width/2-25; ball.y = canvas.height/2-25; arror.push(ball); document.onmousemove = document.onkeydown = document.onclick = null; clearTimeout(timer); } var timer; function bindevent(it) { clearTimeout(timer); timer = setTimeout(function () { if(it==1){ raf = window.requestAnimationFrame(draw); running = true; }else{ Cans.style.display="block" loadpi(); } }, 3000); } window.onload = document.onmousemove = document.onkeydown = document.onclick = function () { bindevent(); }
感謝各位的閱讀!看完上述內(nèi)容,你們對html5中如何實現(xiàn)Canvas屏保動畫大概了解了嗎?希望文章內(nèi)容對大家有所幫助。如果想了解更多相關(guān)文章內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)網(wǎng)站制作公司行業(yè)資訊頻道。