-
Notifications
You must be signed in to change notification settings - Fork 1
/
plot.js
97 lines (84 loc) · 1.94 KB
/
plot.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
const GRID_COLORS = ['#363636', '#282828', '#313131', '#282828']
const LINE_COLOR = '#fff'
const DASHES = [2,2]
const NO_DASHES = []
const COMPOSITE = 'darken'
export default ({ ctx, width: w, height: h, pixelRatio: pr }, size = 1) => {
size = Math.min(size, 9999)
let i = 0, x = 0
let cx = 0, cy = 0
const length = 2048 //buffer.length //2**11
// const buffer = new Float32Array(length)
const step = 2/length
let buffer = []
const f = x => buffer[x] //x*(size)/2
const resize = ({ width, height }) => {
w = width
h = height
}
const drawX = () => {
ctx.beginPath()
ctx.setLineDash(DASHES)
ctx.strokeStyle = GRID_COLORS[0]
ctx.moveTo(0, h/2)
ctx.lineTo(w, h/2)
ctx.stroke()
}
const drawY = () => {
const sc = size*2*2
const vlines = Math.floor(2/sc)
if (sc > 100) {
ctx.beginPath()
ctx.strokeStyle = GRID_COLORS[0]
ctx.moveTo(w/2,0)
ctx.lineTo(w/2,h)
ctx.stroke()
} else {
const vlw = (w/sc)
for (let i = 1; i < sc; i++) {
ctx.beginPath()
ctx.strokeStyle = GRID_COLORS[i%4]
ctx.moveTo(i*vlw,0)
ctx.lineTo(i*vlw,h)
ctx.stroke()
}
}
}
function drawLine () {
i = 0
cx = 0; cy = h/2
ctx.setLineDash(NO_DASHES)
ctx.beginPath()
ctx.lineWidth = 1.5/pr
ctx.strokeStyle = LINE_COLOR
ctx.globalCompositeOperation = COMPOSITE
x = -1
const calc = () => {
cx = ((x+1)*.5)*w
cy = (( 1-(f(i++)+1)*.5)*(h-2))+1
}
calc()
ctx.moveTo(cx, cy)
for (x = -1; x <= 1; x += step) {
calc()
ctx.lineTo(cx,cy)
}
ctx.stroke()
}
return {
drawX,
drawY,
drawLine,
resize,
setBuffer (_buffer) {
buffer = _buffer
},
setSize (_size) {
size = _size
},
}
}
// const sin = x => Math.sin(x*2*Math.PI)
// let i = 2
// plot(x => Math.exp(-x*Math.atan(x*2)*5), i)
// // setInterval(() => plot(sin, ++i), 1500)