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