簡短教學
這是一款使用HTML5 Canvas製作的黑板特效,該黑板特效支援手機移動端,它能模擬使用粉筆在黑板上寫字的效果。此黑板特效的特點還有:
使用滑鼠左鍵能夠在黑板上寫字。
使用滑鼠右鍵能夠擦除已寫的字。
按空白鍵可以清空黑板上的內容物。
點擊下載按鈕可以將寫入的內容儲存為圖片並下載。
使用方法
JavaScript
該HTML5 Canvas黑板特效的完整js程式碼如下:
$(document).ready(chalkboard); function chalkboard(){ $('#chalkboard').remove(); $('.chalk').remove(); $('body').prepend('<div class="panel"><a class="link" target="_blank">Save</a></div>'); $('body').prepend('<img src="/static/imghwm/default1.png" data-src="img/bg.png" class="lazy" id="pattern" style="max-width:90%"支援行動端的HTML5 Canvas逼真黑板特效" >'); $('body').prepend('<canvas id="chalkboard"></canvas>'); $('body').prepend('<div class="chalk"></div>'); var canvas = document.getElementById("chalkboard"); $('#chalkboard').css('width',$(window).width()); $('#chalkboard').css('height',$(window).height()); canvas.width = $(window).width(); canvas.height = $(window).height(); var ctx = canvas.getContext("2d"); var width = canvas.width; var height = canvas.height; var mouseX = 0; var mouseY = 0; var mouseD = false; var eraser = false; var xLast = 0; var yLast = 0; var brushDiameter = 7; var eraserWidth = 50; var eraserHeight = 100; $('#chalkboard').css('cursor','none'); document.onselectstart = function(){ return false; }; ctx.fillStyle = 'rgba(255,255,255,0.5)'; ctx.strokeStyle = 'rgba(255,255,255,0.5)'; ctx.lineWidth = brushDiameter; ctx.lineCap = 'round'; var patImg = document.getElementById('pattern'); document.addEventListener('touchmove', function(evt) { var touch = evt.touches[0]; mouseX = touch.pageX; mouseY = touch.pageY; if (mouseY < height && mouseX < width) { evt.preventDefault(); $('.chalk').css('left', mouseX + 'px'); $('.chalk').css('top', mouseY + 'px'); //$('.chalk').css('display', 'none'); if (mouseD) { draw(mouseX, mouseY); } } }, false); document.addEventListener('touchstart', function(evt) { //evt.preventDefault(); var touch = evt.touches[0]; mouseD = true; mouseX = touch.pageX; mouseY = touch.pageY; xLast = mouseX; yLast = mouseY; draw(mouseX + 1, mouseY + 1); }, false); document.addEventListener('touchend', function(evt) { mouseD = false; }, false); $('#chalkboard').css('cursor','none'); ctx.fillStyle = 'rgba(255,255,255,0.5)'; ctx.strokeStyle = 'rgba(255,255,255,0.5)'; ctx.lineWidth = brushDiameter; ctx.lineCap = 'round'; $(document).mousemove(function(evt){ mouseX = evt.pageX; mouseY = evt.pageY; if(mouseY<height && mouseX<width){ $('.chalk').css('left',(mouseX-0.5*brushDiameter)+'px'); $('.chalk').css('top',(mouseY-0.5*brushDiameter)+'px'); if(mouseD){ if(eraser){ erase(mouseX,mouseY); }else{ draw(mouseX,mouseY); } } }else{ $('.chalk').css('top',height-10); } }); $(document).mousedown(function(evt){ mouseD = true; xLast = mouseX; yLast = mouseY; if(evt.button == 2){ erase(mouseX,mouseY); eraser = true; $('.chalk').addClass('eraser'); }else{ if(!$('.panel').is(':hover')){ draw(mouseX+1,mouseY+1); } } }); $(document).mouseup(function(evt){ mouseD = false; if(evt.button == 2){ eraser = false; $('.chalk').removeClass('eraser'); } }); $(document).keyup(function(evt){ if(evt.keyCode == 32){ ctx.clearRect(0,0,width,height); layPattern(); } }); $(document).keyup(function(evt){ if(evt.keyCode == 83){ changeLink(); } }); document.oncontextmenu = function() {return false;}; function draw(x,y){ ctx.strokeStyle = 'rgba(255,255,255,'+(0.4+Math.random()*0.2)+')'; ctx.beginPath(); ctx.moveTo(xLast, yLast); ctx.lineTo(x, y); ctx.stroke(); // Chalk Effect var length = Math.round(Math.sqrt(Math.pow(x-xLast,2)+Math.pow(y-yLast,2))/(5/brushDiameter)); var xUnit = (x-xLast)/length; var yUnit = (y-yLast)/length; for(var i=0; i<length; i++ ){ var xCurrent = xLast+(i*xUnit); var yCurrent = yLast+(i*yUnit); var xRandom = xCurrent+(Math.random()-0.5)*brushDiameter*1.2; var yRandom = yCurrent+(Math.random()-0.5)*brushDiameter*1.2; ctx.clearRect( xRandom, yRandom, Math.random()*2+2, Math.random()+1); } xLast = x; yLast = y; } function erase(x,y){ ctx.clearRect (x-0.5*eraserWidth,y-0.5*eraserHeight,eraserWidth,eraserHeight); } $('.link').click(function(evt){ $('.download').remove(); var imgCanvas = document.createElement('canvas'); var imgCtx = imgCanvas.getContext("2d"); var pattern = imgCtx.createPattern(patImg,'repeat'); imgCanvas.width = width; imgCanvas.height = height; imgCtx.fillStyle = pattern; imgCtx.rect(0,0,width,height); imgCtx.fill(); var layimage = new Image; layimage.src = canvas.toDataURL("image/png"); setTimeout(function(){ imgCtx.drawImage(layimage,0,0); var compimage = imgCanvas.toDataURL("image/png");//.replace('image/png','image/octet-stream'); $('.panel').append('<a href="'+compimage+'" download="chalkboard.png" class="download">Download</a>'); $('.download').click(function(){ IEsave(compimage); }); }, 500); }); function IEsave(ctximage){ setTimeout(function(){ var win = window.open(); $(win.document.body).html('<img src="/static/imghwm/default1.png" data-src="'+ctximage+'" class="lazy" name="chalkboard.png" alt="支援行動端的HTML5 Canvas逼真黑板特效" >'); },500); } $(window).resize(function(){ chalkboard(); }); }
上述網支援手機端支援端的更多逼真內容 CanvasreeeHT799相關網數的逼真內容,更多內容支援手機端(www.php.cn)!

HTML5代碼由標籤、元素和屬性組成:1.標籤定義內容類型,用尖括號包圍,如。 2.元素由開始標籤、內容和結束標籤組成,如內容。 3.屬性在開始標籤中定義鍵值對,增強功能,如。這些是構建網頁結構的基本單位。

HTML5是構建現代網頁的關鍵技術,提供了許多新元素和功能。 1.HTML5引入了語義化元素如、、等,增強了網頁結構和SEO。 2.支持多媒體元素和,無需插件即可嵌入媒體。 3.表單增強了新輸入類型和驗證屬性,簡化了驗證過程。 4.提供了離線和本地存儲功能,提升了網頁性能和用戶體驗。

H5代碼的最佳實踐包括:1.使用正確的DOCTYPE聲明和字符編碼;2.採用語義化標籤;3.減少HTTP請求;4.使用異步加載;5.優化圖像。這些實踐能提升網頁的效率、可維護性和用戶體驗。

Web标准和技术从HTML4、CSS2和简单的JavaScript演变至今,经历了显著的发展。1)HTML5引入了Canvas、WebStorage等API,增强了Web应用的复杂性和互动性。2)CSS3增加了动画和过渡功能,使页面效果更加丰富。3)JavaScript通过Node.js和ES6的现代化语法,如箭头函数和类,提升了开发效率和代码可读性,这些变化推动了Web应用的性能优化和最佳实践的发展。

H5不僅僅是HTML5的簡稱,它代表了一個更廣泛的現代網頁開發技術生態:1.H5包括HTML5、CSS3、JavaScript及相關API和技術;2.它提供更豐富、互動、流暢的用戶體驗,能在多設備上無縫運行;3.使用H5技術棧可以創建響應式網頁和復雜交互功能。

H5與HTML5指的是同一個東西,即HTML5。 HTML5是HTML的第五個版本,帶來了語義化標籤、多媒體支持、畫布與圖形、離線存儲與本地存儲等新功能,提升了網頁的表現力和交互性。

H5referstoHTML5,apivotaltechnologyinwebdevelopment.1)HTML5introducesnewelementsandAPIsforrich,dynamicwebapplications.2)Itsupportsmultimediawithoutplugins,enhancinguserexperienceacrossdevices.3)SemanticelementsimprovecontentstructureandSEO.4)H5'srespo

H5開發需要掌握的工具和框架包括Vue.js、React和Webpack。 1.Vue.js適用於構建用戶界面,支持組件化開發。 2.React通過虛擬DOM優化頁面渲染,適合複雜應用。 3.Webpack用於模塊打包,優化資源加載。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Atom編輯器mac版下載
最受歡迎的的開源編輯器

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。