canvas.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. //变量声明
  2. var mouseFrom = {},
  3. mouseTo = {},
  4. drawType = 'right', //画板绘制类型
  5. canvasObjectIndex = 0;
  6. var drawWidth = 2; //笔触宽度
  7. var modifySize = 20, modifySmallSize = 10; //大/小 | 对/错画笔直径大小
  8. var color = "#E34F51"; //画笔颜色
  9. var drawingObject = null; //当前绘制对象
  10. var moveCount = 1; //绘制移动计数器
  11. var doDrawing = false; // 绘制状态
  12. var rightColor = "#008B00", wrongColor = "#E34F51";
  13. // var modifyImg = null;//当前批改的图片
  14. var appErrCount = 0;//全局错误个数
  15. var errTextBoxArray = new Array();
  16. var canvasArr = []; //所有(fabric产生)的canvas对象
  17. //切换画板
  18. function switchCanvas(url, index) {
  19. var canvasId = "c" + index;
  20. jQuery("#canvasDiv").find("div").addClass("fn-hide");
  21. jQuery("#canvasDiv").find("div").eq(index).removeClass("fn-hide");
  22. if (canvasArr[index]) {
  23. //canvas已经存在
  24. window.canvas = canvasArr[index];
  25. return false;
  26. }
  27. var canvas = new fabric.Canvas(canvasId, {
  28. isDrawingMode: false,
  29. skipTargetFind: true,
  30. selectable: false,
  31. selection: false
  32. });
  33. fabric.Image.fromURL(url, function (img) {
  34. canvas.add(img);
  35. }, { crossOrigin: 'anonymous' });
  36. canvasArr[index] = canvas;
  37. canvas.freeDrawingBrush.color = color; //设置自由绘颜色
  38. canvas.freeDrawingBrush.width = drawWidth;
  39. window.canvas = canvas;
  40. //绑定画板事件
  41. canvas.on("mouse:down", function (options) {
  42. var xy = transformMouse(options.e.offsetX, options.e.offsetY);
  43. mouseFrom.x = xy.x;
  44. mouseFrom.y = xy.y;
  45. doDrawing = true;
  46. });
  47. canvas.on("mouse:up", function (options) {
  48. var xy = transformMouse(options.e.offsetX, options.e.offsetY);
  49. mouseTo.x = xy.x;
  50. mouseTo.y = xy.y;
  51. drawing();
  52. drawingObject = null;
  53. moveCount = 1;
  54. doDrawing = false;
  55. if (drawType.indexOf("wrong") != -1) {
  56. //有错题
  57. jQuery("#errModal").removeClass("fn-hide");
  58. jQuery("#errModal").find("div[name=etag]").removeClass("active");
  59. ++appErrCount;
  60. var addLeft = 10, addTop = 25;
  61. if (drawType == "wrong") {
  62. addLeft = 25;
  63. addTop = 30;
  64. }
  65. var textbox = new fabric.Textbox(appErrCount.toString(), {
  66. left: mouseFrom.x + addLeft,
  67. top: mouseFrom.y - addTop,
  68. fontSize: 18,
  69. borderColor: "#2c2c2c",
  70. fill: color,
  71. hasControls: false,
  72. eindex: (appErrCount - 1),
  73. type: "textbox"
  74. });
  75. canvas.add(textbox);
  76. errTextBoxArray[appErrCount] = textbox;
  77. }
  78. });
  79. canvas.on("mouse:move", function (options) {
  80. if (moveCount % 2 && !doDrawing) {
  81. //减少绘制频率
  82. return;
  83. }
  84. moveCount++;
  85. var xy = transformMouse(options.e.offsetX, options.e.offsetY);
  86. mouseTo.x = xy.x;
  87. mouseTo.y = xy.y;
  88. drawing();
  89. });
  90. canvas.on("selection:created", function (e) {
  91. if (e.target._objects) {
  92. //多选删除
  93. var etCount = e.target._objects.length;
  94. for (var etindex = 0; etindex < etCount; etindex++) {
  95. if (e.target._objects[etindex].type == "image" || e.target._objects[etindex].type == "textbox") {
  96. continue;
  97. }
  98. var eindex = e.target._objects[etindex].eindex;
  99. canvas.remove(errTextBoxArray[(1 + eindex)]); //移除对应右上角的数字
  100. delete errTextBoxArray[(1 + eindex)];
  101. canvas.remove(e.target._objects[etindex]);
  102. //移除作业批注
  103. var errMsgChildren = jQuery("#errMsg").children();
  104. errMsgChildren.eq(errMsgChildren.length - 1 - eindex).addClass("fn-hide");
  105. }
  106. } else {
  107. //单选删除
  108. if (e.target.type != "image" && e.target.type != "textbox") {
  109. var eindex = e.target.eindex;
  110. canvas.remove(errTextBoxArray[(1 + eindex)]); //移除对应右上角的数字
  111. delete errTextBoxArray[(1 + eindex)];
  112. canvas.remove(e.target);
  113. //移除作业批注
  114. var errMsgChildren = jQuery("#errMsg").children();
  115. errMsgChildren.eq(errMsgChildren.length - 1 - eindex).addClass("fn-hide");
  116. }
  117. }
  118. canvas.discardActiveObject(); //清楚选中框
  119. });
  120. }
  121. window.zoom = window.zoom ? window.zoom : 1;
  122. // window.drawType = drawType;
  123. //坐标转换
  124. function transformMouse(mouseX, mouseY) {
  125. return { x: mouseX / window.zoom, y: mouseY / window.zoom };
  126. }
  127. //绘画方法
  128. function drawing() {
  129. if (drawingObject) {
  130. canvas.remove(drawingObject);
  131. }
  132. var canvasObject = null;
  133. switch (drawType) {
  134. case 'right': //整题正确
  135. var path =
  136. "M " +
  137. (mouseFrom.x - modifySize) +
  138. " " +
  139. (mouseFrom.y - modifySize) +
  140. " L " +
  141. mouseFrom.x +
  142. " " +
  143. mouseFrom.y +
  144. " L " +
  145. (mouseFrom.x + modifySize * 2) +
  146. " " +
  147. (mouseFrom.y - modifySize * 2);
  148. canvasObject = new fabric.Path(path, {
  149. stroke: rightColor,
  150. strokeWidth: drawWidth,
  151. fill: "rgba(255, 255, 255, 0)"
  152. });
  153. break;
  154. case 'wrong': //整题错误
  155. var path =
  156. "M " +
  157. (mouseFrom.x - modifySize) +
  158. " " +
  159. (mouseFrom.y - modifySize) +
  160. " L " +
  161. (mouseFrom.x + modifySize) +
  162. " " +
  163. (mouseFrom.y + modifySize) +
  164. "M " +
  165. (mouseFrom.x + modifySize) +
  166. " " +
  167. (mouseFrom.y - modifySize) +
  168. " L " +
  169. (mouseFrom.x - modifySize) +
  170. " " +
  171. (mouseFrom.y + modifySize);
  172. canvasObject = new fabric.Path(path, {
  173. stroke: wrongColor,
  174. strokeWidth: drawWidth,
  175. eindex: appErrCount,
  176. fill: "rgba(255, 255, 255, 0)"
  177. });
  178. // canvas.add();
  179. break;
  180. case 'smallright': //题目内单个正确
  181. var path =
  182. "M " +
  183. (mouseFrom.x - modifySmallSize) +
  184. " " +
  185. (mouseFrom.y - modifySmallSize) +
  186. " L " +
  187. mouseFrom.x +
  188. " " +
  189. mouseFrom.y +
  190. " L " +
  191. (mouseFrom.x + modifySmallSize * 2) +
  192. " " +
  193. (mouseFrom.y - modifySmallSize * 2);
  194. canvasObject = new fabric.Path(path, {
  195. stroke: rightColor,
  196. strokeWidth: drawWidth,
  197. fill: "rgba(255, 255, 255, 0)"
  198. });
  199. break;
  200. case 'smallwrong': //题目内单个错误
  201. var path =
  202. "M " +
  203. (mouseFrom.x - modifySmallSize) +
  204. " " +
  205. (mouseFrom.y - modifySmallSize) +
  206. " L " +
  207. (mouseFrom.x + modifySmallSize) +
  208. " " +
  209. (mouseFrom.y + modifySmallSize) +
  210. "M " +
  211. (mouseFrom.x + modifySmallSize) +
  212. " " +
  213. (mouseFrom.y - modifySmallSize) +
  214. " L " +
  215. (mouseFrom.x - modifySmallSize) +
  216. " " +
  217. (mouseFrom.y + modifySmallSize);
  218. canvasObject = new fabric.Path(path, {
  219. stroke: wrongColor,
  220. strokeWidth: drawWidth,
  221. eindex: appErrCount,
  222. fill: "rgba(255, 255, 255, 0)"
  223. });
  224. break;
  225. case "rectangle": //圈题
  226. canvasObject = new fabric.Rect({
  227. top: mouseFrom.y,
  228. left: mouseFrom.x,
  229. width: (mouseTo.x - mouseFrom.x),
  230. height: (mouseTo.y - mouseFrom.y),
  231. stroke: color,
  232. strokeWidth: drawWidth,
  233. fill: "rgba(255, 255, 255, 0)"
  234. });
  235. break;
  236. case "remove":
  237. break;
  238. case "help":
  239. break;
  240. default:
  241. break;
  242. }
  243. if (canvasObject) {
  244. // canvasObject.index = getCanvasObjectIndex();
  245. canvasObject.type = drawType;
  246. canvas.add(canvasObject); //.setActiveObject(canvasObject)
  247. drawingObject = canvasObject;
  248. }
  249. }
  250. //获取画板对象的下标
  251. function getCanvasObjectIndex() {
  252. return canvasObjectIndex++;
  253. }