elementOperation.html 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  7. <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/astrobench@0.1.2/src/style.css">
  8. <script src="../dist/zrender.js"></script>
  9. <!-- <script src="https://cdn.jsdelivr.net/npm/zrender@4.2.0/dist/zrender.js"></script> -->
  10. <title>Element Operation</title>
  11. </head>
  12. <body>
  13. <div id="astrobench"></div>
  14. <script src="https://cdn.jsdelivr.net/npm/astrobench@0.1.2/dist/astrobench.js"></script>
  15. <script type="text/javascript">
  16. let zr = zrender.init(document.createElement('canvas'));
  17. // Stop timeline to avoid trigger render
  18. zr.animation.stop();
  19. const commonOpts = {
  20. teardown() {
  21. zr.clear();
  22. }
  23. }
  24. suite('Create Element', function (suite) {
  25. bench('Group', function () {
  26. const group = new zrender.Group();
  27. zr.add(group);
  28. }, commonOpts);
  29. bench('Rect', function () {
  30. const rect = new zrender.Rect();
  31. zr.add(rect);
  32. }, commonOpts);
  33. bench('Circle', function () {
  34. const circle = new zrender.Circle();
  35. zr.add(circle);
  36. }, commonOpts);
  37. bench('Image', function () {
  38. const image = new zrender.Image();
  39. zr.add(image);
  40. }, commonOpts);
  41. bench('Text', function () {
  42. const text = new zrender.Text();
  43. zr.add(text);
  44. }, commonOpts);
  45. });
  46. suite('Attr', function (suite) {
  47. let group;
  48. let rect;
  49. const attrCommonOpts = Object.assign({
  50. setup() {
  51. group = new zrender.Group();
  52. zr.add(group);
  53. rect = new zrender.Rect();
  54. zr.add(rect);
  55. }
  56. }, commonOpts)
  57. bench('Group#attr(transform)', function () {
  58. group.attr({
  59. x: 10,
  60. y: 10
  61. });
  62. }, attrCommonOpts);
  63. bench('Rect#setShape', function () {
  64. rect.setShape({
  65. x: 10,
  66. y: 10,
  67. width: 100,
  68. height: 100
  69. });
  70. }, attrCommonOpts);
  71. bench('Rect#setStyle', function () {
  72. rect.setStyle({
  73. fill: 'red',
  74. stroke: 'black',
  75. lineWidth: 2
  76. })
  77. }, attrCommonOpts);
  78. });
  79. suite('Animation', function (suite) {
  80. let rect;
  81. const attrCommonOpts = Object.assign({
  82. setup() {
  83. rect = new zrender.Rect();
  84. zr.add(rect);
  85. }
  86. }, commonOpts)
  87. bench('Animation#animate(position)', function () {
  88. const animator = zr.animation.animate(rect)
  89. .when(1000, {
  90. x: 10,
  91. y: 10
  92. })
  93. .during(function () {
  94. rect.dirty()
  95. })
  96. .start();
  97. animator.stop();
  98. }, attrCommonOpts);
  99. bench('Element#animate(position)', function () {
  100. rect.animate()
  101. .when(1000, {
  102. x: 10,
  103. y: 10
  104. })
  105. .start();
  106. // Stop immediately
  107. rect.stopAnimation();
  108. }, attrCommonOpts);
  109. bench('Element#animateTo(position)', function () {
  110. rect.animateTo({
  111. x: 10,
  112. y: 10
  113. }, { duration: 1000});
  114. rect.stopAnimation();
  115. }, attrCommonOpts);
  116. bench('Element#animateTo(shape)', function () {
  117. rect.animateTo({
  118. shape: {
  119. x: 10,
  120. y: 10,
  121. width: 100,
  122. height: 100
  123. }
  124. }, { duration: 1000});
  125. rect.stopAnimation();
  126. }, attrCommonOpts);
  127. bench('Element#animateTo(style)', function () {
  128. rect.animateTo({
  129. style: {
  130. fill: 'red',
  131. stroke: 'green',
  132. lineWidth: 2
  133. }
  134. }, { duration: 1000});
  135. rect.stopAnimation();
  136. }, attrCommonOpts);
  137. });
  138. suite('State', function (suite) {
  139. let rect;
  140. const attrCommonOpts = Object.assign({
  141. setup() {
  142. rect = new zrender.Rect();
  143. rect.states = {
  144. emphasis: {
  145. style: {
  146. fill: 'red',
  147. stroke: 'blue'
  148. },
  149. shape: {
  150. width: 100,
  151. height: 100
  152. }
  153. },
  154. selected: {
  155. style: {
  156. fill: 'purple',
  157. lineWidth: 1
  158. }
  159. }
  160. }
  161. zr.add(rect);
  162. }
  163. }, commonOpts)
  164. bench('Animation#useState', function () {
  165. rect.useState('emphasis');
  166. rect.useState('selected');
  167. }, attrCommonOpts);
  168. bench('Element#useStates', function () {
  169. rect.useStates(['emphasis', 'selected']);
  170. }, attrCommonOpts);
  171. bench('Element#clearStates', function () {
  172. rect.useState('selected');
  173. rect.clearStates();
  174. }, attrCommonOpts);
  175. });
  176. </script>
  177. </body>
  178. </html>