-
Notifications
You must be signed in to change notification settings - Fork 7
/
canvas.js
73 lines (63 loc) · 2.5 KB
/
canvas.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
(function () {
const SAMPLE_EXPRESSIONS = [
'(a + b)*c - (x - y)/z',
'(a * b) - c + z / x',
'x - y + (c / (a + b))',
'(a / y) + b - (c * x)',
'(a - b) * (c + d) / z',
'(a * b) - (x / y)'
]
var canvas = document.querySelector('canvas')
var c = canvas.getContext('2d')
function clearCanvas() { c.clearRect(0, 0, canvas.width, canvas.height) }
document.getElementById('generate-tree').addEventListener('click', () => {
var expression = document.getElementById('expression-input').value
if (typeof expression !== 'undefined' && null != expression) {
expression = expression.replace(/\s+/g, '')
expression = expression.toLowerCase()
var postfix = infixToPostfix(expression);
if (null !== postfix) {
try {
var root = constructTree(postfix)
setCoordinates(root)
clearCanvas()
canvas.height = document.getElementById('canvas-container').offsetHeight;
canvas.width = document.getElementById('canvas-container').offsetWidth;
drawTree(root, c)
} catch (e) {
displayErrorMessage()
}
} else {
displayErrorMessage()
}
} else {
displayErrorMessage()
}
})
document.getElementById('clear-tree').addEventListener('click', () => {
document.getElementById('expression-input').value = ''
clearCanvas()
})
document.getElementById('expression-input').value = SAMPLE_EXPRESSIONS[Math.floor(Math.random() * SAMPLE_EXPRESSIONS.length)]
setTimeout(() => {
document.getElementById('generate-tree').click()
}, 500)
})();
function displayErrorMessage() {
Swal.fire({
icon: 'error',
title: 'Invalid expression',
html: `
<div style="font-size:1.1em;text-align: left;margin:0px 0px 0px 60px;">
- You may only use these brackets ( ). <br/>
- Use * for multiplication and / for division. <br/>
- Valid operators and operands are:<br/>
<div style="margin-left: 10px;">
<i>Operators</i>: <b>[+ - * / ]</b><br/>
<i>Operands</i>: Any alphabetic letter.
</div>
</div>
`,
footer: '<a href="https://github.com/lnogueir/expression-tree-gen">Learn more</a>'
})
}