Shake.js 3.1 KB

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