instructor-writer.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. var instructor_writer = function (editor, handler, count) {
  2. this.count = count || 3;
  3. this.handler = handler;
  4. this.bound = $(editor).width() / this.count;
  5. this.editor = editor;
  6. this.isshow = false;
  7. this.init();
  8. }
  9. instructor_writer.prototype.init = function () {
  10. if (this.container)
  11. return;
  12. var that = this;
  13. var dom = this.container = document.createElement("div");
  14. this.container.style.zIndex = 10000000;
  15. this.container.style.position = "absolute";
  16. this.container.style.display = "none";
  17. this.container.id = "writer";
  18. this.container.className = "write-div";
  19. for (var i = 0; i < this.count; i++) {
  20. this.container.appendChild(this.createDrawAbleCanvas());
  21. }
  22. window.addEventListener("unload", function () {
  23. dom.parentNode.removeChild(dom);
  24. window.top.document.removeEventListener("click", docClick);
  25. });
  26. window.docClick = function (e) {
  27. if (e.srcElement.parentNode != dom && that.editor != e.srcElement && e.srcElement.parentNode != that.editor && e.srcElement != that.handler) { //不是canvas
  28. that.hide();
  29. that.onHide&&that.onHide();
  30. }
  31. }
  32. window.addEventListener("click", docClick);
  33. window.top.document.addEventListener("click", docClick);
  34. window.top.document.body.appendChild(this.container);
  35. this.bound = $(this.container).height();
  36. }
  37. instructor_writer.prototype.show = function () {
  38. if (!$(this.editor).is(":visible"))
  39. return;
  40. var ofs = recursionIframe(window);
  41. this.container.style.left = ofs.left + $(this.editor).offset().left + "px";
  42. this.container.style.top = ofs.top - this.bound + $(this.editor).offset().top + "px";
  43. this.container.style.display = "";
  44. this.isshow = true;
  45. }
  46. instructor_writer.prototype.hide = function () {
  47. this.container.style.display = "none";
  48. this.isshow = false;
  49. }
  50. // instructor_writer.prototype.getWriteBackground = function () {
  51. // var cvs = document.createElement("canvas");
  52. // cvs.width = cvs.height = this.bound;
  53. // var g = cvs.getContext("2d"),
  54. // W = cvs.width,
  55. // H = cvs.height;
  56. // g.fillStyle = "white";
  57. // g.fillRect(0, 0, W, H);
  58. // g.strokeStyle = "red";
  59. // g.lineWidth = 2;
  60. // g.rect(1, 1, W - 2, H - 2);
  61. // g.stroke();
  62. // g.lineWidth = 1;
  63. // g.setLineDash([5, 5]);
  64. // g.moveTo(0, 0);
  65. // g.lineTo(W, H);
  66. // g.moveTo(0, H);
  67. // g.lineTo(W, 0);
  68. // g.moveTo(0, H / 2);
  69. // g.lineTo(W, H / 2);
  70. // g.moveTo(W / 2, 0);
  71. // g.lineTo(W / 2, H);
  72. // g.stroke();
  73. // return cvs.toDataURL("image/png");
  74. // };
  75. instructor_writer.prototype.createDrawAbleCanvas = function () {
  76. var this_ = this;
  77. var canvas = document.createElement("canvas");
  78. // var url = this.getWriteBackground();
  79. // canvas.style.backgroundImage = 'URL(' + url + ')';
  80. // canvas.style.backgroundRepeat = "no-repeat";
  81. canvas.width = "239";
  82. canvas.height = "204";
  83. var context = canvas.getContext("2d");
  84. context.lineWidth = 6;
  85. /*在鼠标移动时划线 */
  86. function onMouseMove(event) {
  87. var canvas_ = this;
  88. var p = toCanvasCoord(canvas_, event.clientX, event.clientY);
  89. context.lineTo(p.x, p.y);
  90. context.stroke();
  91. }
  92. function onMouseDown(event) {
  93. var nodes = this.parentNode.children;
  94. for (var i = 0; i < nodes.length; i++) {
  95. if (nodes[i] != this)
  96. onMouseUp.call(nodes[i], event);
  97. }
  98. var canvas_ = this;
  99. canvas_.drawing = true;
  100. canvas_.ismouseup = false;
  101. if (canvas.timeoutid) {
  102. clearTimeout(canvas.timeoutid);
  103. }
  104. canvas_.getContext("2d").beginPath();
  105. var p = toCanvasCoord(canvas_, event.clientX, event.clientY);
  106. canvas_.getContext("2d").moveTo(p.x, p.y);
  107. canvas_.addEventListener("mousemove", onMouseMove, false);
  108. }
  109. function onMouseUp(event) {
  110. var canvas_ = this;
  111. if (canvas_.ismouseup)
  112. return;
  113. canvas_.removeEventListener("mousemove", onMouseMove, false);
  114. var timeoutid = setTimeout(function () {
  115. if (canvas_.drawing) {
  116. canvas_.insertImg(canvas);
  117. canvas_.getContext("2d").clearRect(0, 0, canvas.width, canvas.height);
  118. canvas_.timeoutid = null;
  119. canvas_.drawing = false;
  120. } else {}
  121. }, 1000);
  122. canvas_.timeoutid = timeoutid;
  123. canvas_.ismouseup = true;
  124. }
  125. function toCanvasCoord(canvas, x, y) {
  126. var point = {};
  127. var coord = canvas.getBoundingClientRect();
  128. point.x = x - coord.left;
  129. point.y = y - coord.top;
  130. return point;
  131. }
  132. function createImage(src) {
  133. var img = new Image();
  134. $(img).addClass('item');
  135. img.src = src;
  136. return img;
  137. }
  138. canvas.insertImg = function (canvas) {
  139. var img = createImage(canvas.toDataURL("image/png"));
  140. img.setAttribute("editMode", "handWrite");
  141. $(this_.editor).append(img);
  142. addDivDragStart();
  143. };
  144. /*添加mousedown监听事件,在用户鼠标按下后开始画线,并注册mousemove事件*/
  145. canvas.addEventListener("mousedown", onMouseDown, false);
  146. /*在鼠标按键松开后,注销鼠标移动事件 */
  147. canvas.addEventListener("mouseup", onMouseUp, false);
  148. // canvas.addEventListener("mouseleave", onMouseUp, false);
  149. canvas.addEventListener("mouseenter", function () {
  150. this.ismouseenter = true;
  151. }, false);
  152. canvas.addEventListener("mouseout", function () {
  153. this.ismouseenter = false;
  154. }, false);
  155. document.addEventListener("mouseup", function () {
  156. if (!canvas.ismouseenter) {
  157. onMouseUp.call(canvas);
  158. }
  159. }, false);
  160. return canvas;
  161. };
  162. var recursionIframe = function (win) {
  163. if (!win) {
  164. win = this.wins;
  165. }
  166. function getTop(e) {
  167. var offset = e.offsetTop;
  168. if (e.offsetParent != null)
  169. offset += getTop(e.offsetParent);
  170. return offset;
  171. }
  172. function getLeft(e) {
  173. var offset = e.offsetLeft;
  174. if (e.offsetParent != null)
  175. offset += getLeft(e.offsetParent);
  176. return offset;
  177. }
  178. if (win.parent == top && win.parent == win) {
  179. // var xy = this.getAbsPoint(win);
  180. return {
  181. "left": 0,
  182. "top": 0
  183. };
  184. }
  185. var winPar = win.parent; // .opener
  186. var iframeArr = winPar.document.getElementsByTagName('IFRAME');
  187. var targetIframe;
  188. for (var i = 0; i < iframeArr.length; i++) {
  189. var iframeElem = iframeArr[i];
  190. if (iframeElem.contentWindow == win) {
  191. targetIframe = iframeElem;
  192. break;
  193. }
  194. }
  195. // alert(targetIframe==null)
  196. // var xy = this.getAbsPoint(targetIframe);
  197. var xy = {
  198. left: getLeft(targetIframe),
  199. top: getTop(targetIframe)
  200. }
  201. if (winPar.parent != winPar) {
  202. var xy2 = this.recursionIframe(winPar);
  203. xy.left += xy2.left;
  204. xy.top += xy2.top;
  205. }
  206. return xy
  207. }