This repository was archived by the owner on Dec 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathplayground.js
More file actions
205 lines (177 loc) · 7.86 KB
/
Copy pathplayground.js
File metadata and controls
205 lines (177 loc) · 7.86 KB
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
/* JS for the Playground */
(function () {
var codeInput = document.getElementById('code-input'),
codeEditor = document.getElementById('code-editor');
/** @type {HTMLCanvasElement} */
let canvasOut = document.getElementById('canvas-out'),
lineNumbers = document.getElementById('line-numbers');
/**
* @type {GasmEngine}
*/
var engine = new GasmEngine(canvasOut, {
text: (codeInput.innerText || codeInput.textContent || '')
});
// init canvas...
(function initializeCanvas() {
//get DPI
let dpi = window.devicePixelRatio;
//get canvas
/** @type {HTMLCanvasElement} */
let canvas = canvasOut;
/** A solution for the blurry stuff. I didn't invent this, but I did modify it.
* @see https://medium.com/wdstack/fixing-html5-2d-canvas-blur-8ebe27db07da */
function fix_dpi() {
//get CSS height
//the + prefix casts it to an integer
//the slice method gets rid of "px"
let style_height = +getComputedStyle(canvas).getPropertyValue("height").slice(0, -2);
//get CSS width
let style_width = +getComputedStyle(canvas).getPropertyValue("width").slice(0, -2);
//scale the canvas
//canvas.setAttribute('height', style_height * dpi);
//canvas.setAttribute('width', style_width * dpi);
canvas.height = style_height * dpi;
canvas.width = style_width * dpi;
engine.updateSize();
}
fix_dpi();
window.addEventListener('load', fix_dpi);
window.addEventListener('resize', () => fix_dpi()); // listen
})();
/**
* Initialising the .code-editor & related elements
*/
function initializeCodeEditor() {
/**
* Set the function which reactes to the input's
* content change events
* @param {MutationCallback} handler
* @param {MutationObserverInit} options
*/
function setInputEventHandler(handler, options) {
/* codeInput.addEventListener('change', function(ev) {
console.log(this.innerHTML);
}); */
var observer = new MutationObserver(function (mutations, observer) {
observer.disconnect();
handler(mutations, observer);
observer.observe(codeInput, options);
});
observer.observe(codeInput, options);
// this 'prevent formatted clipboard content' idea
// was got from http://jsfiddle.net/HBEzc/
codeInput.addEventListener("paste", function (e) {
e.preventDefault();
console.debug("Performing plaintext pasting...");
var text = e.clipboardData.getData("text/plain");
document.execCommand("insertHTML", false, text);
});
}
// [Updated on 2022-12-01]: Patched so it works after 2 years...
let preset = document.getElementById('preset');
preset.addEventListener("change", function (ev) {
let t = null;
if (t = document.getElementById(preset.value)) {
// --PATCH.2022.12.01-START--
// --@remove:
// document.getElementById('code-input').innerHTML = t.innerHTML;
// --@add:
codeInput.innerHTML = (t.innerHTML
.replace(/^\r?\n/, "")
.split('\n')
.reduce(function (acc, line) {
return acc + "<div>" + (line ? line : "<br/>") + "</div>";
}, "")
);
// --PATCH.2022.12.01-END--
}
});
setInputEventHandler(
function (mutations, observer) {
mutations.forEach(function (mutation, index) {
var plainTextCode = (codeInput.innerText || codeInput.textContent);
if (codeInput.innerHTML.search(/\s*<div><br\s?\/?>(?:<\/div>)?\s*/) != -1) {
// removes the doubled new lines caused by <div><br /></div> by Chromium
plainTextCode = plainTextCode.replace(/(\r\n|\r|\n){2}/g, '$1');
}
var highlightedCode = plainTextCode;
if (/\S/.test(plainTextCode)) {
highlightedCode = gasmHTMLSyntaxHighlight(plainTextCode, engine.consumers);
} else {
const lastLinePos = plainTextCode.lastIndexOf("\r\n");
highlightedCode = plainTextCode
.substring(0, (
(lastLinePos >= 0) ? lastLinePos : plainTextCode.lastIndexOf('\n')
));
}
const lineCount = ((highlightedCode.split(/\r?\n/g) || []).length || 1);
const initialLineNumber = (
(lineCount != lineNumbers.childElementCount) ?
(lineNumbers.childElementCount + 1) :
((lineCount + 1))
);
if (lineCount >= lineNumbers.childElementCount) {
for (var i = initialLineNumber; i <= lineCount; i++) {
let lineNumber = document.createElement('span');
lineNumber.innerText = i.toString(10); // base-10
lineNumbers.appendChild(lineNumber); // adds it to the line numbers
// DEBUG: console.log('+');
}
} else {
for (var i = initialLineNumber; i > lineCount; i--) {
if (lineNumbers.childElementCount > 1) {
lineNumbers.removeChild(lineNumbers.lastElementChild);
}
}
}
codeEditor.innerHTML = highlightedCode;
});
}, {
subtree: true,
childList: true,
characterData: true,
characterDataOldValue: true
}
);
}
initializeCodeEditor();
var runButton = document.getElementById('run');
var context = canvasOut.getContext("2d");
function clearCanvas() {
// Store the current transformation matrix
context.save();
// Use the identity matrix while clearing the canvas
context.setTransform(1, 0, 0, 1, 0, 0);
context.clearRect(0, 0, canvasOut.width, canvasOut.height);
context.beginPath();
// Restore the transform
context.restore();
};
(function registerRunBurronCallbacks() {
clearCanvas();
runButton.addEventListener('click', function (ev) {
setTimeout(() => (async function () { // in async
clearCanvas();
let newSource = { text: (codeInput.innerText || codeInput.textContent || '') + '\n' };
// let engine = new GasmEngine(canvasOut, { text: codeInput.innerText });
engine.resetLexer(newSource);
console.debug(newSource);
let iCount = 0;
while (engine.interpretInstruction() != true) {
iCount++;
}
console.info("[JS-GASM]: Excecuted " + iCount + " GASM instructions.");
})(), 0);
});
})();
(function registerSaveOutputButtonCallback() {
document.getElementById('save-output').addEventListener('click', function (ev) {
let encodedData = canvasOut.toDataURL();
console.info(encodedData);
let dummyAnchor = document.createElement('a');
dummyAnchor.href = encodedData;
dummyAnchor.download = "canvas.png";
dummyAnchor.click();
})
})();
})();