当前位置:   article > 正文

js实现在图片上画矩形框

drawrectangle.js
JS组件:

@author Darkness
@version 1.0
@date 2010-08-18

  1. Function.prototype.bind = function(obj){
  2. var _method = this;
  3. return function(){
  4. _method.apply(obj, arguments);
  5. }
  6. }
  7. DrawRectangle = function(id, onMouseUp, className){
  8. this.IMG = document.getElementById(id);
  9. this.isDraw = false;
  10. this.isMouseUp = true;
  11. this.index = 0;
  12. this.currentDrawRectangle = null;
  13. this.className = className;
  14. this.RectangleDivs = [];
  15. this.debug = false;
  16. this._onMouseUp = onMouseUp;
  17. this.bindListener();
  18. }
  19. DrawRectangle.prototype = {
  20. bindListener: function(){
  21. this.IMG.onmousemove = this.dragSize.bind(this);
  22. this.IMG.onmouseup = this.onMouseUp.bind(this);
  23. this.IMG.onmouseout = this.onMouseOut.bind(this);
  24. this.IMG.onmouseover = this.onMouseOver.bind(this);
  25. this.IMG.onmousedown = this.drawLayer.bind(this);
  26. this.IMG.onmouseup = this.onMouseUp.bind(this);
  27. },
  28. drawLayer: function(){
  29. this.IMG.setCapture(true);
  30. this.isDraw = true;
  31. this.ismouseup = false;
  32. this.index++;
  33. var pos = this.getSourcePos();
  34. var x = event.offsetX;
  35. var y = event.offsetY;
  36. var top = y + pos.top - 2;
  37. var left = x + pos.left - 2;
  38. var d = document.createElement("div");
  39. document.body.appendChild(d);
  40. d.style.border = "1px solid #ff0000";
  41. d.style.width = 0;
  42. d.style.height = 0;
  43. d.style.overflow = "hidden";
  44. d.style.position = "absolute";
  45. d.style.left = left;
  46. d.style.top = top;
  47. if(this.className) {
  48. d.className = this.className;
  49. }
  50. d.id = "draw" + this.index;
  51. if (this.debug) {
  52. //d.innerHTML = "<div class='innerbg'>x:" + x + ",y:" + y + ". img_x:" + img_x + ",img_y:" + img_y + ".</div>";
  53. }
  54. this.currentDrawRectangle = d;
  55. this.RectangleDivs[this.index] = {
  56. left: left,
  57. top: top,
  58. el: d
  59. };
  60. },
  61. getSourcePos: function(){
  62. return this.getAbsolutePosition(this.IMG);
  63. },
  64. dragSize: function(){
  65. if (this.isDraw) {
  66. if (event.srcElement.tagName != "IMG")
  67. return;
  68. var pos = this.getSourcePos();
  69. var img_x = pos.top;
  70. var img_y = pos.left;
  71. var x = event.offsetX;
  72. var y = event.offsetY;
  73. var drawW = (x + img_x) - this.RectangleDivs[this.index].left;
  74. var drawH = (y + img_y) - this.RectangleDivs[this.index].top;
  75. this.currentDrawRectangle.style.width = drawW > 0 ? drawW : -drawW;
  76. this.currentDrawRectangle.style.height = drawH > 0 ? drawH : -drawH;
  77. if (drawW < 0) {
  78. this.currentDrawRectangle.style.left = x + img_x;
  79. }
  80. if (drawH < 0) {
  81. this.currentDrawRectangle.style.top = y + img_y;
  82. }
  83. if (this.debug) {
  84. this.currentDrawRectangle.innerHTML = "<div class='innerbg'>x:" + x + ",y:" + y +
  85. ". img_x:" +
  86. img_x +
  87. ",img_y:" +
  88. img_y +
  89. ". drawW:" +
  90. drawW +
  91. ",drawH:" +
  92. drawH +
  93. ". Dleft[i]:" +
  94. this.RectangleDivs[this.index].left +
  95. ",Dtop[i]:" +
  96. this.RectangleDivs[this.index].top +
  97. "src:" +
  98. event.srcElement.tagName +
  99. ".</div>";
  100. }
  101. }
  102. else {
  103. return false;
  104. }
  105. },
  106. stopDraw: function(){
  107. this.isDraw = false;
  108. },
  109. onMouseOut: function(){
  110. if (!this.isMouseUp) {
  111. this.isDraw = false;
  112. }
  113. },
  114. onMouseUp: function(){
  115. this.isDraw = false;
  116. this.isMouseUp = true;
  117. this.IMG.releaseCapture();
  118. if(this._onMouseUp) {
  119. this._onMouseUp.call(this, this.currentDrawRectangle);
  120. }
  121. },
  122. onMouseOver: function(){
  123. if (!this.isMouseUp) {
  124. this.isDraw = true;
  125. }
  126. },
  127. getAbsolutePosition: function(obj){
  128. var t = obj.offsetTop;
  129. var l = obj.offsetLeft;
  130. var w = obj.offsetWidth;
  131. var h = obj.offsetHeight;
  132. while (obj = obj.offsetParent) {
  133. t += obj.offsetTop;
  134. l += obj.offsetLeft;
  135. }
  136. return {
  137. top: t,
  138. left: l,
  139. width: w,
  140. height: h
  141. }
  142. }
  143. };


