Canvas.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. // wrapped by build app
  2. define("dojox/drawing/manager/Canvas", ["dijit","dojo","dojox"], function(dijit,dojo,dojox){
  3. dojo.provide("dojox.drawing.manager.Canvas");
  4. (function(){
  5. dojox.drawing.manager.Canvas = dojox.drawing.util.oo.declare(
  6. // summary:
  7. // Creates a dojox.gfx.surface to be used for Drawing. Note that
  8. // The 'surface' that Drawing uses is actually a dojox.gfx.group.
  9. // This allows for more versatility.
  10. //
  11. // Called internally from a dojox.Drawing.
  12. //
  13. // Note: Surface creation is asynchrous. Connect to
  14. // onSurfaceReady in Drawing.
  15. //
  16. function(/*Object*/options){
  17. dojo.mixin(this, options);
  18. var dim = dojo.contentBox(this.srcRefNode);
  19. this.height = this.parentHeight = dim.h;
  20. this.width = this.parentWidth = dim.w;
  21. this.domNode = dojo.create("div", {id:"canvasNode"}, this.srcRefNode);
  22. dojo.style(this.domNode, {
  23. width:this.width,
  24. height:"auto"
  25. });
  26. dojo.setSelectable(this.domNode, false);
  27. this.id = this.id || this.util.uid("surface");
  28. console.info("create canvas");
  29. this.gfxSurface = dojox.gfx.createSurface(this.domNode, this.width, this.height);
  30. this.gfxSurface.whenLoaded(this, function(){
  31. setTimeout(dojo.hitch(this, function(){
  32. this.surfaceReady = true;
  33. if(dojo.isIE){
  34. //this.gfxSurface.rawNode.parentNode.id = this.id;
  35. }else if(dojox.gfx.renderer == "silverlight"){
  36. this.id = this.domNode.firstChild.id
  37. }else{
  38. //this.gfxSurface.rawNode.id = this.id;
  39. }
  40. this.underlay = this.gfxSurface.createGroup();
  41. this.surface = this.gfxSurface.createGroup();
  42. this.overlay = this.gfxSurface.createGroup();
  43. this.surface.setTransform({dx:0, dy:0,xx:1,yy:1});
  44. this.gfxSurface.getDimensions = dojo.hitch(this.gfxSurface, "getDimensions");
  45. if(options.callback){
  46. options.callback(this.domNode);
  47. }
  48. }),500);
  49. });
  50. this._mouseHandle = this.mouse.register(this);
  51. },
  52. {
  53. // zoom: [readonly] Number
  54. // The amount the canvas is zoomed
  55. zoom:1,
  56. useScrollbars: true,
  57. baseClass:"drawingCanvas",
  58. resize: function(width, height){
  59. // summary:
  60. // Method used to change size of canvas. Potentially
  61. // called from a container like ContentPane. May be
  62. // called directly.
  63. //
  64. this.parentWidth = width;
  65. this.parentHeight = height;
  66. this.setDimensions(width, height);
  67. },
  68. setDimensions: function(width, height, scrollx, scrolly){
  69. // summary:
  70. // Internal. Changes canvas size and sets scroll position.
  71. // Do not call this, use resize().
  72. //
  73. // changing the size of the surface and setting scroll
  74. // if items are off screen
  75. var sw = this.getScrollWidth(); //+ 10;
  76. this.width = Math.max(width, this.parentWidth);
  77. this.height = Math.max(height, this.parentHeight);
  78. if(this.height>this.parentHeight){
  79. this.width -= sw;
  80. }
  81. if(this.width>this.parentWidth){
  82. this.height -= sw;
  83. }
  84. this.mouse.resize(this.width,this.height);
  85. this.gfxSurface.setDimensions(this.width, this.height);
  86. this.domNode.parentNode.scrollTop = scrolly || 0;
  87. this.domNode.parentNode.scrollLeft = scrollx || 0;
  88. if(this.useScrollbars){
  89. //console.info("Set Canvas Scroll", (this.height > this.parentHeight), this.height, this.parentHeight)
  90. dojo.style(this.domNode.parentNode, {
  91. overflowY: this.height > this.parentHeight ? "scroll" : "hidden",
  92. overflowX: this.width > this.parentWidth ? "scroll" : "hidden"
  93. });
  94. }else{
  95. dojo.style(this.domNode.parentNode, {
  96. overflowY: "hidden",
  97. overflowX: "hidden"
  98. });
  99. }
  100. },
  101. setZoom: function(zoom){
  102. // summary:
  103. // Internal. Zooms canvas in and out.
  104. this.zoom = zoom;
  105. this.surface.setTransform({xx:zoom, yy:zoom});
  106. this.setDimensions(this.width*zoom, this.height*zoom)
  107. },
  108. onScroll: function(){
  109. // summary:
  110. // Event fires on scroll.NOT IMPLEMENTED
  111. },
  112. getScrollOffset: function(){
  113. // summary:
  114. // Get the scroll position of the canvas
  115. return {
  116. top:this.domNode.parentNode.scrollTop,
  117. left:this.domNode.parentNode.scrollLeft
  118. }; // Object
  119. },
  120. getScrollWidth: function(){
  121. // summary:
  122. // Special method used to detect the width (and height)
  123. // of the browser scrollbars. Becomes memoized.
  124. //
  125. var p = dojo.create('div');
  126. p.innerHTML = '<div style="width:50px;height:50px;overflow:hidden;position:absolute;top:0;left:-1000px;"><div style="height:100px;"></div>';
  127. var div = p.firstChild;
  128. dojo.body().appendChild(div);
  129. var noscroll = dojo.contentBox(div).h;
  130. dojo.style(div, "overflow", "scroll");
  131. var scrollWidth = noscroll - dojo.contentBox(div).h;
  132. dojo.destroy(div);
  133. this.getScrollWidth = function(){
  134. return scrollWidth;
  135. };
  136. return scrollWidth; // Object
  137. }
  138. }
  139. );
  140. })();
  141. });