这是一款使用HTML5 Canvas制作的黑板特效,该黑板特效支持手机移动端,它能模拟使用粉笔在黑板上写字的效果。该黑板特效的特点还有:
使用鼠标左键能够在黑板上写字。
使用鼠标右键能够擦除已写的字。
按空格键可以清空黑板上的内容。
点击下载按钮可以将写入的内容保存为图片并下载。
JavaScript
该HTML5 Canvas黑板特效的完整js代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 | $(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="img/bg.png" id="pattern" width=50 height=50>' ); $( '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="' +ctximage+ '" name="chalkboard.png">' ); },500); } $(window).resize( function (){ chalkboard(); }); } |
特别申明:
本站所有资源都是由网友投稿发布,或转载各大下载站,请自行检测软件的完整性!
本站所有资源仅供学习与参考,请勿用于商业用途,否则产生的一切后果将由您自己承担!
如有侵权请联系我们删除下架,联系方式:lei1294551502@163.com