-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
163 lines (135 loc) · 3.53 KB
/
Copy pathscript.js
File metadata and controls
163 lines (135 loc) · 3.53 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
'use strict'
import Graph from './Graph.js'
import { inputToValidMath } from './functionManipulations.js'
import {factorial} from './gamma.js'
const precision = 0.1
const input = document.querySelector('input.function')
const centerButton = document.querySelector('button.center')
// const widthInput = document.querySelector('input.x')
// const widthSpan = document.querySelector('span.x')
const heightInput = document.querySelector('input.y')
const heightSpan = document.querySelector('span.y')
const canvas = document.createElement('canvas')
canvas.width = window.innerWidth
canvas.height = window.innerHeight
const ctx = canvas.getContext('2d')
document.body.appendChild(canvas)
let func = input.value
function f(x) {
let treatedFunc = func.replaceAll(' ', '').trim()
if (treatedFunc.length === 0) {
stop = true
return
}
treatedFunc = inputToValidMath(treatedFunc, x)
try {
return eval(treatedFunc)
} catch (e) {
stop = true
return
}
}
let stop = false
let w = 50
let h = 50
let graph
function setup() {
zoom = 1
ctx.restore()
graph = new Graph(ctx, w, h, canvas.width, canvas.height)
run()
}
// widthInput.oninput = debounce(setup, 300, () => {
// widthSpan.textContent = 'Graph width: ' + widthInput.value
// w = +widthInput.value
// })
// widthInput.oninput()
heightInput.oninput = debounce(setup, 300, () => {
h = +heightInput.value
heightSpan.textContent = `Graph height: ${h} (-${h / 2} .. ${h / 2})`
})
heightInput.oninput()
function reset() {
ctx.clearRect(
-(canvas.width * 100),
-(canvas.height * 100),
canvas.width * 1000,
canvas.height * 1000
)
}
function run() {
func = input.value
reset()
graph.drawAxes()
const lastPoint = {
x: null,
y: null,
}
for (let i = -graph.width / 2; i <= graph.width / 2; i += precision) {
if (stop) {
stop = false
break
}
const x = i
const y = f(x)
if (lastPoint.x !== null) graph.drawLine(lastPoint.x, lastPoint.y, x, y)
lastPoint.x = x
lastPoint.y = y
}
}
// button.onclick = run
input.oninput = debounce(run, 200)
let isMouseDown = false
let zoom = 1
const lastMousePos = { x: null, y: null }
document.body.onmousemove = (e) => {
if (lastMousePos.x === null || !isMouseDown) {
lastMousePos.x = e.clientX
lastMousePos.y = e.clientY
return
}
const xDistance = (e.clientX - lastMousePos.x) / zoom
const yDistance = (e.clientY - lastMousePos.y) / zoom
// const image = new Image()
// image.onload = () => {
ctx.translate(xDistance, yDistance)
// reset()
// ctx.drawImage(image, 0, 0)
// }
// image.src = canvas.toDataURL()
run()
lastMousePos.x = e.clientX
lastMousePos.y = e.clientY
}
canvas.onmousedown = () => {
isMouseDown = true
}
canvas.onmouseup = () => {
isMouseDown = false
}
canvas.onmouseleave = canvas.onmouseup
centerButton.onclick = () => {
setup()
}
document.body.onwheel = (e) => {
const ratio = e.deltaY > 0 ? 0.9 : 1.1
zoom *= ratio
ctx.scale(ratio, ratio)
run()
}
window.onresize = debounce(() => {
canvas.width = window.innerWidth
canvas.height = window.innerHeight
setup()
})
function debounce(cb, delay = 200, ignoredCb) {
let timeout
return (...args) => {
if (ignoredCb) ignoredCb(...args)
clearTimeout(timeout)
timeout = setTimeout(() => {
cb(...args)
}, delay)
}
}
setup()