modifyelements.html 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <!DOCTYPE HTML>
  2. <html>
  3. <!--
  4. Copyright 2007 The Closure Library Authors. All Rights Reserved.
  5. Use of this source code is governed by the Apache License, Version 2.0.
  6. See the COPYING file for details.
  7. -->
  8. <head>
  9. <meta http-equiv="X-UA-Compatible" content="IE=edge" >
  10. <title>Modifing Graphic Elements Demo</title>
  11. <script type="text/javascript" src="../../base.js"></script>
  12. <script type="text/javascript">
  13. goog.require('goog.dom');
  14. goog.require('goog.graphics');
  15. goog.require('goog.graphics.Font');
  16. </script>
  17. <script type="text/javascript">
  18. /**
  19. * A rectangle, returned from graphics.drawRect.
  20. * @type goog.graphics.RectElement.
  21. */
  22. var rectElement;
  23. /**
  24. * An ellipse, returned from graphics.drawEllipse.
  25. * @type goog.graphics.EllipseElement.
  26. */
  27. var ellipseElement;
  28. /**
  29. * A path element, returned from graphics.drawPath.
  30. * @type goog.graphics.PathElement.
  31. */
  32. var pathElement;
  33. /**
  34. * A text element, returned from graphics.drawText
  35. * @type goog.graphics.PathElement.
  36. */
  37. var textElement;
  38. var graphics, fill, stroke, font;
  39. var rectColor = [];
  40. var pathData1, pathData2;
  41. function setupElements() {
  42. graphics = goog.graphics.createGraphics(600, 200);
  43. fill = new goog.graphics.SolidFill('yellow');
  44. stroke = new goog.graphics.Stroke(2, 'green');
  45. font = new goog.graphics.Font(26, 'Arial');
  46. rectColor.push({s: stroke, f: fill});
  47. rectColor.push({s: new goog.graphics.Stroke(4, 'blue'),
  48. f: new goog.graphics.SolidFill('red')});
  49. rectColor.push({s: null, f: new goog.graphics.SolidFill('#c0c0c0')});
  50. rectColor.push({s: new goog.graphics.Stroke(0.5, 'red'), f: null});
  51. var gradient = new goog.graphics.LinearGradient(0, 0, 0, 300, '#8080ff',
  52. '#000080');
  53. rectColor.push({s: new goog.graphics.Stroke(1, 'black'), f: gradient});
  54. drawElements();
  55. graphics.render(document.getElementById('shapes'));
  56. }
  57. function drawElements() {
  58. rectElement = graphics.drawRect(30, 10, 100, 80, stroke, fill);
  59. ellipseElement = graphics.drawEllipse(400, 150, 100, 40, stroke, fill);
  60. pathData1 = graphics.createPath()
  61. .moveTo(200, 180)
  62. .lineTo(230, 100)
  63. .lineTo(280, 30)
  64. .lineTo(280, 80)
  65. .lineTo(200, 90);
  66. pathData2 = graphics.createPath()
  67. .moveTo(200, 180)
  68. .curveTo(220, 50, 260, 180, 280, 30);
  69. pathElement = graphics.drawPath(pathData1, stroke, null);
  70. textElement = graphics.drawTextOnLine(
  71. document.getElementById('text').value,
  72. 0, 20, 590, 20, 'right', font, stroke, fill);
  73. }
  74. function setRectColors(index) {
  75. var c = rectColor[index];
  76. rectElement.setFill(c.f);
  77. rectElement.setStroke(c.s);
  78. ellipseElement.setFill(c.f);
  79. ellipseElement.setStroke(c.s);
  80. pathElement.setStroke(c.s);
  81. textElement.setStroke(c.s);
  82. textElement.setFill(c.f);
  83. }
  84. function setRectPosition(x, y) {
  85. rectElement.setPosition(x, y);
  86. }
  87. function setRectSize(width, height) {
  88. rectElement.setSize(width, height);
  89. }
  90. function setEllipseCenter(cx, cy) {
  91. ellipseElement.setCenter(cx, cy);
  92. }
  93. function setEllipseRadius(rx, ry) {
  94. ellipseElement.setRadius(rx, ry);
  95. }
  96. function setPath(i) {
  97. pathElement.setPath(i == 1 ? pathData1 : pathData2);
  98. }
  99. function setText() {
  100. textElement.setText(document.getElementById('text').value);
  101. }
  102. </script>
  103. </head>
  104. <body onload="setupElements()">
  105. <div id="shapes"
  106. style="border:1px solid black; width:600px; height:200px;"></div>
  107. <table>
  108. <tr valign="top">
  109. <td>Colors (stroke/fill):</td>
  110. <td>
  111. <input type="button" value="Green(2):yellow" onclick="setRectColors(0)">
  112. <input type="button" value="Blue(4):red" onclick="setRectColors(1)">
  113. <input type="button" value="null:#c0c0c0" onclick="setRectColors(2)">
  114. <input type="button" value="Red(0.5):null" onclick="setRectColors(3)">
  115. <input type="button" value="Gradient" onclick="setRectColors(4)">
  116. </td>
  117. </tr>
  118. <tr valign="top">
  119. <td>Rectangle position:</td>
  120. <td>
  121. <input type="button" value="30,30" onclick="setRectPosition(30, 10)">
  122. <input type="button" value="200,20" onclick="setRectPosition(200, 20)">
  123. <input type="button" value="0,60" onclick="setRectPosition(0, 60)">
  124. </td>
  125. </tr>
  126. <tr valign="top">
  127. <td>Rectangle size:</td>
  128. <td>
  129. <input type="button" value="100,80" onclick="setRectSize(100, 80)">
  130. <input type="button" value="120,120" onclick="setRectSize(120, 120)">
  131. <input type="button" value="40,60" onclick="setRectSize(40, 60)">
  132. </td>
  133. </tr>
  134. <tr valign="top">
  135. <td>Ellipse center:</td>
  136. <td>
  137. <input type="button" value="400,150"
  138. onclick="setEllipseCenter(400, 150)">
  139. <input type="button" value="200,80"
  140. onclick="setEllipseCenter(200, 80)">
  141. <input type="button" value="350,200"
  142. onclick="setEllipseCenter(350, 200)">
  143. </td>
  144. </tr>
  145. <tr valign="top">
  146. <td>Ellipse radius:</td>
  147. <td>
  148. <input type="button" value="100,40" onclick="setEllipseRadius(100, 40)">
  149. <input type="button" value="80,80" onclick="setEllipseRadius(80, 80)">
  150. <input type="button" value="40,60" onclick="setEllipseRadius(40, 60)">
  151. </td>
  152. </tr>
  153. <tr valign="top">
  154. <td>Path:</td>
  155. <td>
  156. <input type="button" value="Line" onclick="setPath(1)">
  157. <input type="button" value="Curve" onclick="setPath(2)">
  158. </td>
  159. </tr>
  160. <tr valign="top">
  161. <td>Text:</td>
  162. <td>
  163. <input type="text" id="text" value="Text Sample" onkeyup="setText()"
  164. onchange="setText()" size="20">
  165. </td>
  166. </tr>
  167. <tr valign="top">
  168. <td colspan="2">
  169. <input type="button" value="Clear Surface" onclick="graphics.clear()">
  170. <input type="button" value="Redraw Elements"
  171. onclick="graphics.clear(); drawElements()">
  172. </td>
  173. </tr>
  174. </table>
  175. </body>
  176. </html>