aboutsummaryrefslogtreecommitdiffhomepage
path: root/experimental/webtry/templates/index.html
blob: d2ba8592cb08743278868ab1094c82bb15a75a45 (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
<!DOCTYPE html>
<html>
<head>
    <title>Skia WebTry</title>
    <meta charset='utf-8' />
    <link rel="stylesheet" href="/css/" type="text/css" media="screen">
</head>
<body>
  <section id=title>
    <a href="/">Home</a>
    <a href="/recent">Recent</a>
    <a href="https://github.com/google/skia/tree/master/experimental/webtry">Code</a>
  </section>
  <section id=content>
  <pre><code>#include "SkCanvas.h"

void draw(SkCanvas* canvas) {
  <textarea name='code' id='code' rows='15' cols='80'>{{.UserCode}}</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>

  </section>
  <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 = '';
        }
        window.history.pushState(null, null, "/c/" + body.hash);
      }

      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>