• 【html】图片多矩形框裁剪


     说明

    由于项目中需要对一个图片进行多选择框进行裁剪,所以特写当前的示例代码。

    代码

    1. html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <base href="/">
    6. <title>图片裁剪title>
    7. head>
    8. <body>
    9. <canvas id="myCanvas" style="border:1px solid #d3d3d3;">
    10. Your browser does not support the HTML5 canvas tag.
    11. canvas>
    12. <script>
    13. let canvas = document.getElementById("myCanvas");
    14. let ctx = canvas.getContext("2d");
    15. let img = new Image();
    16. let rate = 1
    17. img.onload = function () {
    18. let width = img.width
    19. let height = img.height
    20. if (img.width > window.innerWidth || img.height > window.innerHeight) {
    21. if (window.innerWidth / img.width > window.innerHeight / img.height) {
    22. rate = window.innerHeight / img.height
    23. width = img.width * rate
    24. height = window.innerHeight
    25. } else {
    26. width = window.innerWidth
    27. rate = window.innerWidth / img.width
    28. height = img.height * rate
    29. }
    30. }
    31. // 等比缩小
    32. canvas.width = width;
    33. canvas.height = height;
    34. ctx.drawImage(img, 0, 0, canvas.width, canvas.height);
    35. }
    36. let url = new URL(window.location.href);
    37. let params = new URLSearchParams(url.search);
    38. img.src = "https://img1.baidu.com/it/u=1486134966,661096340&fm=253&fmt=auto&app=138&f=JPEG?w=800&h=500";
    39. // 坐标数组
    40. let coorArr = []
    41. // 当前坐标
    42. let coor = {}
    43. // 鼠标按下
    44. canvas.onmousedown = function (e) {
    45. coor.begin = {
    46. x: e.clientX - canvas.offsetLeft,
    47. y: e.clientY - canvas.offsetTop
    48. }
    49. coor.end = {
    50. x: 0,
    51. y: 0
    52. }
    53. }
    54. // 移动鼠标
    55. canvas.onmousemove = function (e) {
    56. let begin = coor.begin;
    57. if (begin === undefined || begin.x === undefined) {
    58. return
    59. }
    60. coor.begin = coor.begin
    61. coor.end = {
    62. x: e.clientX - canvas.offsetLeft,
    63. y: e.clientY - canvas.offsetTop
    64. }
    65. draw();
    66. drawLine(coor);
    67. }
    68. // 鼠标放开
    69. canvas.onmouseup = function (e) {
    70. let begin = coor.begin;
    71. if (begin === undefined || begin.x === undefined) {
    72. return
    73. }
    74. coorArr.push({
    75. begin: coor.begin,
    76. end: {
    77. x: e.clientX - canvas.offsetLeft,
    78. y: e.clientY - canvas.offsetTop
    79. }
    80. })
    81. draw();
    82. coor.begin = {}
    83. }
    84. // 双击裁剪
    85. canvas.ondblclick = function () {
    86. ctx.clearRect(0, 0, canvas.width, canvas.height);
    87. ctx.drawImage(img, 0, 0, canvas.width, canvas.height);
    88. coorArr = []
    89. coor = {}
    90. }
    91. // 鼠标离开则清理
    92. canvas.onmouseout = function () {
    93. ctx.clearRect(0, 0, canvas.width, canvas.height);
    94. ctx.drawImage(img, 0, 0, canvas.width, canvas.height);
    95. coorArr = []
    96. coor = {}
    97. }
    98. // 画框
    99. function draw() {
    100. ctx.clearRect(0, 0, canvas.width, canvas.height);
    101. ctx.drawImage(img, 0, 0, canvas.width, canvas.height);
    102. ctx.beginPath();
    103. ctx.strokeStyle = 'green';
    104. ctx.lineWidth = 5;
    105. ctx.lineCap = 'round';
    106. ctx.lineJoin = 'round';
    107. // 先画之前的框
    108. coorArr.forEach(coor => {
    109. drawLine(coor);
    110. });
    111. // 显示光标位置信息
    112. ctx.font = "18px Arial";
    113. ctx.fillStyle = "red";
    114. // 在canvas外显示光标位置
    115. ctx.fillText("缩放:" + rate.toFixed(2) + ";说明:鼠标离开画布清理,鼠标双击进行裁剪。", 5, 30);
    116. }
    117. // 画矩形
    118. function drawLine(coor) {
    119. let begin = coor.begin;
    120. let end = coor.end;
    121. // 画矩形
    122. ctx.moveTo(begin.x, begin.y);
    123. ctx.lineTo(end.x, begin.y);
    124. ctx.moveTo(end.x, begin.y);
    125. ctx.lineTo(end.x, end.y);
    126. ctx.moveTo(end.x, end.y);
    127. ctx.lineTo(begin.x, end.y);
    128. ctx.moveTo(begin.x, end.y);
    129. ctx.lineTo(begin.x, begin.y);
    130. ctx.stroke();
    131. }
    132. script>
    133. body>
    134. html>

    示例

     

  • 相关阅读:
    08-Linux特殊权限
    解密Prompt系列8. 无需训练让LLM支持超长输入:知识库 & unlimiformer & PCW & NBCE
    [Android Input系统学习]从mouse事件再看input分发
    linux后台开发面试题
    AI 卷到正经「挖矿」业,卡内基科学研究所另辟蹊径,靠关联分析法找到新矿床
    Java面向对象编程
    【Java第30期】:Cookie 和 Session 的工作流程
    Tomcat 架构
    【Git】轻松学会 Git(一):掌握 Git 的基本操作
    ORB-SLAM安装过程遇到问题记录整理
  • 原文地址:https://blog.csdn.net/qq_38428623/article/details/134052601