3 <script language=
"javascript">
6 SVG_NS
= 'http://www.w3.org/2000/svg';
8 function pathFromStar (radius
, innerRadius
, sides
) {
10 for (var i
= 0; i
< sides
* 2; ++i
) {
11 var theta
= Math
.PI
* i
/ sides
+ (Math
.PI
/ 2);
12 var r
= (i
% 2) ? radius
: innerRadius
;
13 var x
= r
* Math
.cos(theta
);
14 var y
= r
* Math
.sin(theta
);
15 path
.push((i
? 'L' : 'M'), x
, y
);
18 return path
.join(' ');
21 function newSvgElement (tagName
, parentNode
, attrs
) {
22 var node
= document
.createElementNS(SVG_NS
, tagName
);
24 for (var k
in attrs
) {
25 node
.setAttribute(k
, attrs
[k
]);
29 parentNode
.appendChild(node
);
34 function setCenterPosition (node
, x
, y
) {
35 node
.setAttribute('transform', 'translate($1, $2)'
42 window
.addEventListener('load', function () {
44 var svgContainer
= newSvgElement('svg', document
.body
);
46 { pos
: [ 100, 100 ], size
: 60, hasStroke
: false },
47 { pos
: [ 250, 100 ], size
: 60, hasStroke
: true },
48 { pos
: [ 400, 100 ], size
: 50, hasStroke
: true }
50 svgContainer
.style
.WebkitShadow
= '5px 5px 5px red';
51 for (var i
= 0; i
< shapes
.length
; ++i
) {
52 var shape
= shapes
[i
];
53 var group
= newSvgElement('g', svgContainer
);
54 var node
= newSvgElement('path', group
, {
55 d
: pathFromStar(shape
.size
, shape
.size
/ 2, 5),
60 setCenterPosition(node
, shape
.pos
[0], shape
.pos
[1]);
61 if (shapes
[i
].hasStroke
) {
62 node
.setAttribute('stroke-dasharray', 20);
64 node
.addEventListener('mousedown', function (ev
) {
66 armed
= { node
: e
.target
|| e
.srcElement
};
68 node
.addEventListener('mouseup', function () {
72 window
.addEventListener('mousemove', function (ev
) {
75 setCenterPosition(armed
.node
, e
.clientX
, e
.clientY
);