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 |
|
|
.mode-viewer canvas { |
16 |
|
|
width: 100%; |
17 |
|
|
} |
18 |
|
|
a { |
19 |
|
|
display: inline-block; |
20 |
|
|
min-width: 1em; |
21 |
|
|
text-align: center; |
22 |
|
|
} |
23 |
|
|
menu { |
24 |
|
|
position: fixed; |
25 |
|
|
bottom: 0; |
26 |
|
|
width: 100%; |
27 |
|
|
margin: 0; |
28 |
|
|
padding: 0; |
29 |
|
|
font-size: 8px; |
30 |
|
|
} |
31 |
|
|
.mode-editor menu .viewer-only, |
32 |
|
|
.mode-viewer menu .editor-only { |
33 |
|
|
display: none; |
34 |
|
|
} |
35 |
|
|
[hidden] { |
36 |
|
|
display: none; |
37 |
|
|
} |
38 |
|
|
#panel { |
39 |
|
|
position: absolute; |
40 |
|
|
left: 20px; |
41 |
|
|
width: 200px; /* 240px - 20px * 2 */ |
42 |
|
|
top: 20px; |
43 |
|
|
max-height: 120px; /* 160px - 20px * 2 */ |
44 |
|
|
overflow: auto; |
45 |
|
|
background-color: #C0C0C0; |
46 |
|
|
} |
47 |
|
|
#panel p { |
48 |
|
|
margin: 0; |
49 |
|
|
padding: 0; |
50 |
|
|
} |
51 |
|
|
</style> |
52 |
|
|
|
53 |
|
|
<body> |
54 |
|
|
|
55 |
|
|
<canvas width=240 height=160></canvas> |
56 |
|
|
|
57 |
|
|
<section id=panel hidden> |
58 |
|
|
|
59 |
|
|
<p> |
60 |
|
|
[<a href="javascript:setProp ('lineWidth', 1); togglePanel ()">1</a> |
61 |
|
|
<a href="javascript:setProp ('lineWidth', 2); togglePanel ()">3</a> |
62 |
|
|
<a href="javascript:setProp ('lineWidth', 3); togglePanel ()">3</a> |
63 |
|
|
<a href="javascript:setProp ('lineWidth', 5); togglePanel ()">5</a> |
64 |
|
|
<a href="javascript:setProp ('lineWidth', 10); togglePanel ()">10</a>] |
65 |
|
|
</p> |
66 |
|
|
|
67 |
|
|
<p> |
68 |
|
|
[<a href="javascript:setProp ('strokeStyle', 'black'); togglePanel ()" style="color: black">black</a> |
69 |
|
|
<a href="javascript:setProp ('strokeStyle', 'red'); togglePanel ()" style="color: red">red</a> |
70 |
|
|
<a href="javascript:setProp ('strokeStyle', 'green'); togglePanel ()" style="color: green">green</a> |
71 |
|
|
<a href="javascript:setProp ('strokeStyle', 'blue'); togglePanel ()" style="color: blue">blue</a> |
72 |
|
|
<a href="javascript:setProp ('strokeStyle', 'orange'); togglePanel ()" style="color: orange">orange</a> |
73 |
|
|
<a href="javascript:setProp ('strokeStyle', 'yellow'); togglePanel ()" style="color: yellow">yellow</a> |
74 |
|
|
<a href="javascript:setProp ('strokeStyle', 'pink'); togglePanel ()" style="color: pink">pink</a> |
75 |
|
|
<a href="javascript:setProp ('strokeStyle', 'purple'); togglePanel ()" style="color: purple">purple</a> |
76 |
|
|
<a href="javascript:setProp ('strokeStyle', 'gray'); togglePanel ()" style="color: gray">gray</a> |
77 |
|
|
<a href="javascript:setProp ('strokeStyle', 'white'); togglePanel ()" style="color: white">white</a>] |
78 |
|
|
</p> |
79 |
|
|
|
80 |
|
|
</section> |
81 |
|
|
|
82 |
|
|
<menu><a href="javascript:togglePanel ()" class=editor-only>Panel</a> |
83 |
|
|
<a href="client.html?mode=editor" id=edit-link rel=edit class=viewer-only>Edit</a> |
84 |
|
|
<a href="client.html?mode=viewer" id=view-link class=editor-only>View</a> |
85 |
|
|
<a href="server.cgi?mode=list" rel=index>List</a> |
86 |
|
|
<a href="server.cgi?mode=prev" id=prev-link rel=prev>Prev</a> |
87 |
|
|
<a href="server.cgi?mode=next" id=next-link rel=next>Next</a> |
88 |
|
|
<a href="client.html?mode=editor">New</a></menu> |
89 |
|
|
|
90 |
|
|
<script> |
91 |
|
|
function $ (id) { return document.getElementById (id) } |
92 |
|
|
|
93 |
|
|
var canvas = document.getElementsByTagName('canvas')[0]; |
94 |
|
|
var ctx = canvas.getContext('2d'); |
95 |
|
|
ctx.lineCap = 'round'; |
96 |
|
|
ctx.lineWidth = 3; |
97 |
|
|
ctx.beginPath (); |
98 |
|
|
|
99 |
|
|
var date; |
100 |
|
|
var q = (location.search || '').replace (/^\?/, '').split (/[&;]/); |
101 |
|
|
var param = {}; |
102 |
|
|
for (var i = 0; i < q.length; i++) { |
103 |
|
|
var p = q[i].split (/=/); |
104 |
|
|
param[p[0]] = p[1]; |
105 |
|
|
} |
106 |
|
|
date = parseInt (param.date) || new Date ().valueOf (); |
107 |
|
|
var url = 'server.cgi?date=' + date; |
108 |
|
|
|
109 |
|
|
$('edit-link').href += ';date=' + date; |
110 |
|
|
$('view-link').href += ';date=' + date; |
111 |
|
|
$('prev-link').href += ';date=' + date; |
112 |
|
|
$('next-link').href += ';date=' + date; |
113 |
|
|
|
114 |
|
|
param.mode = param.mode || 'editor'; |
115 |
|
|
document.body.className = 'mode-' + param.mode; |
116 |
|
|
|
117 |
|
|
if (param.mode == 'viewer') { |
118 |
|
|
readRemoteImage (); |
119 |
|
|
setInterval (readRemoteImage, 2000); |
120 |
|
|
} else { |
121 |
|
|
var drawing = false; |
122 |
|
|
canvas.onmousedown = function (ev) { |
123 |
|
|
drawing = true; |
124 |
|
|
ctx.beginPath (); |
125 |
|
|
var x = ev.clientX; |
126 |
|
|
var y = ev.clientY; |
127 |
|
|
ctx.moveTo (x, y); |
128 |
|
|
write ('moveTo', x, y); |
129 |
|
|
}; |
130 |
|
|
canvas.onmousemove = function (ev) { |
131 |
|
|
if (drawing) { |
132 |
|
|
var x = ev.clientX; |
133 |
|
|
var y = ev.clientY; |
134 |
|
|
ctx.lineTo (x, y); |
135 |
|
|
write ('lineTo', x, y); |
136 |
|
|
ctx.stroke (); |
137 |
|
|
// ctx.closePath (); |
138 |
|
|
// ctx.beginPath (); |
139 |
|
|
// ctx.moveTo (x, y); |
140 |
|
|
} |
141 |
|
|
}; |
142 |
|
|
window.onmouseup = function (ev) { |
143 |
|
|
if (!drawing) return; |
144 |
|
|
ctx.stroke (); |
145 |
|
|
ctx.closePath (); |
146 |
|
|
drawing = false; |
147 |
|
|
}; |
148 |
|
|
|
149 |
|
|
var newEvents = []; |
150 |
|
|
function write () { |
151 |
|
|
newEvents.push (Array.prototype.join.call (arguments, ',')); |
152 |
|
|
} |
153 |
|
|
|
154 |
|
|
readRemoteImage (); |
155 |
|
|
setInterval (function () { |
156 |
|
|
if (newEvents.length) { |
157 |
|
|
var xhr = new XMLHttpRequest (); |
158 |
|
|
xhr.open ('POST', url, true); |
159 |
|
|
xhr.setRequestHeader ('Content-Type', 'text/plain'); |
160 |
|
|
xhr.send (newEvents.join (';')); |
161 |
|
|
newEvents = []; |
162 |
|
|
} |
163 |
|
|
}, 2000); |
164 |
|
|
} |
165 |
|
|
|
166 |
|
|
function setProp (n, v) { |
167 |
|
|
ctx[n] = v; |
168 |
|
|
write (n, v); |
169 |
|
|
} |
170 |
|
|
|
171 |
|
|
function readRemoteImage () { |
172 |
|
|
var xhr = new XMLHttpRequest (); |
173 |
|
|
xhr.open ('GET', url, false); |
174 |
|
|
xhr.setRequestHeader ('Cache-Control', 'no-cache'); |
175 |
|
|
xhr.send (null); |
176 |
|
|
if (xhr.responseText) { |
177 |
|
|
var events = xhr.responseText.split (/\x0A/); |
178 |
|
|
if (events.length) { |
179 |
|
|
canvas.width = canvas.width; // clear |
180 |
|
|
processEvents(events); |
181 |
|
|
} |
182 |
|
|
} |
183 |
|
|
} |
184 |
|
|
|
185 |
|
|
function processEvents (events) { |
186 |
|
|
for (var i = 0; i < events.length; i++) { |
187 |
|
|
var ev = events[i].split (/,/); |
188 |
|
|
if (ev[0] == 'lineTo') { |
189 |
|
|
var x = parseFloat (ev[1]); |
190 |
|
|
var y = parseFloat (ev[2]); |
191 |
|
|
ctx.lineTo (x, y); |
192 |
|
|
ctx.stroke (); |
193 |
|
|
// ctx.closePath (); |
194 |
|
|
// ctx.beginPath (); |
195 |
|
|
// ctx.moveTo (x, y); |
196 |
|
|
} else if (ev[0] == 'moveTo') { |
197 |
|
|
var x = parseFloat (ev[1]); |
198 |
|
|
var y = parseFloat (ev[2]); |
199 |
|
|
ctx.moveTo (x, y); |
200 |
|
|
} else if (ev[0] == 'strokeStyle' || ev[0] == 'lineWidth') { |
201 |
|
|
ctx.closePath (); |
202 |
|
|
ctx.beginPath (); |
203 |
|
|
ctx[ev[0]] = ev[1]; |
204 |
|
|
} |
205 |
|
|
} |
206 |
|
|
} |
207 |
|
|
|
208 |
|
|
function togglePanel () { |
209 |
|
|
if ($('panel').hasAttribute ('hidden')) { |
210 |
|
|
$('panel').removeAttribute ('hidden'); |
211 |
|
|
} else { |
212 |
|
|
$('panel').setAttribute ('hidden', ''); |
213 |
|
|
} |
214 |
|
|
} |
215 |
|
|
</script> |