paint2.html 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
  6. <title> canvas 手写毛笔字效果 </title>
  7. <style type="text/css">
  8. #canvasId {
  9. background-color: #FFFFcc;
  10. }
  11. </style>
  12. </head>
  13. <body>
  14. <canvas id="canvasId" width="700" height="500"></canvas><br />
  15. <input type="button" value="clear" οnclick="hw.clear();" />
  16. <script type="text/javascript">
  17. function Handwriting(id) {
  18. this.canvas = document.getElementById(id);
  19. this.ctx = this.canvas.getContext("2d");
  20. this.ctx.fillStyle = "rgba(0,0,0,0.25)";
  21. var _this = this;
  22. this.canvas.onmousedown = function (e) { _this.downEvent(e)};
  23. this.canvas.onmousemove = function (e) { _this.moveEvent(e)};
  24. this.canvas.onmouseup = function (e) { _this.upEvent(e)};
  25. this.canvas.onmouseout = function (e) { _this.upEvent(e)};
  26. this.moveFlag = false;
  27. this.upof = {};
  28. this.radius = 0;
  29. this.has = [];
  30. this.lineMax = 30;
  31. this.lineMin = 3;
  32. this.linePressure = 1;
  33. this.smoothness = 80;
  34. }
  35. Handwriting.prototype.clear = function () {
  36. this.ctx.clearRect(0,0,this.canvas.width,this.canvas.height);
  37. }
  38. Handwriting.prototype.downEvent = function (e) {
  39. this.moveFlag = true;
  40. this.has = [];
  41. this.upof = this.getXY(e);
  42. }
  43. Handwriting.prototype.moveEvent = function (e) {
  44. if (!this.moveFlag)
  45. return;
  46. var of = this.getXY(e);
  47. var up = this.upof;
  48. var ur = this.radius;
  49. this.has.unshift({time:new Date().getTime() ,dis:this.distance(up,of)});
  50. var dis = 0;
  51. var time = 0;
  52. for (var n = 0; n < this.has.length-1; n++) {
  53. dis += this.has[n].dis;
  54. time += this.has[n].time-this.has[n+1].time;
  55. if (dis>this.smoothness)
  56. break;
  57. }
  58. var or = Math.min(time/dis*this.linePressure+this.lineMin , this.lineMax) / 2;
  59. this.radius = or;
  60. this.upof = of;
  61. if (this.has.length<=4)
  62. return;
  63. var len = Math.round(this.has[0].dis/2)+1;
  64. for (var i = 0; i < len; i++) {
  65. var x = up.x + (of.x-up.x)/len*i;
  66. var y = up.y + (of.y-up.y)/len*i;
  67. var r = ur + (or-ur)/len*i;
  68. this.ctx.beginPath();
  69. this.ctx.arc(x,y,r,0,2*Math.PI,true);
  70. this.ctx.fill();
  71. }
  72. }
  73. Handwriting.prototype.upEvent = function (e) {
  74. this.moveFlag = false;
  75. }
  76. Handwriting.prototype.getXY = function (e)
  77. {
  78. return {
  79. x : e.clientX - this.canvas.offsetLeft + (document.body.scrollLeft || document.documentElement.scrollLeft),
  80. y : e.clientY - this.canvas.offsetTop + (document.body.scrollTop || document.documentElement.scrollTop)
  81. }
  82. }
  83. Handwriting.prototype.distance = function (a,b)
  84. {
  85. var x = b.x-a.x , y = b.y-a.y;
  86. return Math.sqrt(x*x+y*y);
  87. }
  88. var hw = new Handwriting("canvasId");
  89. hw.lineMax = 40;
  90. hw.lineMin = 5;
  91. hw.linePressure = 2.5;
  92. hw.smoothness = 100;
  93. </script>
  94. </body>
  95. </html>
  96.