-
Notifications
You must be signed in to change notification settings - Fork 2
/
ccanvas.js
180 lines (175 loc) · 6.45 KB
/
ccanvas.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
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
// Very simple canvas wrapper.
// This is intended to wrap a cairo canvas when running natively, or
// an HTML5 canvas when running in the browser. We export only the
// APIs we actually need (and don't require string parsing in the API)
// to make the native implementation easier.
// This isn't Simplified JavaScript -- we use try/finally here.
// BUGS FOUND: iOS -- the rect() primitive seems to improperly clear the area
// between 0,0 and the given x,y coordinate. I reimplemented in terms of
// the path primitives to work around.
// iOS - the text primitives don't seem to properly invalidate the rectangle
// the draw into. Therefore, unless there is some *other* drawing operation
// to expand the bounding box of the invalidation, the text will be clipped.
// C. Scott Ananian, May 13 2011
define(function() {
var make = function(canvas_id) {
var canvasElem_ = canvas_id ?
document.getElementById(canvas_id) :
document.createElement("canvas");
var canvas_ = canvasElem_.getContext('2d');
var width_ = canvasElem_.width;
var height_ = canvasElem_.height;
var scale_ = 1;
return {
fontHeight: 10, // font is 10px when canvas is created.
fontBold: false,
fontItalic: false,
// resize the underlying canvas element. Also clears the drawing area.
// scale is 'devicePixelRatio' for devices like the iPhone retina
// display where we want to gain extra resolution w/o shrinking
// everything.
resize: function(width, height, scale) {
if (width === width_ && height === height_ &&
(scale||1) === scale_) {
// skip resize, which will save a clearRect on the client
// end (since spec says we must clear the canvas whenever
// one of the width/height fields is assigned)
return; /* nothing to do */
}
width_ = width;
height_ = height;
scale_ = scale || 1;
canvasElem_.width = width_ * scale_;
canvasElem_.height = height_ * scale_;
canvas_.setTransform(scale_, 0, 0, scale_, 0, 0);
// spec will clear as a side effect of the above.
// We're not going to do it explicitly, because that would just
// waste cycles.
if (false) { /* disabled for performance */
canvas_.clearRect(0,0,width_,height_);
}
},
// get the current canvas size
size: function() {
return { width: width_, height: height_, scale: scale_ };
},
// execute the given function, saving and restoring the canvas context
// around its invocation.
withContext: function(_this_, f) {
var h, b, i;
h = this.fontHeight;
b = this.fontBold;
i = this.fontItalic;
try {
canvas_.save();
return f.apply(_this_ || this);
} finally {
canvas_.restore();
this.fontHeight = h;
this.fontBold = b;
this.fontItalic = i;
}
},
// transforms
scale: function(x, y) {
if (typeof(x)==="object") { y=x.y; x=x.x; }
canvas_.scale(x, y);
},
translate: function(x, y) {
if (typeof(x)==="object") { y=x.y; x=x.x; }
canvas_.translate(x, y);
},
transform: function(a, b, c, d, e, f) {
canvas_.transform(a, b, c, d, e, f);
},
// drawing
clearRect: function(x, y, w, h) {
// fills rectangle with transparent black
canvas_.clearRect(x, y, w, h);
},
beginPath: function() {
canvas_.beginPath();
},
closePath: function() {
canvas_.closePath();
},
arc: function(xc, yc, radius, angle1, angle2, negative) {
canvas_.arc(xc, yc, radius, angle1, angle2, negative);
},
moveTo: function(x, y) {
if (typeof(x)==="object") { y=x.y; x=x.x; }
canvas_.moveTo(x, y);
},
lineTo: function(x, y) {
if (typeof(x)==="object") { y=x.y; x=x.x; }
canvas_.lineTo(x, y);
},
rect: function(x, y, w, h) {
// the rect primitive seems to have some issues on iOS
// (see above). We're going to implement it in terms of
// primitives.
this.moveTo(x,y);
this.lineTo(x+w, y);
this.lineTo(x+w, y+h);
this.lineTo(x, y+h);
this.closePath();
},
stroke: function() {
// simple line joins make it easier to compute stroke bounding
// boxes.
canvas_.miterLimit = 1;
canvas_.lineCap = "round";
canvas_.lineJoin = "round";
canvas_.stroke();
},
fill: function() {
canvas_.fill();
},
clip: function() {
canvas_.clip();
},
setStroke: function(color) {
canvas_.strokeStyle = color.toCSS();
},
setFill: function(color) {
canvas_.fillStyle = color.toCSS();
},
setStrokeWidth: function(width) {
canvas_.lineWidth = width;
},
setFontHeight: function(height) {
// height should be in current drawing units
this.fontHeight = height;
this._updateFont();
},
setFontItalic: function(isItalic) {
this.fontItalic = isItalic;
this._updateFont();
},
setFontBold: function(isBold) {
this.fontBold = isBold;
this._updateFont();
},
_updateFont: function() {
var fs = this.fontHeight+"px sans-serif";
if (this.fontItalic) fs = "italic "+fs;
if (this.fontBold) fs = "bold "+fs;
canvas_.font = fs;
},
drawText: function(text, x, y) {
// uses the fill color
if (typeof(x)==="object") { y=x.y; x=x.x; }
canvas_.fillText(text, x, y);
},
measureText: function(text) {
var metrics = canvas_.measureText(text);
// XXX html5 canvas doesn't give us proper font metrics, boo.
return { width: metrics.width,
height: this.fontHeight,
baseline: this.fontHeight * 0.8
};
},
};
};
return make;
});