Shake.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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.charting.action2d.Shake"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojox.charting.action2d.Shake"] = true;
  8. dojo.provide("dojox.charting.action2d.Shake");
  9. dojo.require("dojox.charting.action2d.Base");
  10. dojo.require("dojox.gfx.matrix");
  11. dojo.require("dojo.fx");
  12. /*=====
  13. dojo.declare("dojox.charting.action2d.__ShakeCtorArgs", dojox.charting.action2d.__BaseCtorArgs, {
  14. // summary:
  15. // Additional arguments for highlighting actions.
  16. // shift: Number?
  17. // The amount in pixels to shift the pie slice. Default is 3.
  18. shift: 3
  19. });
  20. =====*/
  21. (function(){
  22. var DEFAULT_SHIFT = 3,
  23. m = dojox.gfx.matrix,
  24. gf = dojox.gfx.fx;
  25. dojo.declare("dojox.charting.action2d.Shake", dojox.charting.action2d.Base, {
  26. // summary:
  27. // Create a shaking action for use on an element in a chart.
  28. // the data description block for the widget parser
  29. defaultParams: {
  30. duration: 400, // duration of the action in ms
  31. easing: dojo.fx.easing.backOut, // easing for the action
  32. shiftX: DEFAULT_SHIFT, // shift of the element along the X axis
  33. shiftY: DEFAULT_SHIFT // shift of the element along the Y axis
  34. },
  35. optionalParams: {}, // no optional parameters
  36. constructor: function(chart, plot, kwArgs){
  37. // summary:
  38. // Create the shaking action and connect it to the plot.
  39. // chart: dojox.charting.Chart2D
  40. // The chart this action belongs to.
  41. // plot: String?
  42. // The plot this action is attached to. If not passed, "default" is assumed.
  43. // kwArgs: dojox.charting.action2d.__ShakeCtorArgs?
  44. // Optional keyword arguments object for setting parameters.
  45. if(!kwArgs){ kwArgs = {}; }
  46. this.shiftX = typeof kwArgs.shiftX == "number" ? kwArgs.shiftX : DEFAULT_SHIFT;
  47. this.shiftY = typeof kwArgs.shiftY == "number" ? kwArgs.shiftY : DEFAULT_SHIFT;
  48. this.connect();
  49. },
  50. process: function(o){
  51. // summary:
  52. // Process the action on the given object.
  53. // o: dojox.gfx.Shape
  54. // The object on which to process the slice moving action.
  55. if(!o.shape || !(o.type in this.overOutEvents)){ return; }
  56. var runName = o.run.name, index = o.index, vector = [], anim,
  57. shiftX = o.type == "onmouseover" ? this.shiftX : -this.shiftX,
  58. shiftY = o.type == "onmouseover" ? this.shiftY : -this.shiftY;
  59. if(runName in this.anim){
  60. anim = this.anim[runName][index];
  61. }else{
  62. this.anim[runName] = {};
  63. }
  64. if(anim){
  65. anim.action.stop(true);
  66. }else{
  67. this.anim[runName][index] = anim = {};
  68. }
  69. var kwArgs = {
  70. shape: o.shape,
  71. duration: this.duration,
  72. easing: this.easing,
  73. transform: [
  74. {name: "translate", start: [this.shiftX, this.shiftY], end: [0, 0]},
  75. m.identity
  76. ]
  77. };
  78. if(o.shape){
  79. vector.push(gf.animateTransform(kwArgs));
  80. }
  81. if(o.oultine){
  82. kwArgs.shape = o.outline;
  83. vector.push(gf.animateTransform(kwArgs));
  84. }
  85. if(o.shadow){
  86. kwArgs.shape = o.shadow;
  87. vector.push(gf.animateTransform(kwArgs));
  88. }
  89. if(!vector.length){
  90. delete this.anim[runName][index];
  91. return;
  92. }
  93. anim.action = dojo.fx.combine(vector);
  94. if(o.type == "onmouseout"){
  95. dojo.connect(anim.action, "onEnd", this, function(){
  96. if(this.anim[runName]){
  97. delete this.anim[runName][index];
  98. }
  99. });
  100. }
  101. anim.action.play();
  102. }
  103. });
  104. })();
  105. }