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
|
var CShape = function(context, usePathObject) {
this._context = context;
if (usePathObject)
this._path = new Path2D();
else
this._path = context;
};
CShape.prototype.usePathObject = function() {
return this._path instanceof Path2D;
};
CShape.prototype.createShape = function() {
// override
};
CShape.prototype.draw = function() {
var context = this._context;
var path = this._path;
context.beginPath();
this.createShape();
if (this.usePathObject())
context.stroke(path);
else
context.stroke();
};
CShape.prototype.scroll = function() {
var context = this._context;
var path = this._path;
if (this.usePathObject())
context.scrollPathIntoView(path);
else
context.scrollPathIntoView();
};
var overrideShape = function(overrideMethod) {
var shape = function() {
CShape.apply(this, arguments);
};
shape.prototype = new CShape;
shape.prototype.createShape = overrideMethod;
return shape;
};
var CRect = overrideShape(function() {
var path = this._path;
path.rect(-50, -50, 100, 100);
});
var CCapsule = overrideShape(function() {
var path = this._path;
path.arc(-35, 0, 50, Math.PI / 2, Math.PI * 1.5, false);
path.lineTo(35, -50);
path.arc(50, 0, 50, Math.PI * 1.5, Math.PI / 2, false);
path.lineTo(-35, 50);
});
var CStar = overrideShape(function() {
var path = this._path;
path.moveTo(0, -50);
path.lineTo(-15, -10);
path.lineTo(-50, -10);
path.lineTo(-15, 10);
path.lineTo(-35, 50);
path.lineTo(0, 20);
path.lineTo(35, 50);
path.lineTo(15, 10);
path.lineTo(50, -10);
path.lineTo(15, -10);
path.lineTo(0, -50);
});
var CCurve = overrideShape(function() {
var path = this._path;
path.moveTo(-50, -50);
path.bezierCurveTo(-50, 10, 50, 10, 50, 50);
});
var container = document.querySelector("div[class='container']");
var canvas = document.querySelector("canvas");
var context = canvas.getContext("2d");
function getRealValue(shape, degree, usePathObject) {
// reset scroll
container.scrollTop = 0;
container.scrollLeft = 0;
// draw shape stroke on canvas
usePathObject = usePathObject == undefined || usePathObject == null ? false : true;
var s = new shape(context, usePathObject);
context.clearRect(0, 0, 400, 400);
context.save();
context.translate(200, 200);
if (degree != 0 && degree != undefined && degree != null)
context.rotate(Math.PI / 180 * degree);
s.draw();
s.scroll();
context.stroke();
context.restore();
return container.scrollTop;
}
function scrollTest(shape, degree, usePathObject, expectedValue) {
var classes = [ "", "border", "padding", "padding border", "margin" ];
var offset = [ 0, 500, 500, 1000, 500 ];
for (var i = 0; i < classes.length; i++) {
canvas.className = classes[i];
window.testValue = getRealValue(shape, degree, usePathObject);
shouldBe("testValue", String(expectedValue + offset[i]));
}
}
description("Series of tests to ensure correct results of scrolling path into view on canvas");
debug("Test case 1: scrollPathIntoView() / CTM == identity");
scrollTest(CRect, 0, false, 150);
scrollTest(CCapsule, 0, false, 150);
scrollTest(CCurve, 0, false, 150);
scrollTest(CStar, 0, false, 150);
debug("");
debug("Test case 2: scrollPathIntoView() / CTM != identity");
scrollTest(CRect, 20, false, 136);
scrollTest(CCapsule, 42, false, 106);
scrollTest(CCurve, 63, false, 133);
scrollTest(CStar, 40, false, 160);
debug("");
debug("Test case 3: scrollPathIntoView(path2d) / CTM == identity");
scrollTest(CRect, 0, true, 150);
scrollTest(CCapsule, 0, true, 150);
scrollTest(CCurve, 0, true, 150);
scrollTest(CStar, 0, true, 150);
debug("");
debug("Test case 4: scrollPathIntoView(path2d) / CTM != identity");
scrollTest(CRect, 20, true, 136);
scrollTest(CCapsule, 42, true, 106);
scrollTest(CCurve, 63, true, 133);
scrollTest(CStar, 40, true, 160);
debug("");
debug("Test case 5: exceptions");
shouldThrow("context.scrollPathIntoView(null);");
shouldThrow("context.scrollPathIntoView([]);");
shouldThrow("context.scrollPathIntoView({});");
debug("");
|