-
Notifications
You must be signed in to change notification settings - Fork 2
/
snaptut-loadfile.html
84 lines (62 loc) · 2.29 KB
/
snaptut-loadfile.html
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
<!DOCTYPE html>
<html>
<head>
<title>Snap.svg Tutorial</title>
<script src="analytics.js"></script>
</head>
<body>
<link href="/snapstyle.css" rel="stylesheet">
<div id="container"></div>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>
<script src="snap.svg.js"></script>
<a href="http://snapsvg.io/docs/">Snap Docs</a> <a href="/">More SVG Dabbles</a>
<article>
<h1 class="heading">Loading SVG</h1>
<div class="intro">Lets get an SVG shape loaded and displayed. We need a callback for when the file is loaded, then we can add it to our canvas.<br/>
Note, it gets loaded into Snap as a fragment.</div>
<div id="fileContainer"></div>
<pre class="codestuff"><code contenteditable="true">
<xmp>
function handleFileOpen(evt) {
var inputObj = document.createElement ("input");
inputObj.type = "file";
inputObj.name = "document";
document.getElementById('fileContainer2').appendChild(inputObj);
inputObj.click();
inputObj.addEventListener('change', handleFileSelect, false);
}
function handleFileSelect(evt) {
var files = evt.target.files; // FileList object
for (var i = 0, f; f = files[i]; i++) {
var reader = new FileReader();
reader.onload = function( evt ) {
var contents = evt.target.result;
s.append( Snap.parse( contents ) );
};
reader.onerror = function(event) {
console.log(' couldnt not read file. Code ' + event.target.error.code);
};
reader.readAsText(f);
};
}
var s = Snap("#svgout");
var button = s.rect(10,10,40,40,5,5).attr({ fill: "red", stroke: "black" });
s.text(60,30,"click Me");
button.click( handleFileOpen );
// just use a normal div below, not a foreignobject, just using this to test doing something
var fo = '<svg><foreignObject width="80" height="0"><div id="fileContainer2"></div></foreignObject></svg>';
s.group().append( Snap.parse( fo ) );
</xmp>
</code>
<button class="run">Edit/Run</button>
</pre>
</article>
<div class="intro">SVGout area...</div>
<div class="output">
<svg id="svgout" width="400" height="400"></svg>
</div>
<div class="intro">The actual svg markup looks like this (when you've clicked on run)....</div>
<div id="htmlraw"></div>
<script src="snaptut.js"></script></script>
<script src="hijs.js"></script>
</body>