资讯专栏INFORMATION COLUMN

Canvas 涂鸦

thekingisalwaysluc / 529人阅读

摘要:第二步,消除涂鸦锯齿的办法简单的绘制和图片保存完成了,但是在这种情况下,线条会有很明显的锯齿灵魂画手来了。在经过搜索查阅之后,发现有一个绘制办法可以降低锯齿的问题。橡皮擦的原理是,将橡皮绘制的路径覆盖到原来的画笔上。

第一步,我们先实现简单的绘制,并且在绘制之后将图片保存到本地

</>复制代码

  1. var canvas = document.getElementById("canvas"),
  2. ctx = canvas.getContext("2d"),
  3. iptColor = document.getElementById("iptColor"),//画笔颜色
  4. iptSize = document.getElementById("iptSize"),//画笔大小
  5. btnClear = document.getElementById("btnClear"), //清除按钮
  6. btnSave = document.getElementById("btnSave"),//保存按钮
  7. canvasWidth = 800,
  8. canvasHeight = 600;
  9. canvas.setAttribute("width",canvasWidth);
  10. canvas.setAttribute("height",canvasHeight);
  11. iptSize.oninput = function() {
  12. document.querySelector(".txt-size").innerHTML = iptSize.value
  13. }
  14. canvas.addEventListener("mousedown",function(e){
  15. var e = e || window.event;
  16. ctx.lineWidth = iptSize.value;
  17. ctx.strokeStyle = iptColor.value;
  18. ctx.lineCap = "round";
  19. ctx.lineJoin = "round";
  20. ctx.beginPath();
  21. ctx.moveTo(e.clientX - canvas.offsetLeft,e.clientY - canvas.offsetTop); //线条起始位置
  22. document.onmousemove = function(e) {
  23. var e = e || window.event;
  24. ctx.lineTo(e.clientX - canvas.offsetLeft,e.clientY - canvas.offsetTop);
  25. ctx.stroke();//绘制线条
  26. };
  27. canvas.onmouseup = function() {
  28. document.onmousemove = null;
  29. document.onmouseup = null;
  30. };
  31. })
  32. //清除画布
  33. btnClear.addEventListener("click",function(){
  34. ctx.clearRect(0, 0, canvasWidth, canvasHeight);
  35. })
  36. //保存图片到本地
  37. btnSave.addEventListener("click",function(){
  38. var imgData = canvas.toDataURL("image/png").replace("image/png", "image/octet-stream");
  39. var filename = "图片.png";
  40. saveFile(imgData,filename)
  41. })
  42. var saveFile = function(data, filename) {
  43. var save_link = document.createElementNS("http://www.w3.org/1999/xhtml", "a");
  44. save_link.href = data;
  45. save_link.download = filename;
  46. var event = document.createEvent("MouseEvents");
  47. event.initMouseEvent("click", true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
  48. save_link.dispatchEvent(event);
  49. };

这样看起来虽然说好像没问题,但是这一句有一些问题:

</>复制代码

  1. e.clientX - canvas.offsetLeft,e.clientY - canvas.offsetTop

如果页面比较高,产生了滚动条,当滚动条向上滑的时候,绘图的位置就不准确了,示例如:https://codepen.io/jianxiujiu...。

所以此处应该使用

</>复制代码

  1. e.clientX - canvas.getBoundingClientRect().left,e.clientY - canvas.getBoundingClientRect().top

来替换。

第二步,消除涂鸦锯齿的办法

简单的绘制和图片保存完成了,但是在这种情况下,线条会有很明显的锯齿(灵魂画手来了)。

有一个简单粗暴的方法,将线条的边缘进行模糊:

</>复制代码

  1. ctx.shadowBlur = 1;
  2. ctx.shadowColor = iptColor.value;

示例:https://codepen.io/jianxiujiu...
加上边缘模糊之后,线条明显柔和了许多,但是还是有一个问题,就是在画笔收笔的地方,线条会变小(灵魂画手又来了)。

在经过搜索查阅之后,发现有一个绘制办法可以降低锯齿的问题。
原理如图:

绘制一个圆点,在下一个圆点之间,绘制一个矩形进行填充。这样绘制出来的线条的衔接会是圆滑的。

</>复制代码

  1. var canvas = document.getElementById("canvas"),
  2. ctx = canvas.getContext("2d"),
  3. btnClear = document.getElementById("btnClear"),
  4. btnSave = document.getElementById("btnSave"),
  5. iptColor = document.getElementById("iptColor"),
  6. iptSize = document.getElementById("iptSize"),
  7. canvasWidth = 800,
  8. canvasHeight = 600;
  9. canvas.setAttribute("width",canvasWidth);
  10. canvas.setAttribute("height",canvasHeight);
  11. iptSize.oninput = function() {
  12. document.querySelector(".txt-size").innerHTML = iptSize.value
  13. }
  14. canvas.addEventListener("mousedown",function(e){
  15. var e = e || window.event;
  16. var x1 = e.clientX - canvas.getBoundingClientRect().left,
  17. y1 = e.clientY - canvas.getBoundingClientRect().top;
  18. lineSize = iptSize.value;
  19. lineColor = iptColor.value;
  20. ctx.beginPath();
  21. ctx.fillStyle = lineColor;
  22. ctx.lineCap = "round";
  23. ctx.lineJoin = "round";
  24. ctx.arc(x1, y1, lineSize, 0, 2 * Math.PI);
  25. ctx.fill();
  26. ctx.closePath();
  27. document.onmousemove = function(e) {
  28. var e = e || window.event;
  29. var x2 = e.clientX - canvas.getBoundingClientRect().left,
  30. y2 = e.clientY - canvas.getBoundingClientRect().top;
  31. var asin = lineSize * Math.sin(Math.atan((y2 - y1) / (x2 - x1)));
  32. var acos = lineSize * Math.cos(Math.atan((y2 - y1) / (x2 - x1)));
  33. //分别获取矩形的四个点的xy轴位置
  34. var x3 = x1 + asin;
  35. var y3 = y1 - acos;
  36. var x4 = x1 - asin;
  37. var y4 = y1 + acos;
  38. var x5 = x2 + asin;
  39. var y5 = y2 - acos;
  40. var x6 = x2 - asin;
  41. var y6 = y2 + acos;
  42. ctx.beginPath();
  43. ctx.fillStyle = lineColor;
  44. ctx.arc(x2, y2, lineSize, 0, 2 * Math.PI);
  45. ctx.fill();
  46. ctx.closePath();
  47. ctx.beginPath();
  48. ctx.fillStyle = lineColor;
  49. ctx.moveTo(x3, y3);
  50. ctx.lineTo(x5, y5);
  51. ctx.lineTo(x6, y6);
  52. ctx.lineTo(x4, y4);
  53. ctx.fill();
  54. ctx.closePath();
  55. x1 = x2;
  56. y1 = y2;
  57. };
  58. canvas.onmouseup = function() {
  59. document.onmousemove = null;
  60. document.onmouseup = null;
  61. };
  62. })
  63. btnClear.addEventListener("click",function(){
  64. ctx.clearRect(0, 0, canvasWidth, canvasHeight);
  65. })
  66. btnSave.addEventListener("click",function(){
  67. var imgData = canvas.toDataURL("image/png").replace("image/png", "image/octet-stream");
  68. var filename = "图片.png";
  69. saveFile(imgData,filename)
  70. })
  71. var saveFile = function(data, filename) {
  72. var save_link = document.createElementNS("http://www.w3.org/1999/xhtml", "a");
  73. save_link.href = data;
  74. save_link.download = filename;
  75. var event = document.createEvent("MouseEvents");
  76. event.initMouseEvent("click", true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
  77. save_link.dispatchEvent(event);
  78. };

DEMO:
https://codepen.io/jianxiujiu...

第三步,添加橡皮擦工具

用这个方法,我们还可以加上橡皮擦。
橡皮擦的原理是,将橡皮绘制的路径覆盖到原来的画笔上。
这里我们将用到canvas的一个属性globalCompositeOperation,详解参照:
http://www.w3school.com.cn/ta...

</>复制代码

  1. var canvas = document.getElementById("canvas"),
  2. ctx = canvas.getContext("2d"),
  3. btnClear = document.getElementById("btnClear"),
  4. btnSave = document.getElementById("btnSave"),
  5. btnRestore = document.getElementById("btnRestore"),
  6. iptColor = document.getElementById("iptColor"),
  7. iptSize = document.getElementById("iptSize"),
  8. eraser = document.getElementById("eraser")
  9. pen = document.getElementById("pen"),
  10. canvasWidth = 800,
  11. canvasHeight = 600,
  12. isClear = 0;
  13. canvas.setAttribute("width",canvasWidth);
  14. canvas.setAttribute("height",canvasHeight);
  15. iptSize.oninput = function() {
  16. document.querySelector(".txt-size").innerHTML = iptSize.value
  17. }
  18. eraser.addEventListener("click",function(){
  19. isClear = 1;
  20. this.classList.add("cho");
  21. pen.classList.remove("cho")
  22. })
  23. pen.addEventListener("click",function(){
  24. isClear = 0;
  25. this.classList.add("cho");
  26. eraser.classList.remove("cho")
  27. })
  28. canvas.addEventListener("mousedown",function(e){
  29. var e = e || window.event;
  30. var x1 = e.clientX - canvas.getBoundingClientRect().left,
  31. y1 = e.clientY - canvas.getBoundingClientRect().top;
  32. lineSize = iptSize.value;
  33. if(isClear == 0){
  34. lineColor = iptColor.value;
  35. ctx.beginPath();
  36. ctx.fillStyle = lineColor;
  37. ctx.arc(x1, y1, lineSize, 0, 2 * Math.PI);
  38. ctx.fill();
  39. ctx.closePath();
  40. document.onmousemove = function(e) {
  41. draw();
  42. };
  43. btnClear.classList.remove("dis");
  44. btnSave.classList.remove("dis");
  45. }else{
  46. ctx.strokeStyle = "rgba(250,250,250,0)";
  47. document.onmousemove = function() {
  48. ctx.globalCompositeOperation = "destination-out";
  49. draw();
  50. ctx.globalCompositeOperation = "source-over"
  51. }
  52. }
  53. canvas.onmouseup = function() {
  54. document.onmousemove = null;
  55. document.onmouseup = null;
  56. };
  57. draw = function(e){
  58. var e = e || window.event,
  59. x2 = e.clientX - canvas.getBoundingClientRect().left,
  60. y2 = e.clientY - canvas.getBoundingClientRect().top,
  61. asin = lineSize * Math.sin(Math.atan((y2 - y1) / (x2 - x1))),
  62. acos = lineSize * Math.cos(Math.atan((y2 - y1) / (x2 - x1))),
  63. x3 = x1 + asin,
  64. y3 = y1 - acos,
  65. x4 = x1 - asin,
  66. y4 = y1 + acos,
  67. x5 = x2 + asin,
  68. y5 = y2 - acos,
  69. x6 = x2 - asin,
  70. y6 = y2 + acos;
  71. ctx.beginPath();
  72. ctx.arc(x2, y2, lineSize, 0, 2 * Math.PI);
  73. ctx.fill();
  74. ctx.closePath();
  75. ctx.beginPath();
  76. ctx.fillStyle = lineColor;
  77. ctx.moveTo(x3, y3);
  78. ctx.lineTo(x5, y5);
  79. ctx.lineTo(x6, y6);
  80. ctx.lineTo(x4, y4);
  81. ctx.fill();
  82. ctx.closePath();
  83. x1 = x2;
  84. y1 = y2;
  85. }
  86. })
  87. // 清除
  88. btnClear.addEventListener("click",function(){
  89. ctx.clearRect(0, 0, canvasWidth, canvasHeight);
  90. this.classList.add("dis");
  91. btnSave.classList.add("dis");
  92. })
  93. //保存图片
  94. btnSave.addEventListener("click",function(){
  95. var imgData = canvas.toDataURL("image/png").replace("image/png", "image/octet-stream");
  96. var filename = "图片.png";
  97. saveFile(imgData,filename);
  98. this.classList.add("dis");
  99. })
  100. var saveFile = function(data, filename) {
  101. var save_link = document.createElementNS("http://www.w3.org/1999/xhtml", "a");
  102. save_link.href = data;
  103. save_link.download = filename;
  104. var event = document.createEvent("MouseEvents");
  105. event.initMouseEvent("click", true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
  106. save_link.dispatchEvent(event);
  107. };

地址:https://codepen.io/jianxiujiu...

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/89025.html

相关文章

  • vue组件:canvas实现图片涂鸦功能

    摘要:方案背景需求需要对图片进行标注,导出图片。对应方案用实现涂鸦圆形矩形的绘制,最终生成图片编码用于上传大量图片批量上传很耗时间,为了提高用户体验,改为只实现圆形矩形绘制,最终保存成坐标,下次显示时根据坐标再绘制。 方案背景 需求 需要对图片进行标注,导出图片。 需要标注N多图片最后同时保存。 需要根据多边形区域数据(区域、颜色、名称)标注。 对应方案 用canvas实现涂鸦、圆形、...

    roland_reed 评论0 收藏0
  • 利用vue制作在线涂鸦

    摘要:撤销清空等操作撤销前进清空清空前后数据恢复到默认数据地址查看代码 效果展示 showImg(https://segmentfault.com/img/bVHJXf?w=1550&h=846); Canvas API简介 调用方法 getImageData() 返回ImageData对象,该对象为画布上指定的矩形复制像素数据 putImageData() 把图像数据(从指定的 Imag...

    nemo 评论0 收藏0
  • Android:涂鸦tuytaSDK实现逻辑

    摘要:开启扫描时需要设备处于配网状态一分类配网子设备可以通过使用手机蓝牙直接扫描获取设备到设备基础信息,再使用配网接口实现设备的本地配网。   (一)分类   (二)设备配置   (三)设备管理     设备管理,大体分为两类,mesh 和 其他     获取设备列表,给涂鸦sdk发送当前房间id...

    MorePainMoreGain 评论0 收藏0

发表评论

0条评论

thekingisalwaysluc

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<