Canvas.js 5.1 KB

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