-
Notifications
You must be signed in to change notification settings - Fork 19
/
util.js
48 lines (41 loc) · 958 Bytes
/
util.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
var print = console.log
function sum(arr){
return arr.reduce(function(sum, element){
return sum + element
}, 0)
}
function init_array(length, init){
var arr = Array(length)
arr.fill(init)
return arr
}
function draw_rect(x,y,dx,dy,c){
CTX.fillStyle = c
CTX.fillRect(x, y, dx, dy)
}
function draw_rect_edge(x,y,dx,dy,c){
CTX.strokeStyle = c
CTX.strokeRect(x, y, dx, dy)
}
function draw_circle(x,y,r,c){
CTX.fillStyle = c
CTX.beginPath()
CTX.arc(x,y,r,0,2*Math.PI)
CTX.closePath()
CTX.fill()
}
function draw_line(x0,y0,x1,y1,w,c){
CTX.beginPath()
CTX.moveTo(x0, y0)
CTX.lineTo(x1, y1)
CTX.lineWidth = w
CTX.strokeStyle = c
CTX.stroke()
}
function draw_text(text, x, y, c, fontsize, textAlign="center"){
CTX.fillStyle = c
CTX.font = fontsize + "px " + "sans-serif"
CTX.textBaseline = "middle"
CTX.textAlign = textAlign
CTX.fillText(text, x, y)
}