check-bak.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. (function () {
  2. //变量声明
  3. var mouseFrom = {},
  4. mouseTo = {},
  5. drawType = 'right', //画板绘制类型
  6. canvasObjectIndex = 0,
  7. textbox = null;
  8. var drawWidth = 2; //笔触宽度
  9. var color = "#E34F51"; //画笔颜色
  10. var drawingObject = null; //当前绘制对象
  11. var moveCount = 1; //绘制移动计数器
  12. var doDrawing = false; // 绘制状态
  13. var rightColor = "#006400", wrongColor = "#E34F51";
  14. //初始化画板
  15. var canvas = new fabric.Canvas("c", {
  16. isDrawingMode: false,
  17. skipTargetFind: true,
  18. selectable: false,
  19. selection: false
  20. });
  21. fabric.Image.fromURL('http://icdn.apigo.cn/paper.png?1', function (img) {
  22. canvas.add(img);
  23. }, { crossOrigin: 'anonymous' });
  24. window.canvas = canvas;
  25. window.zoom = window.zoom ? window.zoom : 1;
  26. window.drawType = drawType;
  27. canvas.freeDrawingBrush.color = color; //设置自由绘颜色
  28. canvas.freeDrawingBrush.width = drawWidth;
  29. //绑定画板事件
  30. canvas.on("mouse:down", function (options) {
  31. var xy = transformMouse(options.e.offsetX, options.e.offsetY);
  32. mouseFrom.x = xy.x;
  33. mouseFrom.y = xy.y;
  34. doDrawing = true;
  35. });
  36. canvas.on("mouse:up", function (options) {
  37. var xy = transformMouse(options.e.offsetX, options.e.offsetY);
  38. mouseTo.x = xy.x;
  39. mouseTo.y = xy.y;
  40. drawing();
  41. drawingObject = null;
  42. moveCount = 1;
  43. doDrawing = false;
  44. });
  45. canvas.on("mouse:move", function (options) {
  46. if (moveCount % 2 && !doDrawing) {
  47. //减少绘制频率
  48. return;
  49. }
  50. moveCount++;
  51. var xy = transformMouse(options.e.offsetX, options.e.offsetY);
  52. mouseTo.x = xy.x;
  53. mouseTo.y = xy.y;
  54. drawing();
  55. });
  56. canvas.on("selection:created", function (e) {
  57. if (e.target._objects) {
  58. //多选删除
  59. var etCount = e.target._objects.length;
  60. for (var etindex = 0; etindex < etCount; etindex++) {
  61. if (e.target._objects[etindex].type == "image") {
  62. continue;
  63. }
  64. canvas.remove(e.target._objects[etindex]);
  65. }
  66. } else {
  67. //单选删除
  68. if (e.target.type != "image") {
  69. canvas.remove(e.target);
  70. }
  71. }
  72. canvas.discardActiveObject(); //清楚选中框
  73. });
  74. //坐标转换
  75. function transformMouse(mouseX, mouseY) {
  76. return { x: mouseX / window.zoom, y: mouseY / window.zoom };
  77. }
  78. //绑定工具事件
  79. jQuery("#toolsul")
  80. .find("li")
  81. .on("click", function () {
  82. //设置样式
  83. jQuery("#toolsul")
  84. .find("li>i")
  85. .each(function () {
  86. jQuery(this).attr("class", jQuery(this).attr("data-default"));
  87. });
  88. jQuery(this)
  89. .addClass("active")
  90. .siblings()
  91. .removeClass("active");
  92. jQuery(this)
  93. .find("i")
  94. .attr(
  95. "class",
  96. jQuery(this)
  97. .find("i")
  98. .attr("class")
  99. .replace("black", "select")
  100. );
  101. drawType = jQuery(this).attr("data-type");
  102. window.drawType = drawType;
  103. canvas.isDrawingMode = false;
  104. if (textbox) {
  105. //退出文本编辑状态
  106. textbox.exitEditing();
  107. textbox = null;
  108. }
  109. if (drawType == "pen") {
  110. canvas.isDrawingMode = true;
  111. } else if (drawType == "remove") {
  112. canvas.selection = true;
  113. canvas.skipTargetFind = false;
  114. canvas.selectable = true;
  115. } else {
  116. canvas.skipTargetFind = true; //画板元素不能被选中
  117. canvas.selection = false; //画板不显示选中
  118. }
  119. });
  120. //绘画方法
  121. function drawing() {
  122. if (drawingObject) {
  123. canvas.remove(drawingObject);
  124. }
  125. var canvasObject = null;
  126. switch (drawType) {
  127. case "arrow": //箭头
  128. canvasObject = new fabric.Path(drawArrow(mouseFrom.x, mouseFrom.y, mouseTo.x, mouseTo.y, 30, 30), {
  129. stroke: color,
  130. fill: "rgba(255,255,255,0)",
  131. strokeWidth: drawWidth
  132. });
  133. break;
  134. case "line": //直线
  135. canvasObject = new fabric.Line([mouseFrom.x, mouseFrom.y, mouseTo.x, mouseTo.y], {
  136. stroke: color,
  137. strokeWidth: drawWidth
  138. });
  139. break;
  140. case "dottedline": //虚线
  141. canvasObject = new fabric.Line([mouseFrom.x, mouseFrom.y, mouseTo.x, mouseTo.y], {
  142. strokeDashArray: [3, 1],
  143. stroke: color,
  144. strokeWidth: drawWidth
  145. });
  146. break;
  147. case "text":
  148. textbox = new fabric.Textbox("", {
  149. left: mouseFrom.x - 60,
  150. top: mouseFrom.y - 20,
  151. width: 150,
  152. fontSize: 18,
  153. borderColor: "#2c2c2c",
  154. fill: color,
  155. hasControls: false
  156. });
  157. canvas.add(textbox);
  158. textbox.enterEditing();
  159. textbox.hiddenTextarea.focus();
  160. break;
  161. case 'right': //整题正确
  162. var step = 30;
  163. var path =
  164. "M " +
  165. (mouseFrom.x - step) +
  166. " " +
  167. (mouseFrom.y - step) +
  168. " L " +
  169. mouseFrom.x +
  170. " " +
  171. mouseFrom.y +
  172. " L " +
  173. (mouseFrom.x + step * 2) +
  174. " " +
  175. (mouseFrom.y - step * 2);
  176. canvasObject = new fabric.Path(path, {
  177. stroke: rightColor,
  178. strokeWidth: drawWidth,
  179. fill: "rgba(255, 255, 255, 0)"
  180. });
  181. break;
  182. case 'wrong': //整题错误
  183. var step = 30;
  184. var path =
  185. "M " +
  186. (mouseFrom.x - step) +
  187. " " +
  188. (mouseFrom.y - step) +
  189. " L " +
  190. (mouseFrom.x + step) +
  191. " " +
  192. (mouseFrom.y + step) +
  193. "M " +
  194. (mouseFrom.x + step) +
  195. " " +
  196. (mouseFrom.y - step) +
  197. " L " +
  198. (mouseFrom.x - step) +
  199. " " +
  200. (mouseFrom.y + step);
  201. canvasObject = new fabric.Path(path, {
  202. stroke: wrongColor,
  203. strokeWidth: drawWidth,
  204. fill: "rgba(255, 255, 255, 0)"
  205. });
  206. break;
  207. case 'smallright': //题目内单个正确
  208. var step = 10;
  209. var path =
  210. "M " +
  211. (mouseFrom.x - step) +
  212. " " +
  213. (mouseFrom.y - step) +
  214. " L " +
  215. mouseFrom.x +
  216. " " +
  217. mouseFrom.y +
  218. " L " +
  219. (mouseFrom.x + step * 2) +
  220. " " +
  221. (mouseFrom.y - step * 2);
  222. canvasObject = new fabric.Path(path, {
  223. stroke: rightColor,
  224. strokeWidth: drawWidth,
  225. fill: "rgba(255, 255, 255, 0)"
  226. });
  227. break;
  228. case 'smallwrong': //题目内单个错误
  229. var step = 10;
  230. var path =
  231. "M " +
  232. (mouseFrom.x - step) +
  233. " " +
  234. (mouseFrom.y - step) +
  235. " L " +
  236. (mouseFrom.x + step) +
  237. " " +
  238. (mouseFrom.y + step) +
  239. "M " +
  240. (mouseFrom.x + step) +
  241. " " +
  242. (mouseFrom.y - step) +
  243. " L " +
  244. (mouseFrom.x - step) +
  245. " " +
  246. (mouseFrom.y + step);
  247. canvasObject = new fabric.Path(path, {
  248. stroke: wrongColor,
  249. strokeWidth: drawWidth,
  250. fill: "rgba(255, 255, 255, 0)"
  251. });
  252. break;
  253. case "remove":
  254. break;
  255. default:
  256. break;
  257. }
  258. if (canvasObject) {
  259. // canvasObject.index = getCanvasObjectIndex();
  260. canvasObject.type = drawType;
  261. canvas.add(canvasObject); //.setActiveObject(canvasObject)
  262. drawingObject = canvasObject;
  263. }
  264. }
  265. //绘制箭头方法
  266. function drawArrow(fromX, fromY, toX, toY, theta, headlen) {
  267. theta = typeof theta != "undefined" ? theta : 30;
  268. headlen = typeof theta != "undefined" ? headlen : 10;
  269. // 计算各角度和对应的P2,P3坐标
  270. var angle = Math.atan2(fromY - toY, fromX - toX) * 180 / Math.PI,
  271. angle1 = (angle + theta) * Math.PI / 180,
  272. angle2 = (angle - theta) * Math.PI / 180,
  273. topX = headlen * Math.cos(angle1),
  274. topY = headlen * Math.sin(angle1),
  275. botX = headlen * Math.cos(angle2),
  276. botY = headlen * Math.sin(angle2);
  277. var arrowX = fromX - topX,
  278. arrowY = fromY - topY;
  279. var path = " M " + fromX + " " + fromY;
  280. path += " L " + toX + " " + toY;
  281. arrowX = toX + topX;
  282. arrowY = toY + topY;
  283. path += " M " + arrowX + " " + arrowY;
  284. path += " L " + toX + " " + toY;
  285. arrowX = toX + botX;
  286. arrowY = toY + botY;
  287. path += " L " + arrowX + " " + arrowY;
  288. return path;
  289. }
  290. //获取画板对象的下标
  291. function getCanvasObjectIndex() {
  292. return canvasObjectIndex++;
  293. }
  294. })();