forked from sampumon/Pottisheads
-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo-full.xhtml
219 lines (179 loc) · 9.13 KB
/
demo-full.xhtml
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
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<link rel="stylesheet" type="text/css" href="stylesheets/master.css" />
<title>Final SVG vs. Canvas mega battle</title>
<script type="text/javascript" src="javascripts/jquery-1.3.2.min.js"></script>
<script type="text/javascript" src="javascripts/rgbcolor.js"></script>
<script type="text/javascript" src="javascripts/canvg.js"></script>
<script type="text/javascript" src="javascripts/pixel_toolbar.js"></script>
<script type="text/javascript" src="javascripts/pixel_canvas.js"></script>
<script type="text/javascript" src="javascripts/vector_svg.js"></script>
<script type="text/javascript" src="javascripts/canvas_to_svg.js"></script>
<script type="text/javascript" src="javascripts/svg_to_canvas.js"></script>
<script type="text/javascript" src="javascripts/mouse.js"></script>
<script type="text/javascript" src="javascripts/random.js"></script>
<script type="text/javascript" src="javascripts/base64.js"></script>
<script type="text/javascript" src="javascripts/svg_todataurl.js"></script>
<script type="text/javascript">
<![CDATA[
var svg = null;
var canvas = null;
window.onload = function() {
canvas = document.getElementById("canvas");
svg = document.getElementById("svg");
PixelCanvas.init(canvas);
// var canvastool = document.getElementById("canvasarea").getElementsByClassName("toolbar")[0];
// activatePixelToolbar(canvastool, PixelCanvas);
VectorSVG.init(svg);
for (var i = 0; i < 3; i++) {
VectorSVG.addRandomCircle();
PixelCanvas.drawRandomPixel();
}
}
function export_img(data_url) {
$("#export_img").attr("src", data_url);
}
function export_window(data_url) {
window.open(data_url);
}
function importImgToCanvas(img_src) {
var ctx = canvas.getContext('2d');
var img = new Image();
img.src = img_src;
img.onload = function() {
console.log("Exported image size " + img.width + "x" + img.height);
ctx.drawImage(img, 0, 0);
}
}
function break_now(klass) {
// TODO: efekti
$("." + klass).addClass("broken");
}
function will_break(klass) {
// TODO: efekti
$("." + klass).addClass("will_break_canvas");
$("." + klass + " a").each(function(a) {
this.href += " break_now('will_read_canvas');";
})
}
]]>
</script>
</head>
<body>
<h1>From SVG to Canvas and Back (full demo)</h1>
<dl>
<dt>Legend (based on background-color):</dt>
<dd class="broken">This won't work anymore, because Canvas' <code>origin-clean</code> is <var>dirty</var>. Maybe you broke it?</dd>
<dd class="will_break_canvas">Beware! This will set Canvas' <code>origin-clean</code> <var>dirty</var>, making Canvas write-only.</dd>
<dd class="might_break_canvas_legend">This <em>might</em> set Canvas' <code>origin-clean</code> <var>dirty</var>, depending on whether you're transferring images inside SVG data.</dd>
</dl>
<div id="svgarea">
<h2>SVG</h2>
<div class="toolbar">
<a href="javascript:alert('Click that SVG area with your mouse!')">circle</a>
<a href="javascript:VectorSVG.addImage('gordo-small.png');">png image</a>
<a href="javascript:VectorSVG.addImage('gordo.svg'); will_break('might_break_canvas');">svg image</a>
<a class="will_read_canvas" href="javascript:CanvasToSVG.convert(canvas, svg);">import canvas</a>
<a href="javascript:VectorSVG.clearCanvas();">clear</a>
</div>
<svg id="svg" width="500" height="300" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" />
</div>
<div id="canvasarea">
<h2>Canvas</h2>
<div class="toolbar">
<a href="javascript:alert('Click and drag the Canvas area with your mouse!')">pixel</a>
<a href="javascript:PixelCanvas.drawImage('gordo-small.png');">png image</a>
<a href="javascript:PixelCanvas.drawImage('gordo.svg');">svg image</a>
<a class="might_break_canvas" href="javascript:SVGToCanvas.convertCanvg(svg, canvas);">import svg</a>
<a href="javascript:PixelCanvas.clearCanvas();">clear</a>
</div>
<canvas id="canvas" width="500" height="300">NO CANVAS FOR YOU</canvas>
</div>
<div id="demolist">
<ol>
<li><a href="javascript:alert('Click and drag the Canvas area with your mouse!')">Draw pixel graphics with Canvas</a>
<ol>
<li><a href="javascript:PixelCanvas.drawImage('gordo-small.png');">Draw PNG Image</a></li>
<li><a href="javascript:PixelCanvas.drawImage('gordo.svg');">Draw SVG Image</a></li>
</ol>
</li>
<li><a href="javascript:alert('Click that SVG area with your mouse!')">Draw vector graphics with SVG</a>
<ol>
<li><a href="javascript:VectorSVG.addImage('gordo-small.png');">Draw PNG Image</a></li>
<li><a href="javascript:VectorSVG.addImage('gordo.svg'); will_break('might_break_canvas');">Draw SVG Image</a></li>
</ol>
</li>
<li>Interfaces between Canvas and SVG:
<ol>
<li>One-way only transfer from Canvas: canvas→svg , canvas→svg, …
<ol>
<li class="will_read_canvas"><a href="javascript:CanvasToSVG.convert(canvas, svg);">Canvas toDataURL</a>. Can be used multiple times when no SVG data is written onto Canvas.</li>
</ol>
</li>
<li>One-way only transfer from SVG: svg→canvas, svg→canvas, …
<ol>
<li class="will_break_canvas"><a href="javascript:SVGToCanvas.convert(svg, canvas); break_now('will_read_canvas');">Our client-side implementation</a>. Canvas becomes write-only.</li>
</ol>
</li>
<li>Two-way transfer: svg→canvas→svg→canvassvg, ..
<ol>
<li class="will_read_canvas">Canvas→SVG: <a href="javascript:CanvasToSVG.convert(canvas, svg);">Canvas toDataURL</a>. (as in 3.a.i)</li>
<li>SVG→Canvas complete (non-safe) transfer, including possibly non-secure elements:
<ol>
<li><a href="javascript:alert('Please use your operating system\'s screen capture function, capture the SVG area, save as svg_export.png, open developer console and call: importImgToCanvas(\'/path/to/svg_export.png\')')">Manual method</a>
<ul>
<li>Works with all major browsers, including IE9.</li>
<li>This is the only way to capture <em>current</em> rendering state of SVG!</li>
</ul>
</li>
<li><a href="javascript:SVGToCanvas.convertServer(svg, canvas);">SVG rastrerization server</a> (with Imagemagick or similar).</li>
<li><a href="javascript:alert('Dear browser manufacturers, please implement it!');">Our proposed SVG.toDataURL recommendation</a>. No current client-side solution works two-way. We’re waiting for browser manufacturers.</li>
</ol>
</li>
<li>SVG→Canvas security-safe (non-image and non-foreign) elements:
<ol>
<li><a href="javascript:alert('Dear browser manufacturers, please implement it!');">Our proposed SVG.toDataURL recommendation</a>. No current client-side solution works two-way. We’re waiting for browser manufacturers.</li>
<li class="might_break_canvas"><a href="javascript:SVGToCanvas.convertCanvg(svg, canvas);">Canvg</a> (or other SVG rasterization library).
<ul>
<li>Will make Canvas write-only only if the imported SVG has images from different origin, or embedded svg images.</li>
<li>Canvg paints transparent areas from SVG as white, overwriting previous drawings on Canvas.</li>
</ul>
</li>
</ol>
</li>
</ol>
</li>
</ol>
</li>
<li>Exporting as file
<ol>
<li>Canvas→PNG
<ol>
<li>Right-click→Save as… (Firefox supports it).</li>
<li class="will_read_canvas"><a href="javascript:export_img(canvas.toDataURL());">Canvas.toDataURL</a> (<a href="javascript:export_window(canvas.toDataURL());">new window</a>).</li>
</ol>
</li>
<li>SVG→PNG
<ol>
<li><a href="javascript:alert('Please use your operating system\'s screen capture function to capture the SVG area ^____^')">Manual method</a>.</li>
<li><a href="javascript:SVGToCanvas.exportPNG(svg, export_img);">Our implementation (safe elements)</a> won't work, since SVG→Canvas→toDataURL always gives SECURITY_ERR.</li>
<li><a href="javascript:SVGToCanvas.exportPNGserver(svg, export_img);">Serverside (safe and non-safe elements)</a>.</li>
<li><a href="javascript:SVGToCanvas.exportPNGcanvg(svg, export_img);">canvg (safe elements)</a>.</li>
</ol>
</li>
<li>SVG→SVG file
<ol>
<li>Right-click→Save as… (IE9 browser supports it).</li>
<li><a href="javascript:SVGToCanvas.exportSVG(svg, export_img);">Serialize SVG as data URL</a> (<a href="javascript:SVGToCanvas.exportSVG(svg, export_window);">new window</a>).</li>
</ol>
</li>
</ol>
</li>
</ol>
</div>
<img id="export_img" alt="placeholder for exported svg/pixel data" />
</body>
</html>