-
Notifications
You must be signed in to change notification settings - Fork 0
/
thlogo.html
378 lines (351 loc) · 11.1 KB
/
thlogo.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
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
<!DOCTYPE html>
<html lang = "th-TH">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>โลโก้ ก ข</title>
<style>
div {
border: 1px solid lightgray;
}
label, select{
font-weight: bold;
display:flex;
flex-direction: column;
}
#svgarea {
width: 300px;
height: 300px;
position: relative;
background: "#e6e6e6";
padding 5px;
margin: 2px;
}
svg {
width: 100%;
height: 100%;
position: absolute;
}
#tt {
width: 20px;
height: 20px;
border: none;
background-color: transparent;
}
#opt {
width: 42px;
height:300px;
color: lime;
background-color: black;
display: inline-block;
vertical-align: top;
position: absolute;
top: 9px;
left: 315px;
}
.pcolors {
display: block;
vertical-align: top;
}
#pc {
width: 3em;
}
#ptype {
display: flex;
flex-direction: column;
}
#pt {
width: 3em;
}
#cmds {
width: 350px;
color: lime;
background-color: black;
display: flex;
flex-direction: row;
}
#opcode {
width: 4.5em;
}
#operand{
color: lime;
}
#num {
width: 4em;
}
#cli {
width: 12em;
background-color: bisque;
}
button {
width: 40px;
height: 48px;
font-size: 32px;
background-color: gray;
position: absolute;
left: 318px;
}
#log {
width: 350px;
color: black;
font-size: 13px;
border: 1px dotted gray;
position: absolute;
top: 370px;
display: block;
}
</style>
<body>
<div id="svgarea"> </div>
<div id="opt">
<div class="pcolors">
<label for="color">สี</label>
<select name="color" id="pc"/>
<option value="black">⬛️</option>
<option value="red">🟥️</option>
<option value="orange">🟧️</option>
<option value="yellow">🟨️</option>
<option value="green">🟩️</option>
<option value="blue">🟦️</option>
<option value="indigo">🟪️</option>
<option value="brown">🟫️</option>
<option value="white">⬜️</option>
</select>
</div>
<div class="ptype">
<label for="type">เส้น</label>
<select name="type" id="pt"/>
<option value="1">..</option>
<option value="2">--</option>
<option value="4">==</option>
<!-- these add style to line
<option value="dotted">...</option>
<option value="dashed">---</option>
-->
</select>
</div>
</div>
<div id="cmds">
<div id="opcode" display: "inline-block">
<label for="cmd">เดิน|หัน</label>
<select name="cmd" id="opc"/>
<option value="pd">เส้น ⬛️</option>
<option value="pu">เว้น 🔲️</option>
<option value="h">บ้าน 🎯</option>
<option value="lt">ซ้าย ⬅️</option>
<option value="rt">ขวา ➡️</option>
<option value="bk">หลัง ⬇️</option>
<option value="fd">หน้า ⬆️</option>
<option value="cs">ลบ 🧹️</option>
<!-- these commands are **todo**
<option value="lo">จุดนี้ 📍️</option>
<option value="bg">ฉาก 🌅️</option>
<option value="{ ">จาก {</option>
<option value="} ">ถึง }</option>
<option value="go">ทำ ✔️</option>
<option value="q">เลิก ❌️</option>
-->
</select>
</div>
<div class="operand" style="display: inline-block;" >
<label for="num">ก้าว| ํ</label>
<input type="number" id="num" width=4 min="-300" value= "50" max="300"/>
</div>
<div id=>
<label for="cli">คำสั่ง 'op [999]'</label>
<input type="text" id="cli" onchange="parsecmd()" onfocus="this.value=''"></input>
</div>
<div><button id="run" onclick=getcmd()>🚀️</button>
</div>
<div id="log"></div>
<script src="script/beeper2.js" type="text/javascript"></script>
<script>
SVG = {
createCanvas : function( width, height, containerId ){
var container = document.getElementById( containerId );
var canvas = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
canvas.setAttribute('width', width);
canvas.setAttribute('height', height);
container.appendChild( canvas );
return canvas;
},
createLine : function (x1, y1, x2, y2, color, w) {
var aLine = document.createElementNS('http://www.w3.org/2000/svg', 'line');
aLine.setAttribute('x1', x1);
aLine.setAttribute('y1', y1);
aLine.setAttribute('x2', x2);
aLine.setAttribute('y2', y2);
aLine.setAttribute('stroke', color);
aLine.setAttribute('stroke-width', w);
return aLine;
},
createText : function (x, y, dx, dy) {
var aText = document.createElementNS('http://www.w3.org/2000/svg', 'text');
aText.setAttribute('x', x);
aText.setAttribute('y', y);
aText.setAttribute('dx', dx);
aText.setAttribute('dy', dy);
aText.setAttribute('font-size', "28px");
aText.setAttribute('text-anchor',"middle");
//aText.setAttribute('transform', 'rotate(0 150,150)');
return aText;
}
}
function len2XY (l, hd) {
var dx = Math.round(l*Math.sin(hd*Math.PI/180));
var dy = Math.round(l*Math.cos(hd*Math.PI/180));
return [cXY[0]+dx, cXY[1]-dy];
}
function beep(){
var freq1=64, vol1=0.4, dur1=250, freq2=128, vol2=0.3, dur2=500;
beep2(freq1,0,vol1,dur1, freq2,0,vol2,dur2);
}
// ***Initialize svg area and variables
var _WIDTH = 300; _HEIGHT = 300;
var _PD = true;
var _COL = 'black';
var _TYPE= 2;
var w = null, aSvg = null, t = null; tt = null; // ids of elements
var cXY=[_WIDTH/2, _HEIGHT/2]; // current XY, init'd to mid XY
var tXY=[]; // target XY set by len2XY()
// ?what we see as emojis are NOT what they show on svg text element?
var timgs=["🐞️","🪳️","🪲️","🐠️","🥕️"]; // candidates for turtle image
var tRnd=[...Array(timgs.length).keys()].sort(() => Math.random() - 0.5);
var ti = tRnd[0]; // pick emoji
var tN=[0,0,0,90,-45]; //degrees to make turtle's head points North
var tad = tN[ti]; // emoji heading adjustment
var chd = 0; // current heading in deg, 0 is Nth, 90 E/Right. 180 Sth/Down,...
var ic = 0; // run cmd counter
//var _CLI = false; // commandline input assumed false
const log = document.getElementById("log"); //cmd log
function init() {
// set up svg area and 'turtle'
w = document.getElementById('svgarea');
aSvg = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
aSvg.setAttribute('width', _WIDTH);
aSvg.setAttribute('height', _HEIGHT);
t = SVG.createText(cXY[0], cXY[1], 0, '0.5em');
tg = document.createTextNode(timgs[ti]);
t.appendChild(tg);
t.setAttribute("transform", "rotate("+tad+" "+cXY+")" ); // adjust img
aSvg.appendChild(t); // put turtle to svg
w.appendChild(aSvg); // put svg in html container
}
// initialize and wait
if (t==null) {
init();
}
function parsecmd(){
var cls = "pd,pu,h,lt,rt,bk,fd,cs"; // command list for now
var cli = document.getElementById("cli").value; //command line
if (cli==null) {
return false;
}
var cl = cli.toLowerCase().split(" "); //split into [op,p1,p2...]
if (cls.indexOf(cl[0])== -1) {
beep();
logit="? ไม่รู้จัก "+cl[0]+" กรุณาสั่งใหม่ \n";
} else if (isNaN(parseInt(cl[1]))) {
beep();
logit="? ไม่ใช่ตัวเลข "+cl[1]+" กรุณาใส่ ตัวเลขใหม่ \n";
}
runcmd( {op: cl[0], num: cl[1], col: _COL, wid: _TYPE} );
}
function getcmd() {
// build cmd: {op:, ld:, col:, wid:n}
// ? ['fd',30,'blue',2] --> [x2,y2,col,width]
var opc = document.getElementById("opc"); //opcode
var op = opc.options[opc.selectedIndex].value;
var txt = opc.options[opc.selectedIndex].text; //for debug only
var num = document.getElementById("num").value;
// should do when they change but...
var pc = document.getElementById("pc"); //pen color
_COL = pc.options[pc.selectedIndex].value;
var col = pc.options[pc.selectedIndex].text;
var pt = document.getElementById("pt"); //pen type
_TYPE = pt.options[pt.selectedIndex].value;
var typ = pt.options[pt.selectedIndex].text;
runcmd( {op: op, num: num, col: _COL, wid: _TYPE} );
}
// when the button is pressed
function runcmd(cmd) {
// cmd={op: x, ld: n, col: c, wid: m}
var op = cmd.op; ld = parseInt(cmd.num);
var thd = chd+tad; //adj img heading
ic++; // cmd counter
switch (op) {
case "q": // quit
logit=ic+": "+op+" OK เลิกวาด?\n";
break;
case "cs": // clear svg area
logit=ic+": "+op+" OK ลบรูปออกทั้งหมด\n";
while (aSvg.hasChildNodes()) {
aSvg.removeChild(aSvg.firstChild);
}
cXY = [_WIDTH/2, _HEIGHT/2]; chd = 0;
t.setAttribute('x',cXY[0]); t.setAttribute('y',cXY[1]);
t.setAttribute('transform', "rotate("+tad+" " +cXY+")");
aSvg.appendChild(t); // put turtle to svg
break;
case "pd":
_PD = true;
logit=ic+": "+op+" วาด สี "+_COL+", แบบ "+_TYPE+"\n";
break;
case "pu":
_PD = false; _COL = "e6e6e6";
logit=ic+": "+op+" เว้นวาด สี "+_COL+", แบบ "+_TYPE+"\n";
break;
case "h":
cXY = [_WIDTH/2, _HEIGHT/2]; // leave heading as is
t.setAttribute('x',cXY[0]); t.setAttribute('y',cXY[1]);
t.setAttribute('transform', "rotate("+chd+" " +cXY+")");
logit=ic+": "+op+" กลับบ้าน\n";
break;
case "lt":
if (isNaN(ld)) return;
var hd = chd - ld; chd = hd % 360; thd = chd + tad;
//console.log("lt: chd="+chd+" thd="+thd+" cXY="+cXY);
t.setAttribute('transform', "rotate("+thd+" "+cXY+")");
logit=ic+": "+op+" "+ld+"; หันซ้าย "+ld+" ดีกรี\n";
break;
case "rt":
if (isNaN(ld)) return;
var hd = chd + ld; chd = hd % 360; thd = chd + tad;
//console.log("rt: chd="+chd+" thd="+thd+" cXY="+cXY);
t.setAttribute('transform', "rotate("+thd+" "+cXY+")");
logit=ic+": "+op+" "+ld+"; หันขวา "+ld+" ดีกรี\n";
break;
case "fd":
tXY = len2XY(ld, chd); //compute next point in XY
if (_PD) { //create the line and put it in svg area
line=SVG.createLine(cXY[0],cXY[1],tXY[0],tXY[1],_COL,_TYPE);
aSvg.appendChild(line); //lines.push(line); // add to aSvg
}
// move turtle to end point
t.setAttribute('x',tXY[0]); t.setAttribute('y',tXY[1]);
t.setAttribute('transform', "rotate("+thd+" " +tXY+")"); // not needed?
cXY=tXY; // set new current point
//console.log("cXY: "+cXY);
logit=ic+": "+op+" "+ld+"; ไปหน้า "+ld+" ก้าว สี "+_COL+", แบบ "+_TYPE+"\n";
break;
case "bk":
tXY = len2XY(-ld, chd); //compute next point in XY
if (_PD) { //create the line and put it in svg area
line=SVG.createLine(cXY[0],cXY[1],tXY[0],tXY[1],_COL,_TYPE);
aSvg.appendChild(line); //lines.push(line); // add to aSvg
}
// move turtle to end point
t.setAttribute('x',tXY[0]); t.setAttribute('y',tXY[1]);
t.setAttribute('transform', "rotate("+thd+" " +tXY+")"); // not needed
cXY=tXY; // set new current point
logit=ic+": "+op+" "+ld+"; ถอยหลัง "+ld+" ก้าว สี "+_COL+", แบบ "+_TYPE+"\n";
break;
}
log.innerText+=logit;
}
</script>
</body>
</html>
</script>
</body>
</html>