Subversion-Projekte lars-tiefland.cienc

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
9 lars 1
/* FileSaver.js demo script
2
 * 2012-01-23
3
 *
4
 * By Eli Grey, http://eligrey.com
5
 * License: X11/MIT
6
 *   See LICENSE.md
7
 */
8
 
9
/*! @source http://purl.eligrey.com/github/FileSaver.js/blob/master/demo/demo.js */
10
 
11
(function(view) {
12
"use strict";
13
// The canvas drawing portion of the demo is based off the demo at
14
// http://www.williammalone.com/articles/create-html5-canvas-javascript-drawing-app/
15
var
16
	  document = view.document
17
	, $ = function(id) {
18
		return document.getElementById(id);
19
	}
20
	, session = view.sessionStorage
21
	// only get URL when necessary in case Blob.js hasn't defined it yet
22
	, get_blob = function() {
23
		return view.Blob;
24
	}
25
 
26
	, canvas = $("canvas")
27
	, canvas_options_form = $("canvas-options")
28
	, canvas_filename = $("canvas-filename")
29
	, canvas_clear_button = $("canvas-clear")
30
 
31
	, text = $("text")
32
	, text_options_form = $("text-options")
33
	, text_filename = $("text-filename")
34
 
35
	, html = $("html")
36
	, html_options_form = $("html-options")
37
	, html_filename = $("html-filename")
38
 
39
	, ctx = canvas.getContext("2d")
40
	, drawing = false
41
	, x_points = session.x_points || []
42
	, y_points = session.y_points || []
43
	, drag_points = session.drag_points || []
44
	, add_point = function(x, y, dragging) {
45
		x_points.push(x);
46
		y_points.push(y);
47
		drag_points.push(dragging);
48
	}
49
	, draw = function(){
50
		canvas.width = canvas.width;
51
		ctx.lineWidth = 6;
52
		ctx.lineJoin = "round";
53
		ctx.strokeStyle = "#000000";
54
		var
55
			  i = 0
56
			, len = x_points.length
57
		;
58
		for(; i < len; i++) {
59
			ctx.beginPath();
60
			if (i && drag_points[i]) {
61
				ctx.moveTo(x_points[i-1], y_points[i-1]);
62
			} else {
63
				ctx.moveTo(x_points[i]-1, y_points[i]);
64
			}
65
			ctx.lineTo(x_points[i], y_points[i]);
66
			ctx.closePath();
67
			ctx.stroke();
68
		}
69
	}
70
	, stop_drawing = function() {
71
		drawing = false;
72
	}
73
 
74
	// Title guesser and document creator available at https://gist.github.com/1059648
75
	, guess_title = function(doc) {
76
		var
77
			  h = "h6 h5 h4 h3 h2 h1".split(" ")
78
			, i = h.length
79
			, headers
80
			, header_text
81
		;
82
		while (i--) {
83
			headers = doc.getElementsByTagName(h[i]);
84
			for (var j = 0, len = headers.length; j < len; j++) {
85
				header_text = headers[j].textContent.trim();
86
				if (header_text) {
87
					return header_text;
88
				}
89
			}
90
		}
91
	}
92
	, doc_impl = document.implementation
93
	, create_html_doc = function(html) {
94
		var
95
			  dt = doc_impl.createDocumentType('html', null, null)
96
			, doc = doc_impl.createDocument("http://www.w3.org/1999/xhtml", "html", dt)
97
			, doc_el = doc.documentElement
98
			, head = doc_el.appendChild(doc.createElement("head"))
99
			, charset_meta = head.appendChild(doc.createElement("meta"))
100
			, title = head.appendChild(doc.createElement("title"))
101
			, body = doc_el.appendChild(doc.createElement("body"))
102
			, i = 0
103
			, len = html.childNodes.length
104
		;
105
		charset_meta.setAttribute("charset", html.ownerDocument.characterSet);
106
		for (; i < len; i++) {
107
			body.appendChild(doc.importNode(html.childNodes.item(i), true));
108
		}
109
		var title_text = guess_title(doc);
110
		if (title_text) {
111
			title.appendChild(doc.createTextNode(title_text));
112
		}
113
		return doc;
114
	}
115
;
116
canvas.width = 500;
117
canvas.height = 300;
118
 
119
  if (typeof x_points === "string") {
120
	x_points = JSON.parse(x_points);
121
} if (typeof y_points === "string") {
122
	y_points = JSON.parse(y_points);
123
} if (typeof drag_points === "string") {
124
	drag_points = JSON.parse(drag_points);
125
} if (session.canvas_filename) {
126
	canvas_filename.value = session.canvas_filename;
127
} if (session.text) {
128
	text.value = session.text;
129
} if (session.text_filename) {
130
	text_filename.value = session.text_filename;
131
} if (session.html) {
132
	html.innerHTML = session.html;
133
} if (session.html_filename) {
134
	html_filename.value = session.html_filename;
135
}
136
 
137
drawing = true;
138
draw();
139
drawing = false;
140
 
141
canvas_clear_button.addEventListener("click", function() {
142
	canvas.width = canvas.width;
143
	x_points.length =
144
	y_points.length =
145
	drag_points.length =
146
		0;
147
}, false);
148
canvas.addEventListener("mousedown", function(event) {
149
	drawing = true;
150
	add_point(event.pageX - canvas.offsetLeft, event.pageY - canvas.offsetTop, false);
151
	draw();
152
}, false);
153
canvas.addEventListener("mousemove", function(event) {
154
	if (drawing) {
155
		add_point(event.pageX - canvas.offsetLeft, event.pageY - canvas.offsetTop, true);
156
		draw();
157
	}
158
}, false);
159
canvas.addEventListener("mouseup", stop_drawing, false);
160
canvas.addEventListener("mouseout", stop_drawing, false);
161
 
162
canvas_options_form.addEventListener("submit", function(event) {
163
	event.preventDefault();
164
	canvas.toBlob(function(blob) {
165
		saveAs(
166
			  blob
167
			, (canvas_filename.value || canvas_filename.placeholder) + ".png"
168
		);
169
	}, "image/png");
170
}, false);
171
 
172
text_options_form.addEventListener("submit", function(event) {
173
	event.preventDefault();
174
	var BB = get_blob();
175
	saveAs(
176
		  new BB(
177
			  [text.value || text.placeholder]
178
			, {type: "text/plain;charset=" + document.characterSet}
179
		)
180
		, (text_filename.value || text_filename.placeholder) + ".txt"
181
	);
182
}, false);
183
 
184
html_options_form.addEventListener("submit", function(event) {
185
	event.preventDefault();
186
	var
187
		  BB = get_blob()
188
		, xml_serializer = new XMLSerializer
189
		, doc = create_html_doc(html)
190
	;
191
	saveAs(
192
		  new BB(
193
			  [xml_serializer.serializeToString(doc)]
194
			, {type: "application/xhtml+xml;charset=" + document.characterSet}
195
		)
196
		, (html_filename.value || html_filename.placeholder) + ".xhtml"
197
	);
198
}, false);
199
 
200
view.addEventListener("unload", function() {
201
	session.x_points = JSON.stringify(x_points);
202
	session.y_points = JSON.stringify(y_points);
203
	session.drag_points = JSON.stringify(drag_points);
204
	session.canvas_filename = canvas_filename.value;
205
 
206
	session.text = text.value;
207
	session.text_filename = text_filename.value;
208
 
209
	session.html = html.innerHTML;
210
	session.html_filename = html_filename.value;
211
}, false);
212
}(self));