util.js 2.3 KB

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