支持移动端的HTML5 Canvas逼真黑板特效

所属分类:输入-丰富的输入

 34084  420  查看评论 (6)
分享到微信朋友圈
X
支持移动端的HTML5 Canvas逼真黑板特效 ie兼容10

这是一款使用HTML5 Canvas制作的黑板特效,该黑板特效支持手机移动端,它能模拟使用粉笔在黑板上写字的效果。该黑板特效的特点还有:

  • 使用鼠标左键能够在黑板上写字。

  • 使用鼠标右键能够擦除已写的字。

  • 按空格键可以清空黑板上的内容。

  • 点击下载按钮可以将写入的内容保存为图片并下载。

使用方法

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="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();
  });
}


相关插件-丰富的输入

本地化jQuery公式加中文键盘

集合mathquill公式输入和中英文标点符号输入键盘,主要适配pad端
  丰富的输入
 24345  326

jQuery移动端自制画板

可以使用多种颜色画笔进行图画,有橡皮擦,代码注释较全容易扩展。注意请在移动端查看效果!
  丰富的输入
 27494  301

jQuery评论插件

这是一个评论插件,传入一个评论体即可,支持对留言的回复
  丰富的输入
 70909  554

jQuery表情插件emoji.js

基于jQuery的表情选择,html表情
  丰富的输入
 23241  216

讨论这个项目(6)回答他人问题或分享插件使用方法奖励jQ币 评论用户自律公约

    bunilla 0
    2016/9/30 20:09:21
    pc模式下可以画,选移动端页面后画了没反应,但是按保存下载后会出现画的东西,这是哪里有问题吗? 回复
    锐不可当 0
    2016/9/19 8:09:27
    只有我的Google Chrome画不出来吗
        鑫爱0
        2016/9/19 14:09:36
        我的可以
    回复
    一直在路上 0
    2016/9/18 14:09:30
    如何再多一些其他的颜色就更完美了 回复
    板砖做切糕 0
    2016/9/18 9:09:35
    很不错,希望能添加其他颜色的粉笔 回复
    明月半倚深秋 0
    2016/9/17 12:09:25
😃
  • 😀
  • 😉
  • 😥
  • 😵
  • 😫
  • 😘
  • 😡
  • 👍
  • 🌹
  • 👏
  • 🍺
  • 🍉
  • 🌙
  • 💖
  • 💔
😃
  • 😀
  • 😉
  • 😥
  • 😵
  • 😫
  • 😘
  • 😡
  • 👍
  • 🌹
  • 👏
  • 🍺
  • 🍉
  • 🌙
  • 💖
  • 💔
取消回复