util.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. //工具类型文件
  2. CanvasSketch.lastId = 0;
  3. //取得id。
  4. CanvasSketch.getId = function (preString) {
  5. CanvasSketch.lastId += 1;
  6. return preString + CanvasSketch.lastId;
  7. };
  8. //图形的范围
  9. CanvasSketch.Bounds = function (x1, y1, x2, y2) {
  10. this.leftBottom = new CanvasSketch.Position(x1, y1);
  11. this.rigthTop = new CanvasSketch.Position(x2, y2);
  12. this.leftTop = new CanvasSketch.Position(x1, y2);
  13. this.rightBottom = new CanvasSketch.Position(x2, y1);
  14. this.left = x1;
  15. this.right = x2;
  16. this.bottom = y1;
  17. this.top = y2;
  18. };
  19. CanvasSketch.Bounds.prototype.getCenter = function (){
  20. var w = this.right - this.left;
  21. var h = this.top - this.bottom;
  22. return new CanvasSketch.Position(this.left + w/2, this.bottom + h/2);
  23. };
  24. //位置信息类
  25. CanvasSketch.Position = function (x, y) {
  26. this.x = x;
  27. this.y = y;
  28. };
  29. //大小类
  30. CanvasSketch.Size = function (w, h) {
  31. this.w = w;
  32. this.h = h;
  33. };
  34. //矢量图形的默认样式
  35. CanvasSketch.defaultStyle = function () {
  36. this.fill = true;
  37. this.stroke = true;
  38. this.pointRadius = 5;
  39. this.fillOpacity = 0.6;
  40. this.strokeOpacity = 1;
  41. this.fillColor = "red";
  42. this.strokeColor = "black";
  43. };
  44. //保存时间的this。
  45. CanvasSketch.bindAsEventListener = function(func, object) {
  46. return function(event) {
  47. return func.call(object, event || window.event);
  48. };
  49. };
  50. //阻止事件冒泡
  51. CanvasSketch.stopEventBubble = function(e) {
  52. if (e.preventDefault) {
  53. e.preventDefault();
  54. } else {
  55. e.returnValue = false;
  56. }
  57. if (e && e.stopPropagation)
  58. e.stopPropagation();
  59. else
  60. window.event.cancelBubble=true;
  61. };