-
Notifications
You must be signed in to change notification settings - Fork 0
/
inkml2svg.js
232 lines (186 loc) · 6.87 KB
/
inkml2svg.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
// http://www.schepers.cc/svg/inkml/InkML2SVG.svg
// by Schepers
// Saturday, November 17, 2007
// <script><![CDATA[
var SVGDocument = null;
var SVGRoot = null;
var svgns = 'http://www.w3.org/2000/svg';
var xlinkns = 'http://www.w3.org/1999/xlink';
var inkns = 'http://www.w3.org/2003/InkML';
function Init(evt)
{
SVGDocument = evt.target.ownerDocument;
SVGRoot = SVGDocument.documentElement;
ConvertInk();
};
function ConvertInk()
{
var pathGroup = SVGDocument.createElementNS(svgns, 'g');
var traceEls = SVGDocument.getElementsByTagNameNS( inkns,'trace' );
for ( var i = 0, iLen = traceEls.length; iLen > i; i++ )
{
var eachTraceEl = traceEls.item(i);
var eachTraceData = eachTraceEl.firstChild.nodeValue;
//alert(eachTraceData)
eachTraceData = eachTraceData.replace(/^\s*|\s*$/g, "");
//eachTraceData = eachTraceData.replace(" ", " ");
CreateTrace( i, eachTraceData, pathGroup );
}
SVGRoot.appendChild( pathGroup );
var outline = pathGroup.getBBox();
var vb = (outline.x - 20) + ' ' + (outline.y - 20) + ' ' + (outline.width + 40) + ' ' + (outline.height + 40);
SVGRoot.setAttribute( 'viewBox', vb );
var background = SVGDocument.createElementNS(svgns, 'rect');
background.setAttribute( 'id', 'background' );
background.setAttribute( 'x', outline.x - 20 );
background.setAttribute( 'y', outline.y - 20 );
background.setAttribute( 'width', outline.width + 40 );
background.setAttribute( 'height', outline.height + 40 );
background.setAttribute( 'stroke', 'green' );
background.setAttribute( 'fill', 'white' );
SVGRoot.insertBefore( background, pathGroup );
background.addEventListener( 'click', ResetAnimation, false);
var fontSize = 10;
var newLabel = SVGDocument.createElementNS(svgns, 'text');
newLabel.setAttribute( 'id', 'instructions');
newLabel.setAttribute( 'x', (outline.x + (outline.width/2)) );
newLabel.setAttribute( 'y', (outline.y + fontSize - 18) );
newLabel.setAttribute( 'font-size', fontSize );
newLabel.setAttribute( 'font-family', 'Verdana' );
newLabel.setAttribute( 'text-anchor', 'middle');
newLabel.setAttribute( 'pointer-events', 'none');
newLabel.setAttribute( 'fill', 'gray');
var labelText = SVGDocument.createTextNode('Click on background to animate ink. Mouse over shapes to show capture points.');
newLabel.appendChild(labelText);
SVGRoot.insertBefore( newLabel, pathGroup );
setTimeout( 'AnimateShapes()', 10 );
};
function CreateTrace( i, data, group )
{
var shape = CreatePath( data );
var samples = CreateSamples( data );
shape.setAttribute( 'id', 't' + i );
shape.addEventListener( 'mouseover', ShowSamples, false);
shape.addEventListener( 'mouseout', HideSamples, false);
samples.setAttribute( 'id', 't' + i + '-s' );
samples.setAttribute( 'display', 'none' );
samples.setAttribute( 'pointer-events', 'none' );
var traceGroup = SVGDocument.createElementNS(svgns, 'g');
traceGroup.appendChild( shape );
traceGroup.appendChild( samples );
group.appendChild( traceGroup );
};
function CreatePath( data )
{
var shape = SVGDocument.createElementNS(svgns, 'path');
shape.setAttribute( 'd', 'M' + data );
shape.setAttribute( 'fill', 'none' );
shape.setAttribute( 'stroke', 'blue' );
return shape;
};
function CreateSamples( data )
{
var sampleGroup = SVGDocument.createElementNS(svgns, 'g');
var points = data.split(',');
//alert(points)
for ( var i = 0, iLen = points.length; iLen > i; i++ )
{
var eachPoint = points[ i ];
eachPoint = eachPoint.replace(/^\s*|\s*$/g, "");
var pointSplit = eachPoint.split(' ');
//alert(eachPoint + '\n x: ' + pointSplit[ 0 ] + '\n y: ' + pointSplit[ 1 ] );
var dot = SVGDocument.createElementNS(svgns, 'circle');
dot.setAttribute( 'r', 2.5 );
dot.setAttribute( 'cx', pointSplit[ 0 ] );
dot.setAttribute( 'cy', pointSplit[ 1 ] );
dot.setAttribute( 'stroke', 'black' );
var fill = 'yellow';
if ( 0 == i )
{
fill = 'lime';
}
else if ( iLen - 1 == i )
{
fill = 'red';
}
dot.setAttribute( 'fill', fill );
sampleGroup.appendChild( dot );
}
return sampleGroup;
};
function ShowSamples( evt )
{
var targetEl = evt.target;
var targetId = targetEl.getAttribute( 'id' );
var samples = SVGDocument.getElementById( targetId + '-s' );
samples.setAttribute( 'display', 'inline' );
};
function HideSamples( evt )
{
var targetEl = evt.target;
var targetId = targetEl.getAttribute( 'id' );
var samples = SVGDocument.getElementById( targetId + '-s' );
samples.setAttribute( 'display', 'none' );
};
function AnimateShapes()
{
var startAnim = null;
var pathEls = SVGDocument.getElementsByTagNameNS( svgns, 'path' );
for ( var i = 0, iLen = pathEls.length; iLen > i; i++ )
{
var eachPathEl = pathEls.item(i);
var pathLength = eachPathEl.getTotalLength();
eachPathEl.setAttribute( 'stroke-dasharray', pathLength + ' ' + pathLength);
eachPathEl.setAttribute( 'stroke-dashoffset', 0 );
var dur = 2;
var begin = 'a_' + (i - 1) + '.end';
if ( 0 == i )
{
begin = 'background.click + 0.5s';
path1 = null;
}
var animId = 'a_' + i;
var hide = CreateHide( pathLength );
//eachPathEl.appendChild( hide );
var animation = CreateAnimation( animId, begin, dur, pathLength );
eachPathEl.appendChild( animation );
}
};
function CreateHide( length )
{
var set = SVGDocument.createElementNS(svgns, 'set');
set.setAttribute( 'attributeName', 'stroke-dashoffset');
set.setAttribute( 'attributeType', 'XML');
set.setAttribute( 'begin', 'background.click' );
set.setAttribute( 'to', length );
set.setAttribute( 'dur', '1s');
set.setAttribute( 'fill', 'freeze');
set.setAttribute( 'restart', 'whenNotActive');
return set;
};
function CreateAnimation(animId, begin, dur, length)
{
var animation = SVGDocument.createElementNS(svgns, 'animate');
animation.setAttribute( 'id', animId );
animation.setAttribute( 'attributeName', 'stroke-dashoffset');
animation.setAttribute( 'attributeType', 'XML');
animation.setAttribute( 'begin', begin );
animation.setAttribute( 'from', length);
animation.setAttribute( 'to', '0');
animation.setAttribute( 'dur', dur + 's');
animation.setAttribute( 'fill', 'freeze');
animation.setAttribute( 'restart', 'whenNotActive');
return animation;
};
function ResetAnimation()
{
var pathEls = SVGDocument.getElementsByTagNameNS( svgns, 'path' );
for ( var i = 0, iLen = pathEls.length; iLen > i; i++ )
{
var eachPathEl = pathEls.item(i);
var pathLength = eachPathEl.getTotalLength();
eachPathEl.setAttribute( 'stroke-dasharray', pathLength + ' ' + pathLength);
eachPathEl.setAttribute( 'stroke-dashoffset', pathLength);
}
};
// ]]></script>