util.js 2.2 KB

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