|
525
|
1 |
<!DOCTYPE html> |
|
|
2 |
<html lang="en"> |
|
|
3 |
<head> |
|
|
4 |
<meta charset="utf-8"> |
|
|
5 |
<title>Example: Custom Shape</title> |
|
|
6 |
<link rel="stylesheet" href="http://fonts.googleapis.com/css?family=PT+Sans:400,700,400italic,700italic"> |
|
|
7 |
<link rel="stylesheet" href="../../build/cssgrids/cssgrids-min.css"> |
|
|
8 |
<link rel="stylesheet" href="../assets/css/main.css"> |
|
|
9 |
<link rel="stylesheet" href="../assets/vendor/prettify/prettify-min.css"> |
|
|
10 |
<link rel="shortcut icon" type="image/png" href="../assets/favicon.png"> |
|
|
11 |
<script src="../../build/yui/yui-min.js"></script> |
|
|
12 |
|
|
|
13 |
</head> |
|
|
14 |
<body> |
|
|
15 |
<!-- |
|
|
16 |
<a href="https://github.com/yui/yui3"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://s3.amazonaws.com/github/ribbons/forkme_right_darkblue_121621.png" alt="Fork me on GitHub"></a> |
|
|
17 |
--> |
|
|
18 |
<div id="doc"> |
|
|
19 |
<div id="hd"> |
|
|
20 |
<h1><img src="http://yuilibrary.com/img/yui-logo.png"></h1> |
|
|
21 |
</div> |
|
|
22 |
|
|
|
23 |
|
|
|
24 |
<h1>Example: Custom Shape</h1> |
|
|
25 |
<div class="yui3-g"> |
|
|
26 |
<div class="yui3-u-3-4"> |
|
|
27 |
<div id="main"> |
|
|
28 |
<div class="content"><style scoped> |
|
|
29 |
#custom-doc { width: 95%; min-width: 950px; } |
|
|
30 |
#pagetitle {background-image: url(../../assets/bg_hd.gif);} |
|
|
31 |
#mygraphiccontainer { |
|
|
32 |
position: relative; |
|
|
33 |
width: 400px; |
|
|
34 |
height: 300px; |
|
|
35 |
} |
|
|
36 |
</style> |
|
|
37 |
<div class="intro"> |
|
|
38 |
<p> |
|
|
39 |
This example shows how to use the <code>Graphics</code> to create a custom shape. Currently, the <code>Graphics</code> module has four shapes: |
|
|
40 |
</p> |
|
|
41 |
<ul> |
|
|
42 |
<li>rect</li> |
|
|
43 |
<li>circle</li> |
|
|
44 |
<li>ellipse</li> |
|
|
45 |
<li>path</li> |
|
|
46 |
</ul> |
|
|
47 |
<p> |
|
|
48 |
You can also create your own custom shapes. If you need to have reusable shapes, you can do this by extending the <code>Shape</code> class. Once you have created a custom class, you can instantiate it by passing |
|
|
49 |
a reference of your class in the <code>type</code> attribute of your config to the <code>addShape</code> method. In this example, we will create shape called <code>RoundedRect</code>. |
|
|
50 |
</p> |
|
|
51 |
</div> |
|
|
52 |
<div class="example"> |
|
|
53 |
<div id="mygraphiccontainer"></div> |
|
|
54 |
<script> |
|
|
55 |
YUI({filter:"raw"}).use('graphics', function (Y) |
|
|
56 |
{ |
|
|
57 |
var RoundedRect = function() |
|
|
58 |
{ |
|
|
59 |
RoundedRect.superclass.constructor.apply(this, arguments); |
|
|
60 |
} |
|
|
61 |
RoundedRect.NAME = "roundedRect"; |
|
|
62 |
Y.extend(RoundedRect, Y.Shape, { |
|
|
63 |
_draw: function() |
|
|
64 |
{ |
|
|
65 |
var w = this.get("width"), |
|
|
66 |
h = this.get("height"), |
|
|
67 |
ew = this.get("ellipseWidth"), |
|
|
68 |
eh = this.get("ellipseHeight"); |
|
|
69 |
this.clear(); |
|
|
70 |
this.moveTo(0, eh); |
|
|
71 |
this.lineTo(0, h - eh); |
|
|
72 |
this.quadraticCurveTo(0, h, ew, h); |
|
|
73 |
this.lineTo(w - ew, h); |
|
|
74 |
this.quadraticCurveTo(w, h, w, h - eh); |
|
|
75 |
this.lineTo(w, eh); |
|
|
76 |
this.quadraticCurveTo(w, 0, w - ew, 0); |
|
|
77 |
this.lineTo(ew, 0); |
|
|
78 |
this.quadraticCurveTo(0, 0, 0, eh); |
|
|
79 |
this.end(); |
|
|
80 |
} |
|
|
81 |
}, { |
|
|
82 |
ATTRS: Y.mix({ |
|
|
83 |
ellipseWidth: { |
|
|
84 |
value: 4 |
|
|
85 |
}, |
|
|
86 |
|
|
|
87 |
ellipseHeight: { |
|
|
88 |
value: 4 |
|
|
89 |
} |
|
|
90 |
}, Y.Shape.ATTRS) |
|
|
91 |
}); |
|
|
92 |
Y.RoundedRect = RoundedRect; |
|
|
93 |
|
|
|
94 |
var mygraphic = new Y.Graphic({render: "#mygraphiccontainer"}), |
|
|
95 |
myroundrect = mygraphic.addShape({ |
|
|
96 |
type: Y.RoundedRect, |
|
|
97 |
width: 300, |
|
|
98 |
height: 200, |
|
|
99 |
x: 50, |
|
|
100 |
y: 50, |
|
|
101 |
ellipseWidth: 12, |
|
|
102 |
ellipseHeight: 12, |
|
|
103 |
fill: { |
|
|
104 |
type: "linear", |
|
|
105 |
stops: [ |
|
|
106 |
{color: "#9aa9bb", offset: 0}, |
|
|
107 |
{color: "#eeefff", offset: 0.4}, |
|
|
108 |
{color: "#00000f", offset: 0.8}, |
|
|
109 |
{color: "#9aa9bb", offset: 1} |
|
|
110 |
], |
|
|
111 |
rotation: 45 |
|
|
112 |
}, |
|
|
113 |
stroke: { |
|
|
114 |
weight: 2, |
|
|
115 |
color: "#000" |
|
|
116 |
} |
|
|
117 |
}); |
|
|
118 |
}); |
|
|
119 |
</script> |
|
|
120 |
|
|
|
121 |
</div> |
|
|
122 |
|
|
|
123 |
<h2>HTML</h2> |
|
|
124 |
<pre class="code prettyprint"><div id="mygraphiccontainer"></div></pre> |
|
|
125 |
|
|
|
126 |
|
|
|
127 |
<h2>CSS</h2> |
|
|
128 |
<pre class="code prettyprint">#mygraphiccontainer { |
|
|
129 |
position: relative; |
|
|
130 |
width: 400px; |
|
|
131 |
height: 300px; |
|
|
132 |
}</pre> |
|
|
133 |
|
|
|
134 |
<h2>JavaScript</h2> |
|
|
135 |
|
|
|
136 |
<p>Create a custom class. When creating shapes, add a method called <code>_draw</code>. This is where you will put your drawing logic for the custom shape. You will also need to mix in any attributes that you need.</p> |
|
|
137 |
|
|
|
138 |
<pre class="code prettyprint">var RoundedRect = function() |
|
|
139 |
{ |
|
|
140 |
RoundedRect.superclass.constructor.apply(this, arguments); |
|
|
141 |
} |
|
|
142 |
RoundedRect.NAME = "roundedRect"; |
|
|
143 |
Y.extend(RoundedRect, Y.Shape, { |
|
|
144 |
_draw: function() |
|
|
145 |
{ |
|
|
146 |
var w = this.get("width"), |
|
|
147 |
h = this.get("height"), |
|
|
148 |
ew = this.get("ellipseWidth"), |
|
|
149 |
eh = this.get("ellipseHeight"); |
|
|
150 |
this.clear(); |
|
|
151 |
this.moveTo(0, eh); |
|
|
152 |
this.lineTo(0, h - eh); |
|
|
153 |
this.quadraticCurveTo(0, h, ew, h); |
|
|
154 |
this.lineTo(w - ew, h); |
|
|
155 |
this.quadraticCurveTo(w, h, w, h - eh); |
|
|
156 |
this.lineTo(w, eh); |
|
|
157 |
this.quadraticCurveTo(w, 0, w - ew, 0); |
|
|
158 |
this.lineTo(ew, 0); |
|
|
159 |
this.quadraticCurveTo(0, 0, 0, eh); |
|
|
160 |
this.end(); |
|
|
161 |
} |
|
|
162 |
}, { |
|
|
163 |
ATTRS: Y.mix({ |
|
|
164 |
ellipseWidth: { |
|
|
165 |
value: 4 |
|
|
166 |
}, |
|
|
167 |
|
|
|
168 |
ellipseHeight: { |
|
|
169 |
value: 4 |
|
|
170 |
} |
|
|
171 |
}, Y.Shape.ATTRS) |
|
|
172 |
}); |
|
|
173 |
Y.RoundedRect = RoundedRect;</pre> |
|
|
174 |
|
|
|
175 |
<p>Create a <code>Graphic</code> instance and render it to an <code>HTMLElement</code></p> |
|
|
176 |
<pre class="code prettyprint">var mygraphic = new Y.Graphic({render: "#mygraphiccontainer"}),</pre> |
|
|
177 |
|
|
|
178 |
|
|
|
179 |
<p>Using the <code>addShape</code> method, add an instance of the custom class to the <code>Graphic</code> instance.</p> |
|
|
180 |
|
|
|
181 |
<pre class="code prettyprint">myroundrect = mygraphic.addShape({ |
|
|
182 |
type: Y.RoundedRect, |
|
|
183 |
width: 300, |
|
|
184 |
height: 200, |
|
|
185 |
x: 50, |
|
|
186 |
y: 50, |
|
|
187 |
ellipseWidth: 12, |
|
|
188 |
ellipseHeight: 12, |
|
|
189 |
fill: { |
|
|
190 |
type: "linear", |
|
|
191 |
stops: [ |
|
|
192 |
{color: "#9aa9bb", offset: 0}, |
|
|
193 |
{color: "#eeefff", offset: 0.4}, |
|
|
194 |
{color: "#00000f", offset: 0.8}, |
|
|
195 |
{color: "#9aa9bb", offset: 1} |
|
|
196 |
], |
|
|
197 |
rotation: 45 |
|
|
198 |
}, |
|
|
199 |
stroke: { |
|
|
200 |
weight: 2, |
|
|
201 |
color: "#000" |
|
|
202 |
} |
|
|
203 |
});</pre> |
|
|
204 |
|
|
|
205 |
<h2>Complete Example Source</h2> |
|
|
206 |
<pre class="code prettyprint"><div id="mygraphiccontainer"></div> |
|
|
207 |
<script> |
|
|
208 |
YUI({filter:"raw"}).use('graphics', function (Y) |
|
|
209 |
{ |
|
|
210 |
var RoundedRect = function() |
|
|
211 |
{ |
|
|
212 |
RoundedRect.superclass.constructor.apply(this, arguments); |
|
|
213 |
} |
|
|
214 |
RoundedRect.NAME = "roundedRect"; |
|
|
215 |
Y.extend(RoundedRect, Y.Shape, { |
|
|
216 |
_draw: function() |
|
|
217 |
{ |
|
|
218 |
var w = this.get("width"), |
|
|
219 |
h = this.get("height"), |
|
|
220 |
ew = this.get("ellipseWidth"), |
|
|
221 |
eh = this.get("ellipseHeight"); |
|
|
222 |
this.clear(); |
|
|
223 |
this.moveTo(0, eh); |
|
|
224 |
this.lineTo(0, h - eh); |
|
|
225 |
this.quadraticCurveTo(0, h, ew, h); |
|
|
226 |
this.lineTo(w - ew, h); |
|
|
227 |
this.quadraticCurveTo(w, h, w, h - eh); |
|
|
228 |
this.lineTo(w, eh); |
|
|
229 |
this.quadraticCurveTo(w, 0, w - ew, 0); |
|
|
230 |
this.lineTo(ew, 0); |
|
|
231 |
this.quadraticCurveTo(0, 0, 0, eh); |
|
|
232 |
this.end(); |
|
|
233 |
} |
|
|
234 |
}, { |
|
|
235 |
ATTRS: Y.mix({ |
|
|
236 |
ellipseWidth: { |
|
|
237 |
value: 4 |
|
|
238 |
}, |
|
|
239 |
|
|
|
240 |
ellipseHeight: { |
|
|
241 |
value: 4 |
|
|
242 |
} |
|
|
243 |
}, Y.Shape.ATTRS) |
|
|
244 |
}); |
|
|
245 |
Y.RoundedRect = RoundedRect; |
|
|
246 |
|
|
|
247 |
var mygraphic = new Y.Graphic({render: "#mygraphiccontainer"}), |
|
|
248 |
myroundrect = mygraphic.addShape({ |
|
|
249 |
type: Y.RoundedRect, |
|
|
250 |
width: 300, |
|
|
251 |
height: 200, |
|
|
252 |
x: 50, |
|
|
253 |
y: 50, |
|
|
254 |
ellipseWidth: 12, |
|
|
255 |
ellipseHeight: 12, |
|
|
256 |
fill: { |
|
|
257 |
type: "linear", |
|
|
258 |
stops: [ |
|
|
259 |
{color: "#9aa9bb", offset: 0}, |
|
|
260 |
{color: "#eeefff", offset: 0.4}, |
|
|
261 |
{color: "#00000f", offset: 0.8}, |
|
|
262 |
{color: "#9aa9bb", offset: 1} |
|
|
263 |
], |
|
|
264 |
rotation: 45 |
|
|
265 |
}, |
|
|
266 |
stroke: { |
|
|
267 |
weight: 2, |
|
|
268 |
color: "#000" |
|
|
269 |
} |
|
|
270 |
}); |
|
|
271 |
}); |
|
|
272 |
</script></pre> |
|
|
273 |
|
|
|
274 |
</div> |
|
|
275 |
</div> |
|
|
276 |
</div> |
|
|
277 |
|
|
|
278 |
<div class="yui3-u-1-4"> |
|
|
279 |
<div class="sidebar"> |
|
|
280 |
|
|
|
281 |
|
|
|
282 |
|
|
|
283 |
<div class="sidebox"> |
|
|
284 |
<div class="hd"> |
|
|
285 |
<h2 class="no-toc">Examples</h2> |
|
|
286 |
</div> |
|
|
287 |
|
|
|
288 |
<div class="bd"> |
|
|
289 |
<ul class="examples"> |
|
|
290 |
|
|
|
291 |
|
|
|
292 |
<li data-description="Shows how to create a Graphic instance and add shapes."> |
|
|
293 |
<a href="graphics-simple.html">Basic Graphics Implementation</a> |
|
|
294 |
</li> |
|
|
295 |
|
|
|
296 |
|
|
|
297 |
|
|
|
298 |
<li data-description="Shows how to draw lines and polygons."> |
|
|
299 |
<a href="graphics-path.html">Basic Path</a> |
|
|
300 |
</li> |
|
|
301 |
|
|
|
302 |
|
|
|
303 |
|
|
|
304 |
<li data-description="Shows how to create linear and radial gradient fills."> |
|
|
305 |
<a href="graphics-gradients.html">Create Gradient Fills</a> |
|
|
306 |
</li> |
|
|
307 |
|
|
|
308 |
|
|
|
309 |
|
|
|
310 |
<li data-description="Shows how to add drag to a shape."> |
|
|
311 |
<a href="graphics-drag.html">Basic drag with graphic object</a> |
|
|
312 |
</li> |
|
|
313 |
|
|
|
314 |
|
|
|
315 |
|
|
|
316 |
<li data-description="Shows how to apply transforms to shape."> |
|
|
317 |
<a href="graphics-transforms.html">Using Transforms</a> |
|
|
318 |
</li> |
|
|
319 |
|
|
|
320 |
|
|
|
321 |
|
|
|
322 |
<li data-description="Shows how to use a custom shape with the Graphics module."> |
|
|
323 |
<a href="graphics-customshape.html">Custom Shape</a> |
|
|
324 |
</li> |
|
|
325 |
|
|
|
326 |
|
|
|
327 |
|
|
|
328 |
<li data-description="Shows to use the graphics api to draw a realistic glass."> |
|
|
329 |
<a href="graphics-muddyglass.html">Transparent Glass with Shadow</a> |
|
|
330 |
</li> |
|
|
331 |
|
|
|
332 |
|
|
|
333 |
|
|
|
334 |
<li data-description="Shows to use the graphics api to draw a violin."> |
|
|
335 |
<a href="graphics-violin.html">Complex Drawing: Violin</a> |
|
|
336 |
</li> |
|
|
337 |
|
|
|
338 |
|
|
|
339 |
|
|
|
340 |
|
|
|
341 |
</ul> |
|
|
342 |
</div> |
|
|
343 |
</div> |
|
|
344 |
|
|
|
345 |
|
|
|
346 |
|
|
|
347 |
<div class="sidebox"> |
|
|
348 |
<div class="hd"> |
|
|
349 |
<h2 class="no-toc">Examples That Use This Component</h2> |
|
|
350 |
</div> |
|
|
351 |
|
|
|
352 |
<div class="bd"> |
|
|
353 |
<ul class="examples"> |
|
|
354 |
|
|
|
355 |
|
|
|
356 |
|
|
|
357 |
|
|
|
358 |
|
|
|
359 |
|
|
|
360 |
|
|
|
361 |
|
|
|
362 |
|
|
|
363 |
|
|
|
364 |
|
|
|
365 |
|
|
|
366 |
|
|
|
367 |
|
|
|
368 |
|
|
|
369 |
|
|
|
370 |
|
|
|
371 |
|
|
|
372 |
<li data-description="This example demonstrates animating an element along a curved path using bezier control points."> |
|
|
373 |
<a href="../anim/curve.html">Animating Along a Curved Path</a> |
|
|
374 |
</li> |
|
|
375 |
|
|
|
376 |
|
|
|
377 |
</ul> |
|
|
378 |
</div> |
|
|
379 |
</div> |
|
|
380 |
|
|
|
381 |
</div> |
|
|
382 |
</div> |
|
|
383 |
</div> |
|
|
384 |
</div> |
|
|
385 |
|
|
|
386 |
<script src="../assets/vendor/prettify/prettify-min.js"></script> |
|
|
387 |
<script>prettyPrint();</script> |
|
|
388 |
|
|
|
389 |
<script> |
|
|
390 |
YUI.Env.Tests = { |
|
|
391 |
examples: [], |
|
|
392 |
project: '../assets', |
|
|
393 |
assets: '../assets/graphics', |
|
|
394 |
name: 'graphics-customshape', |
|
|
395 |
title: 'Custom Shape', |
|
|
396 |
newWindow: '', |
|
|
397 |
auto: false |
|
|
398 |
}; |
|
|
399 |
YUI.Env.Tests.examples.push('graphics-simple'); |
|
|
400 |
YUI.Env.Tests.examples.push('graphics-path'); |
|
|
401 |
YUI.Env.Tests.examples.push('graphics-gradients'); |
|
|
402 |
YUI.Env.Tests.examples.push('graphics-drag'); |
|
|
403 |
YUI.Env.Tests.examples.push('graphics-transforms'); |
|
|
404 |
YUI.Env.Tests.examples.push('graphics-customshape'); |
|
|
405 |
YUI.Env.Tests.examples.push('graphics-muddyglass'); |
|
|
406 |
YUI.Env.Tests.examples.push('graphics-violin'); |
|
|
407 |
YUI.Env.Tests.examples.push('curve'); |
|
|
408 |
|
|
|
409 |
</script> |
|
|
410 |
<script src="../assets/yui/test-runner.js"></script> |
|
|
411 |
|
|
|
412 |
|
|
|
413 |
|
|
|
414 |
</body> |
|
|
415 |
</html> |