1 |
wakaba |
1.1 |
<!DOCTYPE HTML> |
2 |
|
|
<html lang=en> |
3 |
|
|
<title>Canvas</title> |
4 |
|
|
<meta name="viewport" content="width=device-width"> |
5 |
|
|
<style> |
6 |
|
|
body { |
7 |
|
|
margin: 0; |
8 |
|
|
padding: 0; |
9 |
|
|
background: #C0C0C0; |
10 |
|
|
} |
11 |
|
|
canvas { |
12 |
|
|
width: 240px; |
13 |
|
|
background: white; |
14 |
|
|
} |
15 |
|
|
|
16 |
|
|
a { |
17 |
|
|
display: inline-block; |
18 |
|
|
min-width: 1em; |
19 |
|
|
text-align: center; |
20 |
|
|
} |
21 |
|
|
menu { |
22 |
|
|
position: fixed; |
23 |
|
|
bottom: 0; |
24 |
|
|
width: 100%; |
25 |
|
|
margin: 0; |
26 |
|
|
padding: 0; |
27 |
|
|
font-size: 8px; |
28 |
|
|
} |
29 |
|
|
|
30 |
|
|
[hidden] { |
31 |
|
|
display: none; |
32 |
|
|
} |
33 |
|
|
#panel { |
34 |
|
|
position: absolute; |
35 |
|
|
left: 20px; |
36 |
|
|
width: 200px; /* 240px - 20px * 2 */ |
37 |
|
|
top: 20px; |
38 |
|
|
max-height: 120px; /* 160px - 20px * 2 */ |
39 |
|
|
overflow: auto; |
40 |
|
|
background-color: #C0C0C0; |
41 |
|
|
font-size: smaller; |
42 |
|
|
} |
43 |
|
|
#panel p { |
44 |
|
|
margin: 0; |
45 |
|
|
padding: 0; |
46 |
|
|
} |
47 |
|
|
</style> |
48 |
|
|
|
49 |
|
|
<body> |
50 |
|
|
|
51 |
|
|
<canvas width=240 height=160></canvas> |
52 |
|
|
|
53 |
|
|
<section id=panel hidden> |
54 |
|
|
|
55 |
|
|
<p> |
56 |
|
|
[<a href="javascript:setProp ('lineWidth', 1); togglePanel ()">1</a> |
57 |
|
|
<a href="javascript:setProp ('lineWidth', 2); togglePanel ()">2</a> |
58 |
|
|
<a href="javascript:setProp ('lineWidth', 3); togglePanel ()">3</a> |
59 |
|
|
<a href="javascript:setProp ('lineWidth', 5); togglePanel ()">5</a> |
60 |
|
|
<a href="javascript:setProp ('lineWidth', 10); togglePanel ()">10</a>] |
61 |
|
|
</p> |
62 |
|
|
|
63 |
|
|
<p> |
64 |
|
|
[<a href="javascript:setProp ('strokeStyle', 'black'); togglePanel ()" style="color: black">black</a> |
65 |
|
|
<a href="javascript:setProp ('strokeStyle', 'red'); togglePanel ()" style="color: red">red</a> |
66 |
|
|
<a href="javascript:setProp ('strokeStyle', 'green'); togglePanel ()" style="color: green">green</a> |
67 |
|
|
<a href="javascript:setProp ('strokeStyle', 'blue'); togglePanel ()" style="color: blue">blue</a> |
68 |
|
|
<a href="javascript:setProp ('strokeStyle', 'orange'); togglePanel ()" style="color: orange">orange</a> |
69 |
|
|
<a href="javascript:setProp ('strokeStyle', 'yellow'); togglePanel ()" style="color: yellow">yellow</a> |
70 |
|
|
<a href="javascript:setProp ('strokeStyle', 'pink'); togglePanel ()" style="color: pink">pink</a> |
71 |
|
|
<a href="javascript:setProp ('strokeStyle', 'purple'); togglePanel ()" style="color: purple">purple</a> |
72 |
|
|
<a href="javascript:setProp ('strokeStyle', 'gray'); togglePanel ()" style="color: gray">gray</a> |
73 |
|
|
<a href="javascript:setProp ('strokeStyle', 'white'); togglePanel ()" style="color: white">white</a>] |
74 |
|
|
</p> |
75 |
|
|
|
76 |
|
|
<hr> |
77 |
|
|
|
78 |
|
|
<a id=show-image-page>Show image page</a> |
79 |
|
|
|
80 |
|
|
</section> |
81 |
|
|
|
82 |
|
|
<menu><a href="javascript:togglePanel ()">Panel</a> |
83 |
|
|
<a href="javascript:saveImage ()">Save</a> |
84 |
|
|
<span id=status></span></menu> |
85 |
|
|
|
86 |
|
|
<script> |
87 |
|
|
function $ (id) { return document.getElementById (id) } |
88 |
|
|
|
89 |
|
|
function setStatus (s) { |
90 |
|
|
var status = $ ('status'); |
91 |
|
|
status.innerText = s; |
92 |
|
|
status.textContent = s; |
93 |
|
|
} // setStatus |
94 |
|
|
|
95 |
|
|
var canvas = document.getElementsByTagName('canvas')[0]; |
96 |
|
|
var ctx = canvas.getContext('2d'); |
97 |
|
|
ctx.lineCap = 'round'; |
98 |
|
|
ctx.lineWidth = 3; |
99 |
|
|
ctx.beginPath (); |
100 |
|
|
|
101 |
|
|
var q = (location.search || '').replace (/^\?/, '').split (/[&;]/); |
102 |
|
|
var param = {}; |
103 |
|
|
for (var i = 0; i < q.length; i++) { |
104 |
|
|
var p = q[i].split (/=/); |
105 |
|
|
param[decodeURIComponent (p[0])] = decodeURIComponent (p[1]); |
106 |
|
|
} |
107 |
|
|
|
108 |
|
|
var drawing = false; |
109 |
|
|
canvas.onmousedown = function (ev) { |
110 |
|
|
drawing = true; |
111 |
|
|
ctx.beginPath (); |
112 |
|
|
var x = ev.clientX; |
113 |
|
|
var y = ev.clientY; |
114 |
|
|
ctx.moveTo (x, y); |
115 |
|
|
write ('moveTo', x, y); |
116 |
|
|
}; |
117 |
|
|
canvas.onmousemove = function (ev) { |
118 |
|
|
if (drawing) { |
119 |
|
|
var x = ev.clientX; |
120 |
|
|
var y = ev.clientY; |
121 |
|
|
ctx.lineTo (x, y); |
122 |
|
|
write ('lineTo', x, y); |
123 |
|
|
ctx.stroke (); |
124 |
|
|
// ctx.closePath (); |
125 |
|
|
// ctx.beginPath (); |
126 |
|
|
// ctx.moveTo (x, y); |
127 |
|
|
} |
128 |
|
|
}; |
129 |
|
|
window.onmouseup = function (ev) { |
130 |
|
|
if (!drawing) return; |
131 |
|
|
ctx.stroke (); |
132 |
|
|
ctx.closePath (); |
133 |
|
|
drawing = false; |
134 |
|
|
}; |
135 |
|
|
|
136 |
|
|
var newEvents = []; |
137 |
|
|
|
138 |
|
|
if (param['input-url']) { |
139 |
|
|
newEvents = readRemoteImage (param['input-url']); |
140 |
|
|
} else { |
141 |
|
|
write ('lineCap', 'round'); |
142 |
|
|
write ('lineWidth', 3); |
143 |
|
|
} |
144 |
|
|
|
145 |
|
|
function write () { |
146 |
|
|
setStatus ('Modified'); |
147 |
|
|
newEvents.push (Array.prototype.join.call (arguments, ',')); |
148 |
|
|
} |
149 |
|
|
|
150 |
|
|
function setProp (n, v) { |
151 |
|
|
ctx[n] = v; |
152 |
|
|
write (n, v); |
153 |
|
|
} |
154 |
|
|
|
155 |
|
|
function saveImage () { |
156 |
|
|
setStatus ('Saving...'); |
157 |
|
|
var xhr = new XMLHttpRequest (); |
158 |
|
|
xhr.open ('POST', param['post-url'], true); |
159 |
|
|
xhr.setRequestHeader ('Content-Type', 'application/x-www-form-urlencoded'); |
160 |
|
|
var e = function (n, v) { |
161 |
|
|
return encodeURIComponent (n) + '=' + encodeURIComponent (v); |
162 |
|
|
}; |
163 |
|
|
xhr.onreadystatechange = function () { |
164 |
|
|
if (xhr.readyState == 4) { |
165 |
|
|
if (xhr.status >= 200 && xhr.status < 400) { |
166 |
|
|
setStatus ('Saved'); |
167 |
|
|
} else { |
168 |
|
|
setStatus ('Failed (' + xhr.status + ')'); |
169 |
|
|
} |
170 |
|
|
var location = xhr.getResponseHeader ('Location'); |
171 |
|
|
if (location) { |
172 |
|
|
$('show-image-page').href = location; |
173 |
|
|
} |
174 |
|
|
var postURL = xhr.getResponseHeader ('X-SW-Post-URL'); |
175 |
|
|
if (postURL) { |
176 |
|
|
param['post-url'] = postURL; |
177 |
|
|
} |
178 |
|
|
param.hash = xhr.getResponseHeader ('X-SW-Hash'); |
179 |
|
|
xhr.onreadystate = null; |
180 |
|
|
} |
181 |
|
|
}; |
182 |
|
|
xhr.send ([ |
183 |
|
|
e('text', newEvents.join ("\n")), |
184 |
|
|
e('content-type', 'image/x-canvas-instructions+text'), |
185 |
|
|
e('hash', param.hash), |
186 |
|
|
e('no-redirect', 1) |
187 |
|
|
].join ('&')); |
188 |
|
|
} |
189 |
|
|
|
190 |
|
|
function readRemoteImage (u) { |
191 |
|
|
var xhr = new XMLHttpRequest (); |
192 |
|
|
xhr.open ('GET', u, false); |
193 |
|
|
xhr.setRequestHeader ('Cache-Control', 'no-cache'); |
194 |
|
|
xhr.send (null); |
195 |
|
|
if (xhr.responseText) { |
196 |
|
|
var events = xhr.responseText.split (/\x0A/); |
197 |
|
|
if (events.length) { |
198 |
|
|
canvas.width = canvas.width; // clear |
199 |
|
|
processEvents(events); |
200 |
|
|
} |
201 |
|
|
var hash = xhr.getResponseHeader ('X-SW-Hash'); |
202 |
|
|
if (hash) { |
203 |
|
|
param.hash = hash; |
204 |
|
|
} |
205 |
|
|
return events; |
206 |
|
|
} |
207 |
|
|
return []; |
208 |
|
|
} |
209 |
|
|
|
210 |
|
|
function processEvents (events) { |
211 |
|
|
for (var i = 0; i < events.length; i++) { |
212 |
|
|
var ev = events[i].split (/,/); |
213 |
|
|
if (ev[0] == 'lineTo') { |
214 |
|
|
var x = parseFloat (ev[1]); |
215 |
|
|
var y = parseFloat (ev[2]); |
216 |
|
|
ctx.lineTo (x, y); |
217 |
|
|
ctx.stroke (); |
218 |
|
|
// ctx.closePath (); |
219 |
|
|
// ctx.beginPath (); |
220 |
|
|
// ctx.moveTo (x, y); |
221 |
|
|
} else if (ev[0] == 'moveTo') { |
222 |
|
|
var x = parseFloat (ev[1]); |
223 |
|
|
var y = parseFloat (ev[2]); |
224 |
|
|
ctx.moveTo (x, y); |
225 |
|
|
} else if (ev[0] == 'strokeStyle' || ev[0] == 'lineWidth') { |
226 |
|
|
ctx.closePath (); |
227 |
|
|
ctx.beginPath (); |
228 |
|
|
ctx[ev[0]] = ev[1]; |
229 |
|
|
} |
230 |
|
|
} |
231 |
|
|
} |
232 |
|
|
|
233 |
|
|
function togglePanel () { |
234 |
|
|
if ($('panel').hasAttribute ('hidden')) { |
235 |
|
|
$('panel').removeAttribute ('hidden'); |
236 |
|
|
} else { |
237 |
|
|
$('panel').setAttribute ('hidden', ''); |
238 |
|
|
} |
239 |
|
|
} |
240 |
|
|
|
241 |
|
|
</script> |