aboutsummaryrefslogtreecommitdiffhomepage
path: root/experimental/webtry/templates/index.html
blob: 00a245e067c6735e583e8d83fce3cb08ba54fc65 (plain)
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
<!DOCTYPE html>
<html>
<head>
    <title>Skia WebTry</title>
    <meta charset='utf-8' />
    <style type="text/css" media="screen">
        .waiting, .waiting * {
          cursor: wait;
        }
        textarea {
            margin-left: 0;
            border: solid 1px #ccc;
            color: green;
        }
        pre, code {
            padding: 0;
            color: green;
        }
        #output {
            color: #333;
        }
    </style>
</head>
<body>
  <pre><code>#include "SkCanvas.h"

void draw(SkCanvas* canvas) {
  <textarea name='code' id='code' rows='15' cols='80'>SkPaint p;
p.setColor(SK_ColorRED);
p.setAntiAlias(true);
p.setStyle(SkPaint::kStroke_Style);
p.setStrokeWidth(10);

canvas->drawLine(20, 20, 100, 100, p);
</textarea>
}
</code></pre>

  <input type='button' value='Run' id='run'>

  <p>Image appears here:</p>
  <img id='img' src=''/>

  <pre><code id='output'></code></pre>

  <script type='text/javascript' charset='utf-8'>
      var run = document.getElementById('run');
      var code = document.getElementById('code');
      var output = document.getElementById('output');
      var img = document.getElementById('img');

      function beginWait() {
        document.body.classList.add('waiting');
        run.disabled = true;
      }

      function endWait() {
        document.body.classList.remove('waiting');
        run.disabled = false;
      }

      function codeComplete(e) {
        // The response is JSON of the form:
        // {
        //   "message": "you had an error...",
        //   "img": "<base64 encoded image but only on success>"
        // }
        //
        // The img is optional and only appears if there is a valid
        // image to display.
        endWait();
        console.log(e.target.response);
        body = JSON.parse(e.target.response);
        output.innerText = body.message;
        if (body.hasOwnProperty('img')) {
          img.src = 'data:image/png;base64,' + body.img;
        } else {
          img.src = '';
        }
      }

      function codeError(e) {
        endWait();
        alert('Something bad happened: ' + e);
      }

      run.addEventListener('click', onSubmitCode);
      function onSubmitCode() {
        beginWait();
        var req = new XMLHttpRequest();
        req.addEventListener('load', codeComplete);
        req.addEventListener('error', codeError);
        req.overrideMimeType('application/json');
        req.open('POST', '.', true);
        req.send(code.value);
      }
  </script>
</body>
</html>