测试页面:
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
  5. <title>DrawRectangle</title>
  6. <style type="text/css">
  7. <!--
  8. body {
  9. margin-top: 0px;
  10. margin-left: 0px;
  11. }
  12. div {
  13. margin:0;
  14. padding:0;
  15. }
  16. .innerbg {
  17. background:#ff0000;
  18. filter:Alpha(Opacity=40, FinishOpacity=90, Style=0, StartX=0, StartY=0, FinishX=100, FinishY=100);
  19. width:100%;
  20. height:100%;
  21. }
  22. -->
  23. </style>
  24. <script language="javascript" type="text/javascript" src="DrawRectangle.js"></script>
  25. <script>
  26. window.onload = function() {
  27. new DrawRectangle('bigimg', function(div){
  28. alert(div.outerHTML);
  29. });
  30. }
  31. </script>
  32. </head>
  33. <body>
  34. <img src="test.gif" name="bigimg" border="0" id="bigimg" style="margin-left:0px;margin-top:0px;border:1px solid #ccc;" >
  35. </body>
  36. </html>



@version 1.1
@description 采用div替换原来的img作为画布,修复不兼容其他浏览器bug
@author Darkness
@date 2012-07-27

问题:1.0版本中采用IMG.setCapture(true);方式在图片上实现鼠标拖动等事件监听,该方式在最新的ie9下行不通,也不兼容其他浏览器
改进:采用一个div作为画布替换原来的img,将img作为其背景并删除原来的img,然后监听div上的鼠标拖动等事件达到原来同样的效果,此方式兼容其他浏览器

