This repository has been archived by the owner on Nov 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 57
/
canvasv5.js
executable file
·187 lines (168 loc) · 5.87 KB
/
canvasv5.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
181
182
183
184
185
186
187
/**
* Copyright 2014 Google Inc. All rights reserved.
*
* Use of this source code is governed by a BSD-style
* license that can be found in the LICENSE file.
*
* @fileoverview Description of this file.
*
* A polyfill for HTML Canvas features, including
* Path2D support.
*/
(function (CanvasRenderingContext2D, nodeRequire) {
if (CanvasRenderingContext2D == undefined) {
CanvasRenderingContext2D = nodeRequire('canvas').Context2d;
}
if (CanvasRenderingContext2D.prototype.ellipse == undefined) {
CanvasRenderingContext2D.prototype.ellipse = function(x, y, radiusX, radiusY, rotation, startAngle, endAngle, antiClockwise) {
this.save();
this.translate(x, y);
this.rotate(rotation);
this.scale(radiusX, radiusY);
this.arc(0, 0, 1, startAngle, endAngle, antiClockwise);
this.restore();
}
}
if (typeof Path2D !== 'function' ||
typeof new Path2D().addPath !== 'function') {
(function() {
// Include the SVG path parser.
//= svgpath.js
function Path_(arg) {
this.ops_ = [];
if (arg == undefined) {
return;
}
if (typeof arg == 'string') {
try {
this.ops_ = parser.parse(arg);
} catch(e) {
// Treat an invalid SVG path as an empty path.
}
} else if (arg.hasOwnProperty('ops_')) {
this.ops_ = arg.ops_.slice(0);
} else {
throw 'Error: ' + typeof arg + 'is not a valid argument to Path';
}
};
// TODO(jcgregorio) test for arcTo and implement via something.
// Path methods that map simply to the CanvasRenderingContext2D.
var simple_mapping = [
'closePath',
'moveTo',
'lineTo',
'quadraticCurveTo',
'bezierCurveTo',
'rect',
'arc',
'arcTo',
'ellipse',
'isPointInPath',
'isPointInStroke',
];
function createFunction(name) {
return function() {
this.ops_.push({type: name, args: Array.prototype.slice.call(arguments, 0)});
};
}
// Add simple_mapping methods to Path2D.
for (var i=0; i<simple_mapping.length; i++) {
var name = simple_mapping[i];
Path_.prototype[name] = createFunction(name);
}
Path_.prototype['addPath'] = function(path, tr) {
var hasTx = false;
if (tr
&& tr.a != undefined
&& tr.b != undefined
&& tr.c != undefined
&& tr.d != undefined
&& tr.e != undefined
&& tr.f != undefined) {
hasTx = true;
this.ops_.push({type: 'save', args: []});
this.ops_.push({type: 'transform', args: [tr.a, tr.b, tr.c, tr.d, tr.e, tr.f]});
}
this.ops_ = this.ops_.concat(path.ops_);
if (hasTx) {
this.ops_.push({type: 'restore', args: []});
}
}
original_fill = CanvasRenderingContext2D.prototype.fill;
original_stroke = CanvasRenderingContext2D.prototype.stroke;
original_clip = CanvasRenderingContext2D.prototype.clip;
original_is_point_in_path = CanvasRenderingContext2D.prototype.isPointInPath;
original_is_point_in_stroke = CanvasRenderingContext2D.prototype.isPointInStroke;
// Replace methods on CanvasRenderingContext2D with ones that understand Path2D.
CanvasRenderingContext2D.prototype.fill = function(arg) {
if (arg instanceof Path_) {
this.beginPath();
for (var i = 0, len = arg.ops_.length; i < len; i++) {
var op = arg.ops_[i];
CanvasRenderingContext2D.prototype[op.type].apply(this, op.args);
}
original_fill.apply(this, Array.prototype.slice.call(arguments, 1));
} else {
original_fill.apply(this, arguments);
}
}
CanvasRenderingContext2D.prototype.stroke = function(arg) {
if (arg instanceof Path_) {
this.beginPath();
for (var i = 0, len = arg.ops_.length; i < len; i++) {
var op = arg.ops_[i];
CanvasRenderingContext2D.prototype[op.type].apply(this, op.args);
}
original_stroke.call(this);
} else {
original_stroke.call(this);
}
}
CanvasRenderingContext2D.prototype.clip = function(arg) {
if (arg instanceof Path_) {
// Note that we don't save and restore the context state, since the
// clip region is part of the state. Not really a problem since the
// HTML 5 spec doesn't say that clip(path) doesn't affect the current
// path.
this.beginPath();
for (var i = 0, len = arg.ops_.length; i < len; i++) {
var op = arg.ops_[i];
CanvasRenderingContext2D.prototype[op.type].apply(this, op.args);
}
original_clip.apply(this, Array.prototype.slice.call(arguments, 1));
} else {
original_clip.apply(this, arguments);
}
}
CanvasRenderingContext2D.prototype.isPointInPath = function(arg) {
if (arg instanceof Path_) {
this.beginPath();
for (var i = 0, len = arg.ops_.length; i < len; i++) {
var op = arg.ops_[i];
CanvasRenderingContext2D.prototype[op.type].apply(this, op.args);
}
return original_is_point_in_path.apply(this, Array.prototype.slice.call(arguments, 1));
} else {
return original_is_point_in_path.apply(this, arguments);
}
}
CanvasRenderingContext2D.prototype.isPointInStroke = function(arg) {
if (arg instanceof Path_) {
this.beginPath();
for (var i = 0, len = arg.ops_.length; i < len; i++) {
var op = arg.ops_[i];
CanvasRenderingContext2D.prototype[op.type].apply(this, op.args);
}
return original_is_point_in_stroke.apply(this, Array.prototype.slice.call(arguments, 1));
} else {
return original_is_point_in_stroke.apply(this, arguments);
}
}
// Set up externs.
Path2D = Path_;
})();
}
})(
typeof CanvasRenderingContext2D === "undefined" ? undefined : CanvasRenderingContext2D,
typeof require === "undefined" ? undefined : require
);