cccc-graphic.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. 包含Web2D图形绘制及操作的接口
  3. */
  4. let temp = (function () {
  5. /*
  6. 将一组点依次连接起来形成路径
  7. */
  8. let createPath = function (points) {
  9. if (typeof (points) == 'undefined' || !(points instanceof Array) || points.length == 0)
  10. return;
  11. let path = new Path2D();
  12. path.moveTo(points[0].x, points[0].y);
  13. for (let i = 0; i < points.length; i++)
  14. path.lineTo(points[i].x, points[i].y);
  15. return path;
  16. };
  17. //角度转弧度
  18. function degToRad(degrees) {
  19. return degrees * Math.PI / 180;
  20. };
  21. //在矩形中间绘制文本
  22. // function centerTextInRect({ctx, text, textColor = 'black', rect:{x, y, w, h}, font = 0}) {
  23. // if (font)
  24. // ctx.font = font;
  25. // let textSize = ctx.measureText(text);
  26. // ctx.textBaseline = 'middle';
  27. // ctx.fillStyle = textColor;
  28. // ctx.fillText(text, x + (w - textSize.width) / 2, y + h / 2);
  29. // }
  30. /**
  31. * 在矩形中间绘制文本
  32. * @param options : {ctx, text, textColor = 'black', rect:{x, y, w, h}, font = 0}
  33. */
  34. function centerTextInRect(options) {
  35. if (options.font)
  36. options.ctx.font = options.font;
  37. let textSize =options. ctx.measureText(options.text);
  38. options.ctx.textBaseline = 'middle';
  39. options.ctx.fillStyle = options.textColor;
  40. options.ctx.fillText(options.text, options.rect.x + (options.rect.w - textSize.width) / 2, options.rect.y + options.rect.h / 2);
  41. }
  42. //计算一组点所占区域的矩形边界
  43. function outerBound(points) {
  44. if (typeof points == 'undefined') return null;
  45. if (!points instanceof Array || points.length == 0)
  46. return null;
  47. let minX = points[0].x;
  48. let maxX = minX;
  49. let minY = points[0].y;
  50. let maxY = minY;
  51. points.forEach(function (p) {
  52. if (p.x < minX)
  53. minX = p.x;
  54. else if (p.x > maxX)
  55. maxX = p.x;
  56. if (p.y < minY)
  57. minY = p.y;
  58. else if (p.y > maxY)
  59. maxY = p.y;
  60. });
  61. return {
  62. x: minX, y: minY, w: maxX - minX, h: maxY - minY
  63. }
  64. }
  65. /**
  66. * 多边形构造函数
  67. * options: {
  68. ctx, // CanvasRenderingContext2D类型绘图环境
  69. fillColor = 'rgba(0,0,0,0)',
  70. strokeColor = 'rgb(0,0,0)',
  71. canvasColor,
  72. lineWidth = 1
  73. }
  74. */
  75. function Polygon(option) {
  76. for(let attr in option){
  77. eval( 'var ' + attr + ' = option.' + attr )
  78. }
  79. if (cccc.isUndefined(ctx) || !(ctx instanceof CanvasRenderingContext2D))
  80. throw '创建多边形Polygon时请提供CanvasRenderingContext2D类型绘图环境ctx选项';
  81. this.ctx = ctx;
  82. this.path = new Path2D();
  83. this.points = [];
  84. this.finished = false;
  85. this.fillColor = fillColor;
  86. this.strokeColor = strokeColor;
  87. this.lineWidth = lineWidth;
  88. this.canvasColor = canvasColor;
  89. };
  90. Polygon.prototype.addPoint = function (point) { //多边形添加点
  91. for(let attr in point){
  92. eval( 'var ' + attr + ' = point.' + attr )
  93. }
  94. if (this.finished)
  95. throw '多边形polygon已经完成绘制,不能再添加坐标点'
  96. this.points.push({x:x, y:y});
  97. this.redraw();
  98. };
  99. Polygon.prototype.finish = function () {//多边形完成绘制,在绘制最后必须调用此方法,调用finish后不能再调用addPoint
  100. let start = this.points[0];
  101. this.addPoint(start);
  102. this.finished = true;
  103. };
  104. Polygon.prototype.redraw = function () {
  105. if (this.points.length == 0)
  106. return;
  107. let start = this.points[0];
  108. this.path = new Path2D();
  109. this.path.moveTo(start.x, start.y);
  110. for (let i = 1; i < this.points.length; i++) {
  111. let p = this.points[i];
  112. this.path.lineTo(p.x, p.y);
  113. }
  114. this.ctx.save();
  115. this.ctx.fillStyle = this.fillColor;
  116. this.ctx.strokeStyle = this.strokeColor;
  117. this.ctx.lineWidth = this.lineWidth;
  118. this.ctx.fill(this.path);
  119. if (this.lineWidth > 0)
  120. this.ctx.stroke(this.path);
  121. this.ctx.restore();
  122. };
  123. Polygon.prototype.undo = function () {
  124. var savedStyle = {
  125. strokeColor: this.strokeColor,
  126. fillColor: this.fillColor
  127. };
  128. this.strokeColor = this.fillColor = this.canvasColor;
  129. this.redraw();
  130. this.points.pop();
  131. this.finished = false;
  132. this.strokeColor = savedStyle.strokeColor;
  133. this.fillColor = savedStyle.fillColor;
  134. this.redraw();
  135. };
  136. return {
  137. createPath: createPath,
  138. degToRad: degToRad,
  139. centerTextInRect: centerTextInRect,
  140. outerBound: outerBound,
  141. Polygon: Polygon
  142. };
  143. }());
  144. if (typeof cccc != 'undefined' && cccc)
  145. Object.assign(cccc, temp);
  146. else
  147. cccc = temp;