demo.js 8.2 KB

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