-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaking-of-circle-drawing.js
179 lines (155 loc) · 5.97 KB
/
making-of-circle-drawing.js
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
/*
* From https://www.redblobgames.com/making-of/circle-drawing/
* Copyright 2022 Red Blob Games <[email protected]>
* @license Apache-2.0 <https://www.apache.org/licenses/LICENSE-2.0.html>
*/
console.info("I'm happy to answer questions about the code; email me at [email protected]");
import Vue from './vue.v2.esm.browser.js';
import {Prism, Diff} from './_build/lib.js';
/* Highlight Vue attributes as containing javascript values */
Prism.languages.markup.tag.addAttribute(
/(?:v-|:|@)[-.a-zA-Z0-9]+/.source,
'javascript'
);
/* Highlight Vue template inside js; based on prism-js-templates.js */
Prism.languages.javascript['template-string'].inside.string = {
pattern: /[\s\S]+/,
inside: Prism.languages.html
};
/** count how many spaces are at the beginning of a line */
function measureIndentation(line) {
return line.length - line.trimStart().length;
}
/** extract some indented text starting with regexp 'begin', and
including with if a closing line matches regexp 'end', and
de-indent those lines */
function extractSection(input, begin, end=/[/}]/) {
let lines = input.split('\n');
let output = [];
let matchIndentation = null;
for (let line of lines) {
// State machine: either we're inside or outside the matched region
let beginMatch = line.match(begin);
let endMatch = line.match(end);
let indentation = measureIndentation(line);
if (matchIndentation === null && beginMatch) { // outside
matchIndentation = indentation;
}
if (indentation < matchIndentation) { // past the block
matchIndentation = null;
}
if (matchIndentation !== null) { // inside
output.push(line.slice(matchIndentation));
if (endMatch && !beginMatch && indentation === matchIndentation) {
matchIndentation = null;
}
}
}
return output.join('\n');
}
/** output diff lines with '+' or '-' or ' ' at the beginning */
function calculateDiffs(oldText, newText) {
let oldLines = oldText.split('\n');
let newLines = newText.split('\n');
const isCommon = (aIndex, bIndex) => oldLines[aIndex].trimStart() === newLines[bIndex].trimStart();
let outputLines = [];
let ai = 0, bi = 0
function record(type, lines) {
for (let line of lines) outputLines.push(`${type}${line}`);
}
function foundSubsequence(nCommon, aCommon, bCommon) {
// ai to aCommon are "deleted"
// bi to bCommon are "inserted"
// aCommon to aCommon+nCommon are "same"
// bCommon to bCommon+nCommon are "same"
record('-', oldLines.slice(ai, aCommon));
record('+', newLines.slice(bi, bCommon));
record(' ', oldLines.slice(aCommon, aCommon+nCommon));
ai = aCommon + nCommon;
bi = bCommon + nCommon;
}
Diff(oldLines.length, newLines.length, isCommon, foundSubsequence);
record('-', oldLines.slice(ai));
record('+', newLines.slice(bi));
return outputLines.join('\n');
}
Vue.component('a-output', {
props: ['step', 'skip-nav'],
template: `<div class="output">
<nav v-if="!skipNav">Step {{step}}
| <a target="_blank" :href="step+'/'">Open ⧉</a>
| <a target="_blank" :href="'https://github.com/redblobgames/making-of-circle-drawing/tree/main/'+step+'/'">Source ⧉</a>
</nav>
<iframe ref="iframe" :src="step+'/'" scrolling="no" @load="resizeIFrame" />
</div>`,
methods: {
resizeIFrame() {
let {iframe} = this.$refs;
iframe.style.height = iframe.contentDocument.body.clientHeight + "px";
},
},
});
Vue.component('a-step', {
props: ['step', 'show', 'diff', 'restrict'],
data() {
return {
tab: this.show ?? 'html', /* html, js */
prevHtml: "",
prevJs: "",
currHtml: "",
currJs: "",
};
},
template: `
<pre v-else><code :class="languageClass" v-html="syntaxHighlightedText"/></pre>
`,
computed: {
curr() { return this.step + "/"; },
prev() { return (parseFloat(this.step)-1) + "/"; },
lang() {
return {html: "handlebars", js: "javascript"}[this.tab];
},
languageClass() {
let diff = this.diff? "diff-" : "";
return `language-${diff}${this.lang}`;
},
syntaxHighlightedText() {
let [prev, curr] =
this.tab === 'html'? [this.prevHtml, this.currHtml]
: this.tab === 'js'? [this.prevJs, this.currJs]
: ["??", "??"];
if (this.restrict) {
prev = extractSection(prev, new RegExp(this.restrict));
curr = extractSection(curr, new RegExp(this.restrict));
}
if (this.diff) {
return Prism.highlight(calculateDiffs(prev, curr),
Prism.languages.diff,
"diff-" + this.lang);
} else {
return Prism.highlight(curr,
Prism.languages[this.lang],
this.lang);
}
},
},
async mounted() {
if (this.prev) {
let loadPrevHtml = fetch(this.prev + "index.html");
let loadPrevJs = fetch(this.prev + "circle-drawing.js");
this.prevHtml = await (await loadPrevHtml).text();
this.prevJs = await (await loadPrevJs).text();
}
if (this.curr) {
let loadCurrHtml = fetch(this.curr + "index.html");
let loadCurrJs = fetch(this.curr + "circle-drawing.js");
this.currHtml = await (await loadCurrHtml).text();
this.currJs = await (await loadCurrJs).text();
}
},
});
for (let figure of document.querySelectorAll("figure")) {
new Vue({
el: figure,
});
}