Skip to content

Commit 2a9c325

Browse files
committed
Relocate
1 parent 12921c9 commit 2a9c325

File tree

3 files changed

+102
-13
lines changed

3 files changed

+102
-13
lines changed

src/plugins/plugin.filler.js

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import defaults from '../core/core.defaults';
1010
import Line, {_boundSegment, _boundSegments} from '../elements/element.line';
1111
import {clipArea, unclipArea} from '../helpers/helpers.canvas';
1212
import {valueOrDefault, isFinite, isArray, extend} from '../helpers/helpers.core';
13+
import {_normalizeAngle} from '../helpers/helpers.math';
1314

1415

1516
defaults._set('global', {
@@ -239,7 +240,6 @@ function _clip(ctx, target, clipY) {
239240
ctx.clip();
240241
}
241242

242-
const TAU = Math.PI * 2;
243243
function getBounds(property, first, last, loop) {
244244
if (loop) {
245245
return;
@@ -248,12 +248,19 @@ function getBounds(property, first, last, loop) {
248248
let end = last[property];
249249

250250
if (property === 'angle') {
251-
start = (start + TAU) % TAU;
252-
end = (end + TAU) % TAU;
251+
start = _normalizeAngle(start);
252+
end = _normalizeAngle(end);
253253
}
254254
return {property, start, end};
255255
}
256256

257+
function _getEdge(a, b, prop, fn) {
258+
if (a && b) {
259+
return fn(a[prop], b[prop]);
260+
}
261+
return a ? a[prop] : b ? b[prop] : 0;
262+
}
263+
257264
function _segments(line, target, property) {
258265
const points = line.points;
259266
const tpoints = target.points;
@@ -265,18 +272,34 @@ function _segments(line, target, property) {
265272
if (!target.segments) {
266273
// Special case for boundary not supporting `segments` (simpleArc)
267274
// Bounds are provided as `target` for partial circle, or undefined for full circle
268-
parts.push({source: segment, target: bounds});
275+
parts.push({
276+
source: segment,
277+
target: bounds,
278+
start: points[segment.start],
279+
end: points[segment.end]
280+
});
269281
continue;
270282
}
271283

272284
// Get all segments from `target` that intersect the bounds of current segment of `line`
273285
const subs = _boundSegments(target, bounds);
274286

275287
for (let sub of subs) {
276-
const fillSources = _boundSegment(segment, points, getBounds(property, tpoints[sub.start], tpoints[sub.end], sub.loop));
288+
const subBounds = getBounds(property, tpoints[sub.start], tpoints[sub.end], sub.loop);
289+
const fillSources = _boundSegment(segment, points, subBounds);
277290

278291
for (let source of fillSources) {
279-
parts.push({source, target: sub});
292+
parts.push({
293+
source,
294+
target: sub,
295+
start: {
296+
[property]: _getEdge(bounds, subBounds, 'start', Math.max)
297+
},
298+
end: {
299+
[property]: _getEdge(bounds, subBounds, 'end', Math.min)
300+
}
301+
302+
});
280303
}
281304
}
282305
}
@@ -303,30 +326,27 @@ function interpolatedLineTo(ctx, target, point, property) {
303326
function _fill(ctx, cfg) {
304327
const {line, target, property, color, scale} = cfg;
305328
const segments = _segments(cfg.line, cfg.target, property);
306-
const points = line.points;
307329

308330
ctx.fillStyle = color;
309331
for (let i = 0, ilen = segments.length; i < ilen; ++i) {
310-
const {source: src, target: tgt} = segments[i];
311-
const first = points[src.start];
312-
const last = points[src.end];
332+
const {source: src, target: tgt, start, end} = segments[i];
313333

314334
ctx.save();
315335

316-
clipBounds(ctx, scale, getBounds(property, first, last));
336+
clipBounds(ctx, scale, getBounds(property, start, end));
317337

318338
ctx.beginPath();
319339

320340
let loop = !!line.pathSegment(ctx, src);
321341
if (loop) {
322342
ctx.closePath();
323343
} else {
324-
interpolatedLineTo(ctx, target, last, property);
344+
interpolatedLineTo(ctx, target, end, property);
325345
}
326346

327347
loop &= target.pathSegment(ctx, tgt, {move: loop, reverse: true});
328348
if (!loop) {
329-
interpolatedLineTo(ctx, target, first, property);
349+
interpolatedLineTo(ctx, target, start, property);
330350
}
331351

332352
ctx.closePath();
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
const data1 = [];
2+
const data2 = [];
3+
const data3 = [];
4+
for (let i = 0; i < 200; i++) {
5+
const a = i / Math.PI / 10;
6+
7+
data1.push({x: i, y: i < 86 || i > 104 && i < 178 ? Math.sin(a) : NaN});
8+
9+
if (i % 10 === 0) {
10+
data2.push({x: i, y: Math.cos(a)});
11+
}
12+
13+
if (i % 15 === 0) {
14+
data3.push({x: i, y: Math.cos(a + Math.PI / 2)});
15+
}
16+
}
17+
18+
module.exports = {
19+
config: {
20+
type: 'line',
21+
data: {
22+
datasets: [{
23+
borderColor: 'rgba(255, 0, 0, 0.5)',
24+
backgroundColor: 'rgba(255, 0, 0, 0.25)',
25+
data: data1,
26+
fill: false,
27+
}, {
28+
borderColor: 'rgba(0, 0, 255, 0.5)',
29+
backgroundColor: 'rgba(0, 0, 255, 0.25)',
30+
data: data2,
31+
fill: 0,
32+
}, {
33+
borderColor: 'rgba(0, 255, 0, 0.5)',
34+
backgroundColor: 'rgba(0, 255, 0, 0.25)',
35+
data: data3,
36+
fill: 1,
37+
}]
38+
},
39+
options: {
40+
animation: false,
41+
responsive: false,
42+
legend: false,
43+
title: false,
44+
datasets: {
45+
line: {
46+
lineTension: 0.4,
47+
borderWidth: 1,
48+
pointRadius: 1.5,
49+
}
50+
},
51+
scales: {
52+
x: {
53+
type: 'linear',
54+
display: false
55+
},
56+
y: {
57+
type: 'linear',
58+
display: false
59+
}
60+
}
61+
}
62+
},
63+
options: {
64+
canvas: {
65+
height: 512,
66+
width: 512
67+
}
68+
}
69+
};
47.4 KB
Loading

0 commit comments

Comments
 (0)