forked from kbroman/ProgrammingNotes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcanvas.txt
More file actions
73 lines (56 loc) · 1.85 KB
/
canvas.txt
File metadata and controls
73 lines (56 loc) · 1.85 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
html5 canvas
See http://www.w3.org/TR/2dcontext/
# current state
context.fillStyle = "slateblue" # color of filled region
context.strokeStyle = "#000" # color of lines
context.lineWidth = 3 # line width
context.globalAlpha = 0.5 # transparency
context.font = "20px Sans-Serif"
context.textBaseline = "middle" # vertical centering of text
context.textAlign = "center" # horizontal centering of text
context.lineCap = "butt"
context.lineJoin = "bevel"
context.save() # save current state
context.restore() # restore last saved state
# make a rectangle
context.fillRect(x, y, width, height) # filled rectangle
context.strokeRect(x, y, width, height) # outline of rectangle
context.clearRect(x, y, width, height) # clear rectangular region
# lines (stroke goes outward in each direction from the x,y coordinates
context.beginPath()
context.moveTo(w/8, h/8)
context.lineTo(w*7/8, h/8)
context.lineTo(w*7/8, h*7/8)
context.stroke()
context.closePath()
# circle
context.strokeStyle = "black"
context.beginPath()
context.arc(w/8, h/2, w/16, 0, 2*Math.PI, false)
context.stroke() # or .fill()
context.closePath()
# arc
context.beginPath()
context.strokeStyle = "violetred"
context.lineWidth = 5
context.arc(w/8, h/2, w/16, Math.PI/2, Math.PI, false)
context.stroke()
context.closePath()
#### create a canvas region in D3
canvasApp = () ->
return unless Modernizr.canvas
w = 800
h = 500
d3.select("div.canvas")
.append("canvas")
.attr("id", "canvasOne")
.attr("width", w)
.attr("height", h)
theCanvas = d3.select("canvas#canvasOne")[0][0]
context = theCanvas.getContext("2d")
console.log("Drawing canvas")
drawScreen = () ->
context.strokeStyle = "#000"
context.strokeRect(0, 0, w, h)
drawScreen()
window.onload = canvasApp