代码:
  1. DrawRectangle = function(id, onMouseUp, className){
  2. document.oncontextmenu=function() {
  3. return true;
  4. };
  5. this.IMG = document.getElementById(id);
  6. var masker = document.createElement("div");
  7. masker.id = "mask_" + id;
  8. var position = this.getAbsolutePosition(this.IMG);
  9. masker.style.width = position.width + "px";
  10. masker.style.height = position.height + "px";
  11. masker.style.left = position.left;
  12. masker.style.top = position.top;
  13. masker.style["background-image"] = "url("+this.IMG.src+")";
  14. masker.className = "imgmasker";
  15. this.masker = masker;
  16. this.IMG.parentNode.appendChild(masker);
  17. this.IMG.parentNode.removeChild(this.IMG);
  18. this.isDraw = false;
  19. this.isMouseUp = true;
  20. this.index = 0;
  21. this.currentDrawRectangle = null;
  22. this.className = className;
  23. this.RectangleDivs = [];
  24. this.debug = true;
  25. this._onMouseUp = onMouseUp;
  26. this.bindListener();
  27. };
  28. DrawRectangle.prototype = {
  29. bindListener: function(){
  30. this.masker.onmousemove = this.dragSize.bind(this);
  31. this.masker.onmouseup = this.onMouseUp.bind(this);
  32. this.masker.onmouseout = this.onMouseOut.bind(this);
  33. this.masker.onmouseover = this.onMouseOver.bind(this);
  34. this.masker.onmousedown = this.drawLayer.bind(this);
  35. this.masker.onmouseup = this.onMouseUp.bind(this);
  36. },
  37. drawLayer: function(){
  38. //this.IMG.setCapture(true);
  39. this.isDraw = true;
  40. this.ismouseup = false;
  41. this.index++;
  42. var pos = this.getSourcePos();
  43. var x = event.offsetX;
  44. var y = event.offsetY;
  45. var top = y + pos.top - 2;
  46. var left = x + pos.left - 2;
  47. var d = document.createElement("div");
  48. // document.body.appendChild(d);
  49. this.masker.appendChild(d);
  50. d.style.border = "1px solid #ff0000";
  51. d.style.width = 0;
  52. d.style.height = 0;
  53. d.style.overflow = "hidden";
  54. d.style.position = "absolute";
  55. d.style.left = left + "px";
  56. d.style.top = top + "px";
  57. d.style.opacity = 0.5;
  58. d.style["z-index"] = 2;
  59. if(this.className) {
  60. d.className = this.className;
  61. }
  62. d.id = "draw" + this.index;
  63. if (this.debug) {
  64. d.innerHTML = "<div class='innerbg'>x:" + x + ",y:" + y + "..</div>";
  65. }
  66. this.currentDrawRectangle = d;
  67. this.RectangleDivs[this.index] = {
  68. left: left,
  69. top: top,
  70. el: d
  71. };
  72. },
  73. getSourcePos: function(){
  74. return this.getAbsolutePosition(this.masker);
  75. },
  76. dragSize: function(){
  77. if (this.isDraw) {
  78. if (!(event.srcElement.tagName.toLowerCase() == "div" && event.srcElement.className == "imgmasker"))
  79. return;
  80. var pos = this.getSourcePos();
  81. var img_x = pos.top;
  82. var img_y = pos.left;
  83. var x = event.offsetX;
  84. var y = event.offsetY;
  85. var drawW = (x + img_x) - this.RectangleDivs[this.index].left;
  86. var drawH = (y + img_y) - this.RectangleDivs[this.index].top;
  87. this.currentDrawRectangle.style.width = (drawW > 0 ? drawW : -drawW) + "px";
  88. this.currentDrawRectangle.style.height = (drawH > 0 ? drawH : -drawH) + "px";
  89. if (drawW < 0) {
  90. this.currentDrawRectangle.style.left = (x + img_x) + "px";
  91. }
  92. if (drawH < 0) {
  93. this.currentDrawRectangle.style.top = (y + img_y) + "px";
  94. }
  95. if (this.debug) {
  96. this.currentDrawRectangle.innerHTML = "<div class='innerbg'>x:" + x + ",y:" + y +
  97. ". img_x:" +
  98. img_x +
  99. ",img_y:" +
  100. img_y +
  101. ". drawW:" +
  102. drawW +
  103. ",drawH:" +
  104. drawH +
  105. ". Dleft[i]:" +
  106. this.RectangleDivs[this.index].left +
  107. ",Dtop[i]:" +
  108. this.RectangleDivs[this.index].top +
  109. "src:" +
  110. event.srcElement.tagName +
  111. ",this.isDraw: " + this.isDraw +
  112. ",this.isMouseUp: " + this.isMouseUp +
  113. ".</div>";
  114. }
  115. }
  116. else {
  117. return false;
  118. }
  119. },
  120. stopDraw: function(){
  121. this.isDraw = false;
  122. },
  123. onMouseOut: function(){
  124. if (!this.isMouseUp) {
  125. this.isDraw = false;
  126. }
  127. },
  128. onMouseUp: function(){
  129. this.isDraw = false;
  130. this.isMouseUp = true;
  131. //this.IMG.releaseCapture();
  132. if(this._onMouseUp) {
  133. this._onMouseUp.call(this, this.currentDrawRectangle);
  134. }
  135. },
  136. onMouseOver: function(){
  137. if (!this.isMouseUp) {
  138. this.isDraw = true;
  139. }
  140. },
  141. getAbsolutePosition: function(obj){
  142. var t = obj.offsetTop;
  143. var l = obj.offsetLeft;
  144. var w = obj.offsetWidth;
  145. var h = obj.offsetHeight;
  146. while (obj = obj.offsetParent) {
  147. t += obj.offsetTop;
  148. l += obj.offsetLeft;
  149. }
  150. return {
  151. top: t,
  152. left: l,
  153. width: w,
  154. height: h
  155. };
  156. }
  157. };


测试:
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
  5. <title>DrawRectangle</title>
  6. <style type="text/css">
  7. <!--
  8. body {
  9. margin-top: 0px;
  10. margin-left: 0px;
  11. }
  12. div {
  13. margin:0;
  14. padding:0;
  15. }
  16. .innerbg {
  17. background:#ff0000;
  18. filter:Alpha(Opacity=40, FinishOpacity=90, Style=0, StartX=0, StartY=0, FinishX=100, FinishY=100);
  19. width:100%;
  20. height:100%;
  21. }
  22. div.imgmasker {
  23. position: absolute;
  24. z-index: 1;
  25. cursor: pointer;
  26. border:1px solid red;
  27. background:#000000;
  28. opacity: 1;
  29. }
  30. -->
  31. </style>
  32. <script language="javascript" type="text/javascript" src="arkjs/lang.js"></script>
  33. <script language="javascript" type="text/javascript" src="DrawRectangle.js"></script>
  34. <script>
  35. window.onload = function() {
  36. new DrawRectangle('bigimg', function(div){
  37. //alert(div.outerHTML);
  38. });
  39. }
  40. </script>
  41. </head>
  42. <body>
  43. <img src="test.jpg" name="bigimg" border="0" id="bigimg" style="margin-left:0px;margin-top:0px;border:1px solid #ccc;"/>
  44. </body>
  45. </html>

效果图:

23165914_RAkU.bmp

转载于:https://my.oschina.net/darkness/blog/510218

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/332594
推荐阅读
相关标签
  

闽ICP备14008679号